-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab1_q1
39 lines (35 loc) · 1015 Bytes
/
lab1_q1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>
#include <stdlib.h>
/*Write a program to compute the sum of all numbersthatare multiples of 4,between 30 and 1000,in three different ways:
with afor loop, a while loop and a do-while loop.After each loop print the value on the screen */
int main(void)
{
int i,sum1=0,sum2=0,sum3=0;
for(i=30;i<=1000;i++) { //for loop goes from 30 to 1000 and runs for all i less than or equal to 1000
if(i%4==0) // if numbeer is divisble by 4 it will be added to the sum
{
sum1 += i;
}
}
printf("%d",sum1);
i = 30;
while(i<=1000) { //while loop goes from 30 to 1000 and runs for all i less than or equal to 1000
i++;
if(i%4==0)
{
sum2 += i;
}
}
printf("\n%d",sum2);
i = 29; // i does not equal to 30 because we use a do while loop
do //do while loop goes from 30 to 1000 and runs for all i less than or equal to 1000
{
i++;
if(i%4==0)
{
sum3 += i;
}
}while(i<=1000);
printf("\n%d",sum3);
return 0;
}