473,324 Members | 2,501 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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

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
8 15952
sicarie
4,677 Expert Mod 4TB
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
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
4,677 Expert Mod 4TB
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
3,652 Expert 2GB
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
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
AricC
1,892 Expert 1GB
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
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
4,677 Expert Mod 4TB
Actually, I'd go for the ternary operator ?:

var ? var<100 : print var++

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

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

Similar topics

30
by: Martin Bless | last post by:
Why can't we have an additional 'print' statement, that behaves exactly like 'print' but doesn't insert that damn blank between two arguments? Could be called 'printn' or 'prin1' or 'prinn'...
14
by: Marcin Ciura | last post by:
Here is a pre-PEP about print that I wrote recently. Please let me know what is the community's opinion on it. Cheers, Marcin PEP: XXX Title: Print Without Intervening Space Version:...
3
by: SQL | last post by:
The trick is to use a pivot table Check out the code here http://sqlservercode.blogspot.com/2005/09/fast-date-ranges-without-loops-in-sql.html
22
by: Saurabh Saxena | last post by:
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.
1
by: bafidi | last post by:
i want links to read where it teachs me to print without cristal reports please help
3
by: Mike | last post by:
Hi, Is there anyway to print from VB.NET or C# and bypass the printer dialog box? Thanks Mike
0
by: JMH | last post by:
Hi, I've seen plenty of examples of printing a windows form without previewing the data, but I was wondering if anyone knew of a way to do this on a webpage? I have a webform, reportviewer...
1
by: Alba López | last post by:
Hi, I want to print a webform directly to the default printer, without showing the user the print dialog box. I used to do it with window.open and it worked as I wanted to, but after installing...
0
by: engloon | last post by:
Hi guys. I found this code that read a file then print out. The problem that I'm facing is, I have a lot of files to be printed, so I would like to print without showing the print property menu....
1
by: Pheddy | last post by:
Hi all.. Does anybody knows if it is possible to create a page that IE will print WITHOUT the url information in the bottom of the page? Thanks. Frederik
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.