You are on page 1of 6

Amrita Vishwa Vidyapeetham

Amrita School of Engineering, Bangalore


Department of Computer Science and Engineering

19CSE102-Computer Programming
Lab Handout
Loops Control Structures

Exercise Problems

1. Check the output for the following FOR loop codes

#include<stdio.h>
int main()
{
float a=5.2;
if(a == 5.2)
{
printf("equal");
}
else
{
printf("not equal");
}
}

#include <stdio.h>
Int i=10;
void main()
{
int i =20,n;
for(n=0;n<=i;n++)
{
int i=10;
i++;
}
printf("%d", i);
#inclide <stdio.h>
void main()
{
int i,j=1;
for(i=0;i<10;i++);
{
j=j+1;}
printf("%d %d",i,j);
}

#include <stdio.h>
void main()
{
int i=20,j,k=0;
for(j=1;j >i;j++)
{
k+=j<10?4:3;
}
printf("%d", k);
}

#include<stdio.h>
int main()
{
int i = 1, j = 1;
for(--i && j++ ; i<10; i+=2)
{
printf("loop ");
}
return 0;
}
#include<stdio.h>
int main()
{

for(5;2;2)
printf("Hello");
return 0;
}
#include<stdio.h>
int main()
{

for(5;2;2) ;
printf("Hello");
return 0;
}

int main()
{
int x = 3, k;
while (x-- >= 0) {
printf("%d", x);
}
return 0;
}

#include <stdio.h>
int main()
{
int c = 5, no = 10;
do {
no /= c;
} while(c--);

printf ("%d\n", no);


return 0;
}

#include <stdio.h>
int main()
{
int i;
for(i=0;i<5;i++);
{
printf("hello\n");
}
}
#include <stdio.h>
int main()
{
int i;
for(i=0;i<5;i++)
{
printf("hello\n");
}
}

#include <stdio.h>
int main()
{
int i;
for(i=0;i<5;)
{
printf("hello\n");
}
}

#include <stdio.h>
int main()
{
int i;
for(; ;i++)
{
printf("hello\n");
}
}

#include <stdio.h>
int main()
{
int i;
for(;i<5;)
{
printf("hello\n");
}
}
2. Check the output of the following WHILE loop codes

 #include<stdio.h> 
 
int main() 

int n,i=1, sum=0; 
 
printf("enter the value of n\n"); 
scanf("%d",&n); 
 
while(i<=n)  //syntax while(expression){  body of the loop} initilization of the
loop variable, condition check, loop variable updation 

sum=sum+i; 
i=i+1; 

printf("the sum is=%d\n",sum); 

#include<stdio.h> 
 
int main() 

int n,i=1, sum=0; 
printf("enter the value of n\n"); 
scanf("%d",&n); 
do     //synatax do{body of the loop} while(condition); 

sum=sum+i; 
 
i=i+1; 
}while(i<=n); 
 
 
printf("the sum is=%d\n",sum); 

Assignment Problems

1. Program to swap the first and the last digit of a given number.

2. Print the product of all the numbers entered by the user, until the user enters any value

divisible by 5.

3. Print the Fibonacci series upto n terms.

You might also like