In: Computer Science
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf())
Complete the following functions using C programming language:
To compute the sum of all numbers that are multiples of 4, between 30 and 1000, in three different ways: with a for loop, a while loop and a do-while loop, accordingly. After each loop print the value on the screen. Return the total sum at the end of each function
Code Explanation:
Code:
#include <stdio.h>
#include<conio.h>
int Q1_for();
int Q1_while();
int Q1_do();
void main()
{
printf("%d\n",Q1_for());
printf("%d\n",Q1_while());
printf("%d\n",Q1_do());
}
int Q1_for(){
int sum = 0, i;;
for(i = 30; i<=1000; i++){
if(i%4==0){
sum = sum +i;
}
}
return(sum);
}
int Q1_while(){
int i=30, sum = 0;
while(i<=1000){
if(i%4==0){
sum = sum+i;
}
i++;
}
return(sum);
}
int Q1_do(){
int i = 30, sum = 0, temp;
do{
if(i%4==0){
sum = sum + i;
}
i++;
}
while(i<=1000);
return(sum);
}
O/P:
125388
125388
125388
Code screenshot:
O/P screenshot:
(If you still have any doubts please comment I will definitely help)