By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,345 Members | 1,791 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,345 IT Pros & Developers. It's quick & easy.

print 1 to 100 without loops in c/c++

P: 5
Hi,

If any one know the answer for the following questions

Write a C++ program without using any loop (if, for, while etc) to print numbers from 1 to 100
Nov 9 '06 #1
Share this Question
Share on Google+
8 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
Hi,

If any one know the answer for the following questions

Write a C++ program without using any loop (if, for, while etc) to print numbers from 1 to 100
That's one hell of a cout statement.

If I were you i would go back with a program that printed

"numbers from 1 to 100"

(though that would probably not be beneficial to your grade ;) )
Nov 9 '06 #2

P: 3
Hi,

If any one know the answer for the following questions

Write a C++ program without using any loop (if, for, while etc) to print numbers from 1 to 100
void main()
{ int count=1
LABEL: cout<<count<<',';
count++;
if(count<=100) goto LABEL;
}
Nov 9 '06 #3

sicarie
Expert Mod 2.5K+
P: 4,677
void main()
{ int count=1
LABEL: cout<<count<<',';
count++;
if(count<=100) goto LABEL;
}
That's still an 'if' statement.
Nov 9 '06 #4

Ganon11
Expert 2.5K+
P: 3,652
Expand|Select|Wrap|Line Numbers
  1. int main() { 
  2.  
  3.    cout << 1 << endl;
  4.    cout << 2 << endl;
  5.    cout << 3 << endl;
  6.    cout << 4 << endl;
  7.    cout << 5 << endl;
  8.    cout << 6 << endl;
  9.    cout << 7 << endl;
  10.    // ...
  11.    cout << 100 << endl;
  12.    system("PAUSE");
  13.    return 0;
  14. }
  15.  
Nov 9 '06 #5

P: 3
How's this?

Expand|Select|Wrap|Line Numbers
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. void
  5. printnum(int n)
  6. {
  7.         printf("%d\n", n);
  8. }
  9.  
  10. void
  11. done(int n)
  12. {
  13.         exit(0);
  14. }
  15.  
  16. int
  17. main(void)
  18. {
  19.         int n = 1, i;
  20.         void (*f[2])(int);
  21.         f[0] = &done;
  22.         f[1] = &printnum;
  23.  
  24.         LABLE:
  25.         i = n <= 100;
  26.         (*f[i])(n);
  27.         n++;
  28.         goto LABLE;
  29.  
  30.         return 1;
  31. }
  32.  
Nov 9 '06 #6

Expert 100+
P: 1,892
That has to be the dumbest C assignment I've ever seen whatever happened to Print "Hello World"? Why wouldn't you use a loop for that?
Nov 9 '06 #7

P: 3
Heh, I'm an idiot, it can also be done recursively to avoid anything like a loop.

Expand|Select|Wrap|Line Numbers
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. void (*f[2])(int);
  5.  
  6. void
  7. printnum(int n)
  8. {
  9.         printf("%d\n", n);
  10.         int i;
  11.         i = ++n <= 100;
  12.         (*f[i])(n);
  13. }
  14.  
  15. void
  16. done(int n)
  17. {
  18.         exit(0);
  19. }
  20.  
  21. int
  22. main(void)
  23. {
  24.         int n = 1;
  25.  
  26.         f[0] = &done;
  27.         f[1] = &printnum;
  28.  
  29.         printnum(n);
  30.  
  31.         return 1;
  32. }
  33.  
Nov 9 '06 #8

sicarie
Expert Mod 2.5K+
P: 4,677
Actually, I'd go for the ternary operator ?:

var ? var<100 : print var++

(the above is pseudocode)
Nov 9 '06 #9

Post your reply

Sign in to post your reply or Sign up for a free account.