C Programming: Finding Factors Using 'do-while' Loop and User Interaction
#include <stdio.h>
int main()
{
int a, b;
do
{
// Initialize 'b' to 2 for each iteration of the loop
b = 2;
// Prompt the user to enter a digit
printf("Enter a digit: \n");
// Read the input from the user
scanf("%d", &a);
// Display message for searching factors
printf("Searching: \n");
// Loop to find factors of the entered number
while (a > b)
{
if (a % b == 0)
{
// If 'b' is a factor of 'a', print 'b' and increment 'b' to search for the next factor
printf("%d \n", b);
b++;
}
else
{
// If 'b' is not a factor of 'a', increment 'b' to check the next number
b++;
}
}
// Display message after listing factors
printf("Factors are listed above... \n");
// Prompt the user to repeat the loop if they wish to find factors for another number
printf("Press 1 if you want to repeat this: \n");
scanf("%d", &a);
} while (a == 1); // Loop continues if 'a' is 1
return 0;
}
Comments
Post a Comment