Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Tutorial on concept of

loops
1. #include<stdio.h> a. Infinite loop
int main() b. Compilation error
{ c. 0
int n=10; d. -1
for ( ; n>=0;--n); e. None of the
printf("n = %d", n); above
return 0; f. 9 8 7 6 5 4……….
}
2. #include<stdio.h> a. Compilation error
int main() b. Infinite loop
{ c. 9 7 5 3 1 -1……...
int n; d. None of the above
for (n = 9; n!=0; n--)
printf("n = %d", n--);
return 0;
}
3. #include<stdio.h>
int main()
{
int a=1; a. 1 1 1 2 2 2 3 3 3...
int b=1; b. 111
int c=1; c. Nothing will be
while(a==b==c) printed
{ d. None of the above
printf(“%d%d%d”,a,b,c);
a++;b++;c++;
}
return 0;
}
4. int main( )
{
int a; a. Compilation error
a=6; b. Bye
if(a==5); c. HelloHiBye
{ d. None of the above
printf(“Hello”);
printf(“Hi”);
}
printf(“Bye”);
}
5. int main( )
{
int a; a. Compilation error
a=5; b. hellohi
if(a==5); c. Nothing will be
printf(“hello”); printed
d. None of the above
printf(“hi”);
else
printf(“bye”);
}
6. #include<stdio.h> a. Compilation error
int main() b. 222222222...
{ c. 233333…….
int n=1; d. 12222222…….
for ( n>5;n++;n=2) e. None of the
printf("n = %d", n); above
return 0;
}
7. Write a program to reverse a number.

Enter an integer: 2345

Reversed number = 5432


Answer
int main() {
int n, rev = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
while (n != 0) {
remainder = n % 10;
rev = rev * 10 + remainder;
n /= 10;
}
printf("Reversed number = %d", rev);
return 0;
}
8. Print the following pattern using loop.

**

***

****

*****
Answer:
int main()
{
int i, j, rows;
printf("Enter number of rows: ");
scanf("%d", &rows);
for (i=1; i<=rows; ++i)
{
for (j=1; j<=i; ++j)
{
printf("* ");
}
printf("\n");
}
return 0;}
9. WACP to check given number is palindrome or not. ( USE
earlier reverse program and change it).

Example:

121 is palindrome

123 is not palindrome


Answer
#include <stdio.h> // palindrome if
int main() orignalN and
reversedN are equal
{
if(originalN
int n,reversedN = 0,remainder,originalN; ==reversedN)
printf("Enter an integer: "); printf("%d is a
scanf("%d", &n); palindrome.",
originalN);
originalN = n; else
while (n != 0) { printf("%d is not a
remainder = n % 10; palindrome.",
originalN);
reversedN = reversedN * 10 + remainder;
return 0;
n /= 10;
}
}

You might also like