-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp6b_controlStructures.c
58 lines (45 loc) · 1.04 KB
/
p6b_controlStructures.c
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// 2. LOOPING STRUCTURES : while, do-while, for
#include<stdio.h>
int main()
{
// i) GCD using while loop
int a,b;
printf("Enter two numbers:");
scanf("%d %d",&a,&b);
while(a!=b){
if(a>b)
a-=b;
else
b-=a;
}
printf("GCD:%d\n",a);
//or
int rem;
rem = a%b;
while(rem!=0){
a=b;
b=rem;
rem=a%b;
}
printf("GCD:%d\n",b);
// ii) sum of numbers using for loop
int i, sum; int n=5;
//sum=0;
for(int j=1,i=0,sum=0; i<=5; ++i,j++){
sum+=i;
printf("j:%d\n",j);
printf("sum:%d\n",sum);
}
//printf("Sum:%d\n",sum); //garbage value as it was initialised inside the loop and used
//printf("j:%d\n",j); //error: use of undeclared identifier 'j' - declared inside loop cannot be used outside the same loop
// iii) sum of digits using do-while loop
sum=0;
int z = 1234;
do{
sum+=z%10;
z/=10;
}
while(z);
printf("SUM : %d\n",sum);
return 0;
}