KEMBAR78
Loops Questions | PDF | Control Flow | Software Development
0% found this document useful (0 votes)
35 views13 pages

Loops Questions

The document contains a series of C programming code snippets that demonstrate various types of loops, including while, for, and do-while loops. Each snippet includes different logic and conditions, showcasing how loops can be used to control program flow and perform repetitive tasks. Additionally, there are questions related to loop execution counts and expected outputs from the provided code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views13 pages

Loops Questions

The document contains a series of C programming code snippets that demonstrate various types of loops, including while, for, and do-while loops. Each snippet includes different logic and conditions, showcasing how loops can be used to control program flow and perform repetitive tasks. Additionally, there are questions related to loop execution counts and expected outputs from the provided code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

LOOPS

1.#include <stdio.h>
int main(){
int i, j , x=4;
i = 1 ; j = 1;
while ( i< 10 ) {
j = j * i;
i= i + 1;
if (i == x) break;
printf("%d %d\n",i,j);
}
}

2.#include <stdio.h>
int main()
{
int i = 1;
do
{
if(i % 2 != 0)
printf("%d ", i);
i++;
} while(i <= 5);
return 0;
}

3.#include <stdio.h>
#define and &&
#define or ||
int main() {
int var1=5,var2=3;
int res;
for(;var1 and var2;)
{
var1=var1 or ~var2;
var2=~var2 and !var1;
}
printf("%d %d",var1,var2);
}

4.How many times the loop will execute?

#include <stdio.h>
int main()
{
int x;
for(x = 1; x = 3; x++)
printf("Hi");
}

5.#include <stdio.h>
int main() {
int num = 0;
do {
--num;
printf("%d",--num);
if(num<-5)
break;
}while(num < 2);
}

6.#include <stdio.h>
int main() {
char i=0;
for(i=34;i>-128;i--);
i--;
printf("%d",i);
}

7.#include <stdio.h>
int main()
{
int sum = 0, i = 2;
while(i <= 10)
{
sum += i;
i += 2;
} printf("%d", sum);
return 0;
}

8.#include <stdio.h>
int main()
{
int i = 0;
while(i < 5)
{
printf("%d ", i);
return 0;
}
}

9.#include <stdio.h>
int main()
{
int i;
for (i = -2; i < -5; i++)
if(i==-3)
printf("welcome\n");
else
printf("hai\n");
}

10.#include <stdio.h>
int main()
{
int i;
for(i = 1; i <= 4; i++)
{
if(i % 2 == 0)
continue;
printf("%d ", i);
}
return 0;
}

11.What is the value of digit in the following program ?

#include <stdio.h>
int main() {
int digit = 0;
for(; digit <= 9; )
digit++;
digit *= 2;
--digit;
return 0;
}

12.#include <stdio.h>
int main() {
char c=10;
for(c++;c--;c>2);
printf("%d",c--);
}

13.# include <stdio.h>


int main()
{
int i = 0;
for (i=0; i<20; i++)
{
switch(i)
{
case 0:
i += 5;
case 1:
i += 2;
case 5:
i += 5;
default:
i += 4;
break;
}
printf("%d ", i);
}
return 0;
}

14.#include<stdio.h>
int main() {
int i = 3;
while(i>>1)
{
switch (i) {
case 1:printf("0");
break;
case 1+2:printf("1");
default:printf("2");return 0;
}
}
}

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

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


return 0;
}

16.#include<stdio.h>
int main()
{
int i=1;
char ch='a';
do
{
printf("%c\n",ch);
}while(i++<2||++ch<100);
printf("%d\n",i);
}

17.#include<stdio.h>
int main(){
int a,b;
for(a=5,b=10;a+b;)
{
printf("%d %d\n",a,b);
a/=2;
b%=2;
}
}

18.#include <stdio.h>
int main()
{
int x = 3;
while(x==3)
{
if (x == 2);
x = 0;
if (x == 3)
x++;
else x += 2;
printf("x = %d", x);
return 0;
}
}

19.#include<stdio.h>
int main()
{
int i=0,j=6,k=6;
for(;1;)
{
if(i==j || k<0)
break;
printf("%d %d %d \n",i,j,k);
i++;j--;
k=k-4;
}
}

20.#include<stdio.h>
int main()
{
int i=0;
while(i++==0)
printf("%d",++i);
printf("\n");
}

21.The minimum number of execution for do while loop is _____.

22.#include<stdio.h>
int main()
{
int i = -5;
while (i <= 5)
{
if (i >= 0)
break;
else
{
i++;
continue;
}
printf("Hello");
}
return 0;
}

23.#include <stdio.h>
int main()
{
int index;
for(index=1; index<=5; index++)
{
printf("%d", index);
if (index==3)
continue;
}
}

24.#include<stdio.h>
int main()
{
int i=5;
while(i++>=i)
printf("%d ",i);
while(i++<--i);
printf("%d ",i);
}
25.#include<stdio.h>
int main()
{
int r=12,s=35;
for( ;r<p,s&p;r++,s++ )
printf("hello");
printf("hai");
}

26.#include<stdio.h>
int main()
{
int i,val;
for(i=0;i<10;i+=3)
val+=i*i;
printf("%d\n",val);
}

27.#include <stdio.h>
int main()
{
unsigned int i = 65000;
while (i++ != 0);
printf("%d", i);
return 0;
}

28.#include <stdio.h>
int main()
{
int i = 1, j;
for ( ; ; )
{
if (i)
j = --i;
if (j < 10)
printf("Yes", j++);
else
break;
}
return 0;
}

29.What is the output of the following code


#include <stdio.h>
int main()
{
int a=15;
do
printf("loop");
while (a/=2 && 0);
}

30.#include <stdio.h>
int main()
{
int i,j,a;
for(i=0;i<5;i++)
for(j=i;j>0;j--)
a=i+j+3;
printf("a=%d\n",a);
}

31.#include <stdio.h>
void main()
{
int i = 0;
while (i < 4)
{
i+=i*2;
printf("hi\n");
while (i < 4)
{
i++;
printf("hello\n");
}
}
}

32.What is the purpose of break statement and where it can be used.

33.#include <stdio.h>
int main()
{
int i = 1, j = 0;
while (i< 3, j<6)
{
i*=2;
j=j+1;
}
printf("%d, %d\n", i, j);
}

34.#include <stdio.h>
int main()
{
int i = 1, j = 0;
for(i=0;i<2;i++)
{
continue;
for(j=0;j<3;j++)
{
if(i==1)
break;
}
printf("%d %d",i,j);
}
printf("%d %d\n", i, j);
}

35.#include <stdio.h>
int main()
{
char c = '1';
while(c<53 || c<54 && c<55)
{
printf("%c ",c);
c+=2;
}
}

36.#include <stdio.h>
int main()
{
int a=0xa0;
char b=a>>7;
while(b)
{
switch(b+48)
{
case 1:printf("hai");
case '1':printf("hello");break;
}
b=!b;
}
}

37.#include <stdio.h>
int main() {
int val=10;
for(;;)
{
while(1)
{
break;
val^=6;
}
break;
}
printf("%d",val);
}

38.#include <stdio.h>
int main() {
int no=10;
l:
for(no++; ;no++)
{
printf("%d ",no+=2);
if(no==13)
goto l;
break;
}
}

39.#include<stdio.h>
int main()
{
int k,j;
for(k=0;k<=4;k++)
{
for(j=0;j<3;j++)
if(j!=k)
continue;
else
break;
printf("%d,%d\n",k,j);
}
}

40.#include<stdio.h>
int main()
{
char ch=257;
do
{
if(ch>2)
continue;
else
printf("%d\n",ch);
}while(ch>>2);
}

41.#include<stdio.h>
int main()
{
int a=10;
label:
while(1)
{
printf("hello\n");
if(a++<15)
goto label;
else
break;
}
printf("%d\n",a);
}

42.#include<stdio.h>
int main()
{
int s=4>>3;

while(!s)
{
switch(~s)
{
default:printf("default\n");break;
case 0:printf("hai\n");break;
case 1:printf("hello\n");break;
case -1:printf("bye\n");
default:printf("welcome\n");break;
}
}
}

43.#include<stdio.h>
int main()
{
int a=20;
float m=2,m2=4;
for(;a>5;a++)
{
a=a^m;
printf("%d",a);
a=a^m2;
printf("%d",a);
}
}

44.#include<stdio.h>
int main()
{
int k,a=2;
while(k=sizeof('A'),a--)
{
printf("%d\n",k);
}
}

45.#include<stdio.h>
int main()
{
int n=5;
do
{
switch(n>>1)
{
case 1:printf("case1\n");break;
case 3:printf("case2\n");break;
case 2:goto label;

}
}while(1);
label:
printf("welcome\n");

46.#include<stdio.h>
main()
{
char ch= 'b' , ch1 = ch ;
for(;;)
{
ch = ch + '1';
ch1= ch1 - 32;
printf("ch= %d , ch1= %d \n", ch , ~ch1 );
break;
}
}

47.#include<stdio.h>
int main()
{
int coding='2'+ 05;
while(coding)
{
printf("%x \n",coding);
coding=!coding;
}
}

48.#include<stdio.h>
int main()
{
int x = 5, counter = 1 ;
while ( (x -1 ) )
{ ++counter ;
x -- ;
}
printf("%d ",counter);
}

49.#include <stdio.h>
int main()
{
int i = 3;
while (i--)
{
int i = 100;
i--;
printf("%d ", i);
}
return 0;
}

50.Write a program to find the sum of odd digits and product of even digits in a given
number.
(Ex: no=12345
Sum of odd digits = 1+3+5=9
Product of even digits = 2*4=8
)

You might also like