Divide two numbers or print if division isn't possible
C Practice Exercise
Write a C program to calculate the sum of all numbers not divisible by 17 between two given integer numbers.
Sample Solution:
C Code:
#include <stdio.h>
int main() {
int x, y, temp, i, sum=0;
// Prompt for user input
printf("\nInput the first integer: ");
scanf("%d", &x);
printf("\nInput the second integer: ");
scanf("%d", &y);
// Swap values if x is greater than y
if(x > y) {
temp = y;
y = x;
x = temp;
}
// Calculate the sum of numbers not divisible by 17
for(i = x; i <= y; i++) {
if((i % 17) != 0) {
sum += i;
}
}
// Display the sum
printf("\nSum: %d\n", sum);
return 0;
}
Sample Output:
Input the first integer: 50 Input the second integer: 99 Sum: 3521
Flowchart:
C Programming Code Editor:
Previous: Write a program that reads two numbers and divide the first number by second number. If the division not possible print "Division not possible".
Next: Write a C program to find all numbers which dividing it by 7 and the remainder is equal to 2 or 3 between two given integer numbers.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics