473,387 Members | 1,597 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,387 software developers and data experts.

Nested Loop

Hi guys,

how can i write a program look like this
enter a number > than 1
1
wrong input
enter a number > than 1
11
prime number
press any key...........

enter a number > than 1
18
18 = 2 * 9
18 = 3 * 6
18 = 6 *3
18 = 9 * 2
press any key...........

this my work but it didn't work as it required

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main(){
  4. int num,i,o,j,k=50;
  5.  
  6. do{
  7.    printf ("Enter end values>\n");
  8.    scanf("%d",&num);
  9.    if (num<=1 )
  10.    printf("wrong in put\n"); break;
  11.    }
  12.    while (num>1); 
  13.           for (i = 2; i<num; i++)
  14.           {
  15.           if ((num % i) == 0)
  16.           {
  17.           o=1;
  18.           break;
  19.           }
  20.           }
  21.           if (o==2)
  22.           printf("prime number\n");
  23.           else
  24.           for (i=num; i<=num; i++) {
  25.           printf("The divisors of %d are \n",i);
  26.           for (j=2; j<=k;  j++) {
  27.           if(i%j==0) 
  28.           printf("%d\n",j); 
  29.           }  
  30.           }
  31.    system ("pause");
  32.    return 0; 
  33.    }
Apr 7 '10 #1

✓ answered by Banfa

Are you sure about the knowing how it works because your loop round lines 6 to 12 of your listing never runs more than once because of the break on line 10. It does not ensure a correct input value and it passes whatever value it is given onto the rest of the code.

Also you claim to be able to do the complex things like finding the divisors of a number but not the simple things like printing the result??

If X is a divisor of Y then

printf("%d = %d * %d\n", Y, X, Y/X);

should do it.

8 2807
newb16
687 512MB
Do you understand how your work works? What prevents you from making it work as required?
Apr 7 '10 #2
yes,of course i know how it work.
but i don't know how i can make it work as it required.
i know how to find the divisors of a number like 18
i can make the program show the divisors of a number
like
2
3
6
9
18

but how i can make the program show it me like that
18 = 2 * 9
18 = 3 * 6
18 = 6 *3
18 = 9 * 2
Apr 7 '10 #3
Banfa
9,065 Expert Mod 8TB
Are you sure about the knowing how it works because your loop round lines 6 to 12 of your listing never runs more than once because of the break on line 10. It does not ensure a correct input value and it passes whatever value it is given onto the rest of the code.

Also you claim to be able to do the complex things like finding the divisors of a number but not the simple things like printing the result??

If X is a divisor of Y then

printf("%d = %d * %d\n", Y, X, Y/X);

should do it.
Apr 7 '10 #4
think you every one
you helped me alot

but stil there is somthing wrong with the do-while
if i put 1 the program show
wrong input
prime number

the same problem if i write any number below 1
how can i make it stop at wrong input if i write any number below 1
Apr 7 '10 #5
jkmyoung
2,057 Expert 2GB
I think you want continue as opposed to break?

--edit Actually, just take out the break completely, like suggested earlier.
Apr 7 '10 #6
Guys if I remove the break on line 10
the program will not work

the only problem is when I write 1 or less
it prints
wrong input
prime number

I want it to prints only ((( wrong input )))

please guys help me how I can solve this problem . . .
Apr 7 '10 #7
newb16
687 512MB
Dump the 'beak' and change loop condition to (num<=1) sothat it loops until correct number is entered.
Apr 7 '10 #8
Thanks every one. . .

The program is know working as I want.
Apr 8 '10 #9

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

Similar topics

25
by: chad | last post by:
I am writing a program to do some reliability calculations that require several nested for-loops. However, I believe that as the models become more complex, the number of required for-loops will...
0
by: mark | last post by:
My problem is I need to have a "nested" repeater. I have an array which I load into a hashtable - that part works great. I can setup the second repeater to work just fine, as long as it's not...
5
by: Martin Schou | last post by:
Please ignore the extreme simplicity of the task :-) I'm new to C, which explains why I'm doing an exercise like this. In the following tripple nested loop: int digit1 = 1; int digit2 = 0;...
46
by: Neptune | last post by:
Hello. I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)" (Sam's series), and for nested loops, he writes (p116) "It's often necessary to create a loop even when you are...
17
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html Why is C# 500% slower than C++ on Nested Loops ??? Will this problem be solved in...
9
by: Javaman59 | last post by:
Using local declarations within a block often makes code more readable, but is it less efficient? eg... void P() { while (...) { int i = ...; bool b = ...; .... } }
2
by: mark | last post by:
(not sure if this is the correct group) My problem is I need to have a "nested" repeater. I have an array which I load into a hashtable - that part works great. I can setup the second repeater...
77
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is 450% slower on something as simple as a nested loop....
5
by: =?Utf-8?B?QUEyZTcyRQ==?= | last post by:
Could someone give me a simple example of nested scope in C#, please? I've searched Google for this but have not come up with anything that makes it clear. I am looking at the ECMA guide and...
8
by: Nathan Sokalski | last post by:
I have several nested For loops, as follows: For a As Integer = 0 To 255 For b As Integer = 0 To 255 For c As Integer = 0 To 255 If <Boolean ExpressionThen <My CodeElse Exit For Next If Not...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.