473,659 Members | 2,663 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

can we write program without loops

can we write the program to write no 1 to n without using
switch,do,while ,for,goto,if and conditional operator where n will be
input by user.
Nov 14 '05 #1
22 4547
Saurabh Saxena wrote:
can we write the program to write no 1 to n without using
switch,do,while ,for,goto,if and conditional operator where n will be
input by user.


What, is your whole class coming to the NG for help? :-)

Use recursion -- 'Nuff said

--
gabriel
Nov 14 '05 #2
gabriel <no@no--spam.com> spoke thus:
Saurabh Saxena wrote:
can we write the program to write no 1 to n without using
switch,do,while ,for,goto,if and conditional operator where n will be
input by user.

Use recursion -- 'Nuff said


How will you stop recursing without using conditionals?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #3
On Thu, 5 Feb 2004 16:44:12 +0000 (UTC), Christopher Benson-Manica
<at***@nospam.c yberspace.org> wrote:
gabriel <no@no--spam.com> spoke thus:
Use recursion -- 'Nuff said


How will you stop recursing without using conditionals?


Short-circuiting.

--
#include <standard.discl aimer>
_
Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
Per the FCA, this address may not be added to any commercial mail list
Nov 14 '05 #4

"Saurabh Saxena" <sa******@vit.a c.in> wrote in message
news:30******** *************** ***@posting.goo gle.com...
can we write the program to write no 1 to n without using
switch,do,while ,for,goto,if and conditional operator where n will be input by user.


OK OK Here it is:

#include <stdio.h>
#include <stdlib.h>

typedef void (*callback)(int );
void zero(int);
void greaterZero(int );

callback callbackTable[2] = {
zero,
greaterZero
};

void zero(int a)
{
exit(0);
}

void greaterZero(int a)
{
printf("%d\n",a--);
callbackTable[a>0](a);
}

int main(int argc,char *argv[])
{
greaterZero(ato i(argv[1]));
return 0;
}

d:\lcc\problem> lcc pb.c
d:\lcc\problem> pb 10

10
9
8
7
6
5
4
3
2
1

I tried to avoid any warnings. A version with error checking
follows...

Nov 14 '05 #5

"Saurabh Saxena" <sa******@vit.a c.in> wrote in message
news:30******** *************** ***@posting.goo gle.com...
can we write the program to write no 1 to n without using
switch,do,while ,for,goto,if and conditional operator where n will be input by user.


As promised, here is a version with error
checking

#include <stdio.h>
#include <stdlib.h>

typedef void (*callback)(int );
void zero(int);
void greaterZero(int );

callback callbackTable[2] = {
zero,
greaterZero
};

void zero(int a)
{
exit(0);
}

void greaterZero(int a)
{
printf("%d\n",a--);
callbackTable[a>0](a);
}

void errormessage(in t);
void Continue(int);

callback errortable[2] = {
errormessage,
Continue
};

void errormessage(in t a)
{
printf("Wrong input\nUsage:\n pb n, where n>0\n");
exit(1);
}

void Continue(int a)
{
}
int main(int argc,char *argv[])
{
int a;

errortable[argc>1](1);
a = atoi(argv[1]);
errortable[a >0](a);
greaterZero(ato i(argv[1]));
return 0;
}

Nov 14 '05 #6
nrk
Christopher Benson-Manica wrote:
gabriel <no@no--spam.com> spoke thus:
Saurabh Saxena wrote:

can we write the program to write no 1 to n without using
switch,do,while ,for,goto,if and conditional operator where n will be
input by user.

Use recursion -- 'Nuff said


How will you stop recursing without using conditionals?


No. The thing forbidden is the use of the conditional operator (?:).
You're allowed to use logical operators (I assume).

#include <stdio.h>

void print(int n) {
n && (print(n-1), 1) && printf("%d\n", n);
}

int main(void) {
print(10);
return 0;
}

-nrk.

--
Remove devnull for email
Nov 14 '05 #7
Saurabh Saxena <sa******@vit.a c.in> scribbled the following:
can we write the program to write no 1 to n without using
switch,do,while ,for,goto,if and conditional operator where n will be
input by user.


#include <stdio.h>
int main(void) {
char input[100];
scanf("%100s", input);
printf("1 to %s\n", input);
return 0;
}

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"To know me IS to love me."
- JIPsoft
Nov 14 '05 #8
nrk
Joona I Palaste wrote:
Saurabh Saxena <sa******@vit.a c.in> scribbled the following:
can we write the program to write no 1 to n without using
switch,do,while ,for,goto,if and conditional operator where n will be
input by user.


#include <stdio.h>
int main(void) {
char input[100];
scanf("%100s", input);
printf("1 to %s\n", input);
return 0;
}


Possible UB.

-nrk.
--
Remove devnull for email
Nov 14 '05 #9


Saurabh Saxena wrote:
can we write the program to write no 1 to n without using
switch,do,while ,for,goto,if and conditional operator where n will be
input by user.


I can't imagine any possibile way that this is not a homework
assignment, which begs the question "Why are so many of us immediately
providing solutions instead of guidance?".

Ed.

Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
8799
by: Ron Adam | last post by:
Hi, I'm having fun learning Python and want to say thanks to everyone here for a great programming language. Below is my first Python program (not my first program) and I'd apreciate any feedback on how I might do things differently to make it either more consice, readable, or faster. ie... are there better ways to do it in Python? It won't break any records for calculating pi, that wasn't my goal, learning Python was. But it might...
12
7006
by: Steven Bethard | last post by:
So I need to do something like: for i in range(len(l)): for j in range(i+1, len(l)): # do something with (l, l) where I get all pairs of items in a list (where I'm thinking of pairs as sets, not tuples, so order doesn't matter). There isn't really anything wrong with the solution here, but since Python's for-each construction is so nice, I usually try to avoid range(len(..)) type
1
1550
by: Starx | last post by:
I have a while loop in my program that is going to execute a very large number of loops (well over 100000000). Sometimes this can take quite some time for the computer to process so I'd like the program to supply some sort of estimate as to how long the process may take. The number of times the program must loop as well as the amount of proccessing required for the block of code in the loop is entirely dependant on choices the user makes...
88
8038
by: Peter Olcott | last post by:
Cab you write code directly in the Common Intermediate language? I need to optimize a critical real-time function.
7
7902
by: Gernot Frisch | last post by:
Hi, I want to have a representative information about the read and write speeds of a drive (cf-card, HDD and RAM-Disk). I think the buffering of the filesystem must be fooled somehow for my test program (sorry, uses GetTickCount() == Win32 for Milliseconds) # # #
18
1945
by: HYRY | last post by:
I want to join two mono wave file to a stereo wave file by only using the default python module. Here is my program, but it is much slower than the C version, so how can I increase the speed? I think the problem is at line #1, #2, #3. import wave import array lfile = wave.open(lfilename) rfile = wave.open(rfilename)
3
1665
by: kalyankiran33 | last post by:
I have com across a program in which I should take 17 as a global constant. I have to take 17 inputs. for those inputs .. I have to find the number of even numbers entered the number of odd numbers entered the number of prime numbers entered smallest number among those 17 largest number among those 17 factorial of the smallest number
3
10556
by: jm.suresh | last post by:
I am trying to measure the time the processor spends on some operation, and I want this measure to not depend on the current load of the machine. But doing the following prints different values each time a run. import time c1 = time.clock() for x in xrange(0xFFFFF): pass c2 = time.clock()
12
2081
by: lalou89 | last post by:
Develop a simple text editor program. The program will show the user a menu of choices and will act according to his choice. Use functional decomposition to break the system into small functions that are accessed by the main program to provide the system functionality. Using the text editor, the user can: • Input the name of a file • The program will check if a file with this name exists or not. If yes, it will tell the user “This File...
0
8746
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8523
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8626
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6178
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4334
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2749
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.