Write a C program requesting users to enter any two positive integers. Please find the sum ofall integers between these two numbers that can be divided (divisible) by four (4).Your program should validate that both numbers are positive. If it is not so, inform the user untilhe enters a positive integer. The user can enter a larger number as the first integer and a smallernumber as the second intege
Question
Write a C program requesting users to enter any two positive integers. Please find the sum ofall integers between these two numbers that can be divided (divisible) by four (4).Your program should validate that both numbers are positive. If it is not so, inform the user untilhe enters a positive integer. The user can enter a larger number as the first integer and a smallernumber as the second intege
Solution
Sure, here is a simple C program that fulfills your requirements:
#include <stdio.h>
int main() {
int num1, num2, sum = 0;
// Request the first number
printf("Enter the first positive integer: ");
scanf("%d", &num1);
// Validate the first number
while(num1 <= 0) {
printf("Invalid input. Please enter a positive integer: ");
scanf("%d", &num1);
}
// Request the second number
printf("Enter the second positive integer: ");
scanf("%d", &num2);
// Validate the second number
while(num2 <= 0) {
printf("Invalid input. Please enter a positive integer: ");
scanf("%d", &num2);
}
// Ensure num1 is smaller than num2
if(num1 > num2) {
int temp = num1;
num1 = num2;
num2 = temp;
}
// Calculate the sum of numbers divisible by 4
for(int i = num1; i <= num2; i++) {
if(i % 4 == 0) {
sum += i;
}
}
// Print the result
printf("The sum of all integers between %d and %d that are divisible by 4 is: %d\n", num1, num2, sum);
return 0;
}
This program first asks the user to input two positive integers. It validates the input and asks again if the input is not valid. It then ensures that the first number is smaller than the second one. After that, it calculates the sum of all numbers between the two numbers that are divisible by 4 and prints the result.
Similar Questions
rite a C program requesting users to enter any two positive integers. Please find the sum ofall integers between these two numbers that can be divided (divisible) by four (4)
The program must accept four integers a, b, c and d as input. The program must solve the equation b * c - (a + d) and print it as the output.
Problem Statement:Write a program to accept 4 numbers as input and sequence them in the descending order. You may assume that the numbers will not be equal.Input Format:Four space separated Integers - num1, num2, num3 and num4.Output Format:Display the four integers in the decreasing order - integer>integer>integer>integerConstraints1<=num<=2,147,483,647Sample Input 16789 6790 6788 6787Sample Output 16790>6789>6788>6787Sample Input 20 1 2 3Sample Output 23>2>1>0
.Write a C program that prompts a user for two integers and the appropriate operator(i.e. +,-,/,*) then uses switch case to determine the appropriate operation.
Problem StatementSuppose you entered a 4 digit number. Write a program to display a number whose digits are 2 greater than the corresponding digits of the entered number.For example, if he entered number is 5696, then the displayed number should be 7818.Input FormatA single integer that consists of 4 digits.Output Format A Single integer where 2 should be added to every single digit of the input integerSample Input 03798Sample Output 05910Sample Input 17891Sample Output 1
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.