Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Trying to write a C program that asks the user for a positive integer. If not gi

ID: 672151 • Letter: T

Question

Trying to write a C program that asks the user for a positive integer. If not given, trap them until they give it. Use a single function for this task that returns the positive integer that the user types in.

I'm having trouple trapping the user.

Then, prompt the user for a second positive integer and trap them again if the incorrect value is given.

Once you have two positive integers, use a function to compute the largest integer that evenly divides both of the input values and print it on screen.

Your function should compute this value by starting at the smaller of the two values and counting down by one until you find a number (the number 1 will always do it) that evenly divides both of the original numbers. For example if you enter the number 8 and 4 your function will return 4 because 4 goes into 4 one time and into 8 two times. Call this function and output the result on the screen.

Example Output:

"Please enter a positive integer: -1

I'm sorry that number is unrecognized or not positive

Please enter a positive integer: 0

I'm sorry that number is unrecognized or not positive

Please enter a positive integer: 20

Please enter a second positive integer: 8

The largest integer that divdies both 20 and 8 is: 4

Press any key to continue..."

What i have so far:

#include
#include

int main(int argc, char* argv[])
{
   int x;
   printf("Please enter a positive integer: ");
   scanf("%d", &x);
   if(x <= 0)
   {
       printf("I'm sorry, that number is unrecognized or not positive ");
       printf("Please enter a positivie integer: ");
       scanf("%d", &x);
   }
   else
   {
       int y;
       printf("Please enter a second positive integer: ");
       scanf("%d", &y);
       {
           if (y<=0)
           {
               printf("I'm sorry, that number is unrecognized or not positive ");
               printf("Please enter a positivie integer: ");
               scanf("%d", &x);
           }
       }
   }
}
  

Obviously I haven't trapped the user into a loop for integers.
  

Explanation / Answer

// I have made one function getInput() for getting input from user and other function evenlyDivides() that takes two values entered by user to find largest integer which evenly divides the two integer.

// In getInput() I have used while loop which executes untill user do not enter positive number.

// In evenlyDivides() first we will check smaller of two numbers. then we will use for loop which will start iterating from this smaller number and decrementing is every step because we need to find largest number which divides these two input evenly. example suppose user enter two numbers 20 and 8. Then number which evenly divides both numbers means any number who divides both 20 and 8. So If we take 20 then 20 can not divide evenly.

SO whatever be the smaller of these two numbers is the only option which may or may not divide both number. Then if we take 8 but 8 also can not divide 20 evenly. And we need to find largest no which divides evenly So we will start checking the number from 8 and then move down one by one and check whether that number can divide both inputs evenly.

So options are 7,6,5,4,3,2,1. First 7 --can not divide

Second 6- can divide both

third 5- can not divide both

fourth 4- It can divide both numbers. So 4 is the answer.

#include<stdio.h>
int main(int argc, char* argv[])
{
int x ,y;
  
x=getInput();
y=getInput();
evenlyDivides(x,y);
return 0;
}

int getInput()
{
int input=0;
printf("Please enter a positive integer: ");
scanf("%d", &input);

// This loop is trapping the user untill he/she will not enter Positive number.

while(input<=0) // <----- this the logic for trapping the user
{
  
printf("I'm sorry that number is unrecognized or not positive ");
printf("Please enter a positive integer: ");
scanf("%d", &input);
  
}
  
return input;
  
}

void evenlyDivides(int x,int y)
{
int smaller,i;
if(x==y)
{

printf("The largest integer that divdies both %d and %d is: %d",x,y,x);
}
if(x<y)
{
smaller=x;

}
else
smaller=y;

for(i=smaller;i>0;i--) //<------ This is the logic which start with smaller of two numbers then decrementing // one by one.
{

if(x%i==0 && y%i==0)
{
printf("The largest integer that divdies both %d and %d is: %d",x,y,i);   

break;
}


}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote