Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. c program that inputs 5 pairs of integers and determine if the second one is multiple of the first one

c program that inputs 5 pairs of integers and determine if the second one is multiple of the first one

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    raeiko
    wrote on last edited by
    #1

    Hello everybody, i need some help with my code. The program seems to work,the problem is that it only outputs the result for the last pair of numbers and repeat it for 5 times. Could you please help me to understand what i'm doing wrong? Thanks a lot, raeiko /* Program that reads a pair of numbers and determines whether the second number is multiple of the first one */ #include <stdio.h> int multiple ( int j, int z); /* function prototype */ int main( void ) /* function main begins program execution */ { int num1, num2, x, i; /* declare variables */ for ( x = 1; x <= 5; x++ ){ printf( "Enter the first number:" ); /* prompt for input */ scanf_s("%d", &num1 ); /* read number from user */ if ( num1 != 0){ printf( "Enter the second number:" ); /* prompt for input */ scanf_s("%d", &num2 ); /* read number from user */ } else { break; printf( "\nBroke from loop because num1 must be greater than 0\n" ); /* break loop if num1 == 0 */ } } multiple( num1, num2); return 0; } int multiple ( int j, int z ) /* copy of the argment to function */ { int y; /* counter */ int result; result = z % j; for ( y = 1; y <= 5; y++ ) if( result == 0){ printf( "%d is multiple of %d\n", z, j ); } else { printf( "%d is not multiple of %d\n", z, j ); } return result; } /* end of multiple function */

    S CPalliniC D 3 Replies Last reply
    0
    • R raeiko

      Hello everybody, i need some help with my code. The program seems to work,the problem is that it only outputs the result for the last pair of numbers and repeat it for 5 times. Could you please help me to understand what i'm doing wrong? Thanks a lot, raeiko /* Program that reads a pair of numbers and determines whether the second number is multiple of the first one */ #include <stdio.h> int multiple ( int j, int z); /* function prototype */ int main( void ) /* function main begins program execution */ { int num1, num2, x, i; /* declare variables */ for ( x = 1; x <= 5; x++ ){ printf( "Enter the first number:" ); /* prompt for input */ scanf_s("%d", &num1 ); /* read number from user */ if ( num1 != 0){ printf( "Enter the second number:" ); /* prompt for input */ scanf_s("%d", &num2 ); /* read number from user */ } else { break; printf( "\nBroke from loop because num1 must be greater than 0\n" ); /* break loop if num1 == 0 */ } } multiple( num1, num2); return 0; } int multiple ( int j, int z ) /* copy of the argment to function */ { int y; /* counter */ int result; result = z % j; for ( y = 1; y <= 5; y++ ) if( result == 0){ printf( "%d is multiple of %d\n", z, j ); } else { printf( "%d is not multiple of %d\n", z, j ); } return result; } /* end of multiple function */

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      You need to put the multiple( num1, num2); call in the for loop - it's not in the loop in the code you've posted.

      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

      R 1 Reply Last reply
      0
      • S Stuart Dootson

        You need to put the multiple( num1, num2); call in the for loop - it's not in the loop in the code you've posted.

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        R Offline
        R Offline
        raeiko
        wrote on last edited by
        #3

        If i don't ask too much,could you please show me? This thing has been driving me crazy for one week...and i guess this is still nothing in comparison to advanced C...I feel a bit stupid... Thanks again, raeiko

        S 1 Reply Last reply
        0
        • R raeiko

          Hello everybody, i need some help with my code. The program seems to work,the problem is that it only outputs the result for the last pair of numbers and repeat it for 5 times. Could you please help me to understand what i'm doing wrong? Thanks a lot, raeiko /* Program that reads a pair of numbers and determines whether the second number is multiple of the first one */ #include <stdio.h> int multiple ( int j, int z); /* function prototype */ int main( void ) /* function main begins program execution */ { int num1, num2, x, i; /* declare variables */ for ( x = 1; x <= 5; x++ ){ printf( "Enter the first number:" ); /* prompt for input */ scanf_s("%d", &num1 ); /* read number from user */ if ( num1 != 0){ printf( "Enter the second number:" ); /* prompt for input */ scanf_s("%d", &num2 ); /* read number from user */ } else { break; printf( "\nBroke from loop because num1 must be greater than 0\n" ); /* break loop if num1 == 0 */ } } multiple( num1, num2); return 0; } int multiple ( int j, int z ) /* copy of the argment to function */ { int y; /* counter */ int result; result = z % j; for ( y = 1; y <= 5; y++ ) if( result == 0){ printf( "%d is multiple of %d\n", z, j ); } else { printf( "%d is not multiple of %d\n", z, j ); } return result; } /* end of multiple function */

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          IMHO your code has several flaws.

          • you're treating num1 and num2 in main (as well as j and z in multiple) like they where array elements, but, in fact, they aren't.

          • in the array-like scenario, the break on num1==0 is inappropriate, that 'exceptional' input would be better handled inside the multiple function (possibly with a continue).

          • the line

            raeiko wrote:

            printf( "\nBroke from loop because num1 must be greater than 0\n" );

            is wrong: you're simply rejecting num1==0, i.e. you code accepts negative num1 values.
            As already suggested by Stuart Dootson, would be probably better abandoning the array-like design: make multiply function acting on just two operands and call it inside the input loop. :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          In testa che avete, signor di Ceprano?

          1 Reply Last reply
          0
          • R raeiko

            If i don't ask too much,could you please show me? This thing has been driving me crazy for one week...and i guess this is still nothing in comparison to advanced C...I feel a bit stupid... Thanks again, raeiko

            S Offline
            S Offline
            Stuart Dootson
            wrote on last edited by
            #5

            I see what you've done. You've got the loop in main and multiple, but you're passing single integers into multiple. There are two solutions (both built & tested under darwin-gcc-4.0.1):

            1. Call multiple in the loop and remove the loop in multiple:

              /* Program that reads a pair of numbers and determines whether the second number is multiple of the first one */

              #include <stdio.h>

              int multiple ( int j, int z); /* function prototype */

              int main( void ) /* function main begins program execution */
              {
              int num1, num2, x, i; /* declare variables */

              for ( x = 1; x <= 5; x++ )
              {
              printf( "Enter the first number:" ); /* prompt for input */
              scanf("%d", &num1 ); /* read number from user */

                if ( num1 != 0)
                {
                   printf( "Enter the second number:" ); /\* prompt for input \*/
                   scanf("%d", &num2 ); /\* read number from user \*/
                } 
                else
                {
                   break;
                   printf( "\\nBroke from loop because num1 must be greater than 0\\n" ); /\* break loop if num1 = = 0 \*/
                }
                multiple( num1, num2);
              

              }

              return 0;
              }

              int multiple ( int j, int z ) /* copy of the argment to function */
              {
              int result;

              result = z % j;

              if( result == 0)
              {
              printf( "%d is multiple of %d\n", z, j );
              }
              else
              {
              printf( "%d is not multiple of %d\n", z, j );
              }
              return result;
              } /* end of multiple function */

            2. Gather the numbers into two arrays and process them in one call to multiple:

              /* Program that reads a pair of numbers and determines whether the second number is multiple of the first one */

              #include <stdio.h>

              int multiple ( int* j, int* z, int n); /* function prototype */

              int main( void ) /* function main begins program execution */
              {
              int num1[5] = {0};
              int num2[5] = {0};
              int x, i; /* declare variables */

              for ( x = 1; x <= 5; x++ )
              {
              printf( "Enter the first number:" ); /* prompt for input */
              scanf("%d", &(num1[x-1]) ); /* read number from user */

                if ( num1\[x-1\] != 0)
                {
                   printf( "Enter the second number:" ); /\* prompt for input \*/
                   scanf("%d", &(num2\[x-1\]) ); /\* read number from user \*/
                } 
                else
                {
                   break;
                   printf( "\\nBroke from loop because num1 must be greater than 0\\n" ); /\* break loop if num1 = = 0 \*/
                }
              

              }

              multiple( num1, num2, x-1);

              return 0;
              }

              int multip

            R 1 Reply Last reply
            0
            • S Stuart Dootson

              I see what you've done. You've got the loop in main and multiple, but you're passing single integers into multiple. There are two solutions (both built & tested under darwin-gcc-4.0.1):

              1. Call multiple in the loop and remove the loop in multiple:

                /* Program that reads a pair of numbers and determines whether the second number is multiple of the first one */

                #include <stdio.h>

                int multiple ( int j, int z); /* function prototype */

                int main( void ) /* function main begins program execution */
                {
                int num1, num2, x, i; /* declare variables */

                for ( x = 1; x <= 5; x++ )
                {
                printf( "Enter the first number:" ); /* prompt for input */
                scanf("%d", &num1 ); /* read number from user */

                  if ( num1 != 0)
                  {
                     printf( "Enter the second number:" ); /\* prompt for input \*/
                     scanf("%d", &num2 ); /\* read number from user \*/
                  } 
                  else
                  {
                     break;
                     printf( "\\nBroke from loop because num1 must be greater than 0\\n" ); /\* break loop if num1 = = 0 \*/
                  }
                  multiple( num1, num2);
                

                }

                return 0;
                }

                int multiple ( int j, int z ) /* copy of the argment to function */
                {
                int result;

                result = z % j;

                if( result == 0)
                {
                printf( "%d is multiple of %d\n", z, j );
                }
                else
                {
                printf( "%d is not multiple of %d\n", z, j );
                }
                return result;
                } /* end of multiple function */

              2. Gather the numbers into two arrays and process them in one call to multiple:

                /* Program that reads a pair of numbers and determines whether the second number is multiple of the first one */

                #include <stdio.h>

                int multiple ( int* j, int* z, int n); /* function prototype */

                int main( void ) /* function main begins program execution */
                {
                int num1[5] = {0};
                int num2[5] = {0};
                int x, i; /* declare variables */

                for ( x = 1; x <= 5; x++ )
                {
                printf( "Enter the first number:" ); /* prompt for input */
                scanf("%d", &(num1[x-1]) ); /* read number from user */

                  if ( num1\[x-1\] != 0)
                  {
                     printf( "Enter the second number:" ); /\* prompt for input \*/
                     scanf("%d", &(num2\[x-1\]) ); /\* read number from user \*/
                  } 
                  else
                  {
                     break;
                     printf( "\\nBroke from loop because num1 must be greater than 0\\n" ); /\* break loop if num1 = = 0 \*/
                  }
                

                }

                multiple( num1, num2, x-1);

                return 0;
                }

                int multip

              R Offline
              R Offline
              raeiko
              wrote on last edited by
              #6

              Thank you so much!!! Now i see what was wrong:-) raeiko

              1 Reply Last reply
              0
              • R raeiko

                Hello everybody, i need some help with my code. The program seems to work,the problem is that it only outputs the result for the last pair of numbers and repeat it for 5 times. Could you please help me to understand what i'm doing wrong? Thanks a lot, raeiko /* Program that reads a pair of numbers and determines whether the second number is multiple of the first one */ #include <stdio.h> int multiple ( int j, int z); /* function prototype */ int main( void ) /* function main begins program execution */ { int num1, num2, x, i; /* declare variables */ for ( x = 1; x <= 5; x++ ){ printf( "Enter the first number:" ); /* prompt for input */ scanf_s("%d", &num1 ); /* read number from user */ if ( num1 != 0){ printf( "Enter the second number:" ); /* prompt for input */ scanf_s("%d", &num2 ); /* read number from user */ } else { break; printf( "\nBroke from loop because num1 must be greater than 0\n" ); /* break loop if num1 == 0 */ } } multiple( num1, num2); return 0; } int multiple ( int j, int z ) /* copy of the argment to function */ { int y; /* counter */ int result; result = z % j; for ( y = 1; y <= 5; y++ ) if( result == 0){ printf( "%d is multiple of %d\n", z, j ); } else { printf( "%d is not multiple of %d\n", z, j ); } return result; } /* end of multiple function */

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                Check out your multiple() function. Notice that it is called outside of the loop, so it is only going to work on the last two values of num1 and num2. Also, why does the multiple() function have a for loop?

                "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups