Question 01
Write a C program that asks the user for an integer value, and outputs whether or not this integer is a multiple of 2, 3, 4, 5, 6, 7, 8, or 9.
Answer 01
There are many ways to solve this problem, and this is one of them.
#include <stdio.h>
void main()
{
int num;
int isMultiple = 0; // Flag to track if any multiples are found
// Ask the user for an integer value
printf("Enter an integer: ");
scanf("%d", &num);
// Check for multiples from 2 to 9
for (int i = 2; i <= 9; i++)
{
if (num % i == 0)
{
printf("%d is a multiple of %d\n", num, i);
isMultiple = 1; // Set flag if a multiple is found
}
}
// If no multiples were found, print a single message
if (!isMultiple)
{
printf("%d is not a multiple of any number from 2 to 9\n", num);
}
}
Last modified: Monday, 28 October 2024, 8:24 AM