Nested Loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • za777ef
    New Member
    • Apr 2010
    • 5

    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

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main(){
    int num,i,o,j,k=50;
       
    do{
       printf ("Enter end values>\n");
       scanf("%d",&num);
       if (num<=1 )
       printf("wrong in put\n"); break;
       }
       while (num>1); 
              for (i = 2; i<num; i++)
              {
              if ((num % i) == 0)
              {
              o=1;
              break;
              }
              }
              if (o==2)
              printf("prime number\n");
              else
              for (i=num; i<=num; i++) {
              printf("The divisors of %d are \n",i);
              for (j=2; j<=k;  j++) {
              if(i%j==0) 
              printf("%d\n",j); 
              }  
              }
       system ("pause");
       return 0; 
       }
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    Do you understand how your work works? What prevents you from making it work as required?

    Comment

    • za777ef
      New Member
      • Apr 2010
      • 5

      #3
      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

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        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.

        Comment

        • za777ef
          New Member
          • Apr 2010
          • 5

          #5
          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

          Comment

          • jkmyoung
            Recognized Expert Top Contributor
            • Mar 2006
            • 2057

            #6
            I think you want continue as opposed to break?

            --edit Actually, just take out the break completely, like suggested earlier.

            Comment

            • za777ef
              New Member
              • Apr 2010
              • 5

              #7
              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 . . .

              Comment

              • newb16
                Contributor
                • Jul 2008
                • 687

                #8
                Dump the 'beak' and change loop condition to (num<=1) sothat it loops until correct number is entered.

                Comment

                • za777ef
                  New Member
                  • Apr 2010
                  • 5

                  #9
                  Thanks every one. . .

                  The program is know working as I want.

                  Comment

                  Working...