Write a program in C that asks the user to enter two numbers (integers). The fir
ID: 3602885 • Letter: W
Question
Write a program in C that asks the user to enter two numbers (integers). The first integer should be considered the low value and the second integer the high value of a range. The program should loop through all the integers between the low and high values. The loop should include the low and high integers. The program must calculate and/or display the following:
1. Calculates and displays the number of integers in the range.
2. Displays the number of integers divisible by 5 OR 6, but not both.
3. Displays the sum of the integers from condition 2 as well as the sum of all the integers.
4. Displays the largest integer and the smallest integer that satisfy condition number 2.
5. Computes the average of the largest and smallest integer. Use two decimal point precision for the average.
6. Verifies that the first number is smaller than the second number and will continue to ask the user for the numbers until the user corrects the problem.
Explanation / Answer
Source Code
#include<stdio.h>
int main()
{
/*
low-Lowest number in the given input range
high-highest number in the given input range
i-looping variable
allSum-sum of all integers in the given input range
divCount-number of integers in the given range divisible by 5 or 6, but not both
divSum-sum of all integers in the given range divisible by 5 or 6, but not both
divHigh-highest integer in the given range divisible by 5 or 6, but not both
divLow-lowest integer in the given range divisible by 5 or 6, but not both
*/
int low=0, high=0,i,allSum=0,divCount=0,divSum=0,divHigh=0,divLow=0;
float avg;
printf ("Enter first number ");
scanf ("%d", &low);
printf ("Enter second number ");
scanf ("%d", &high);
//while loop executes till the first number is less than second number
while(1==1){
if(low>high){
printf("First number should be less than second number ");
printf ("Enter lowest number ");
scanf ("%d", &low);
printf ("Enter highest number ");
scanf ("%d", &high);
}else{
break;
}
}
printf ("Numbers in the range from %d to %d are",low,high);
for(i=low;i<=high;i++){
allSum+=i;
printf (" %d",i);
if(i%5==0){
if(i%6!=0){
divCount++;
divSum+=i;
if(divLow==0){
divLow=i;
}
divHigh=i;
}
}else if(i%6==0){
if(i%5!=0){
divCount++;
divSum+=i;
if(divLow==0){
divLow=i;
}
divHigh=i;
}
}
}
avg=((float)(low+high))/2;
printf(" number of integers divisible by 5 OR 6, but not both::%d",divCount);
printf(" sum of the integers divisible by 5 OR 6, but not both::%d",divSum);
printf(" The largest integer and the smallest integer that are divisible by 5 OR 6, but not both are::largest= %d, smallest=%d",divHigh,divLow);
printf (" All integer Sum %d", allSum);
printf (" Average of smallest and largest integer %.2f", avg);
return 0;
}
Output
Enter first number
20
Enter second number
30
Numbers in the range from 20 to 30 are 20 21 22 23 24
25 26 27 28 29 30
number of integers divisible by 5 OR 6, but not both::3
sum of the integers divisible by 5 OR 6, but not both::69
The largest integer and the smallest integer that are divisible by 5 OR 6, but not both are::largest= 25, smallest=20
All integer Sum 275
Average of smallest and largest integer 25.00
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.