Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 12

LOOP CONTROL INSTRUCTION SOLUTIONS

SAMPLE OUTPUT SOLUTIONS PROGRAM ASSIGNMENT SOLUTION


a) DRIEMS
1. A PROGRAM TO COMPUTE THE SUM OF DIGITS
b) 6 7 8 9 10 11 12 #include<stdio.h>
#include<conio.h>
c) Infinite Loop void main()
{
d) E D C B 65 int x,d,sum=0;
printf("Enter a number ");
e) 6 3 scanf("%d",&x);
3 0 while(x!=0)
1 0 {
0 0 d=x%10;
sum=sum+d;
f) 10 11 x=x/10;
}
g) 1 7 printf("The Sum of Digits = %d",sum);
}
h) Infinite Loop

i) Infinite Loop 2. A PROGRAM TO REVERSE A NUMBER AND CHECK THAT THE


NUMBER IS PALINDROME
j) IT and Computer
#include<stdio.h>
k) 1 2 3 4 x=5 #include<conio.h>
void main()
l) a = 11 {
a = 12 int x,d,temp,rev=0;
12 clrscr();
m) x=1 x=5 x=1 x=2 x=2 printf("Enter a number ");
scanf("%d",&x);
n) a= 19 temp=x;
a= 15 while(temp!=0)
a= 11 {
a=7 d=temp%10;
a=3 rev=rev*10+d;
temp=temp/10;
o) I=0 j=0 j=1 j=2 j=3 j=4 j=5 j=6 j=7 }
I=1 j=0 j=1 j=2 j=3 j=4 j=5 j=6 j=7 printf("The Reverse Number = %d",rev);
I=3 j=0 j=1 j=2 j=3 j=4 j=5 j=6 j=7 if(rev==x)
printf("\n%d is a Palindrome ",x);
else
printf("\n%d is not a Palindrome ",x);
} {
int x,i,f=1;
clrscr();
3. A PROGRAM TO CHECK A NUMBER IS AN ARMSTRONG NUMBER printf("Enter a number ");
scanf("%d",&x);
#include<stdio.h> for(i=1;i<=x;i++)
#include<conio.h> f=f*i;
void main() printf("The Factorial of %d = %d",x,f);
{ }
int x,d,temp,sum=0;
clrscr(); 6. A PROGRAM TO COMPUTE THE POWER(x,y)
printf("Enter a number ");
scanf("%d",&x); #include<stdio.h>
temp=x; #include<conio.h>
while(temp!=0) void main()
{ {
d=temp%10; int x,y,i,p=1;
sum=sum+d*d*d; clrscr();
temp=temp/10; printf("Enter the value of x ");
} scanf("%d",&x);
if(sum==x) printf("Enter the Value of y ");
printf("\n%d is an Armstrong Number ",x); scanf("%d",&y);
else for(i=1;i<=y;i++)
printf("\n%d is not an Armstrong Number ",x); p=p*x;
} printf("The power = %d",p);
}
4. A PROGRAM TO GENERATE A TRIANGULAR NUMBER

#include<stdio.h> 7. A PROGRAM TO COMPUTE (1+x)n


#include<conio.h>
void main() #include<stdio.h>
{ #include<conio.h>
int x,i,sum=0; void main()
clrscr(); {
printf("Enter a number "); int x,n,i,p=1;
scanf("%d",&x); clrscr();
for(i=1;i<=x;i++) printf("Enter the value of x ");
sum=sum+i; scanf("%d",&x);
printf("The Triangular Number Generated = %d",sum); printf("Enter the Value of n ");
} scanf("%d",&n);
for(i=1;i<=n;i++)
p=p*(1+x);
5. A PROGRAM TO COMPUTE THE FACTORIAL OF NUMBER printf("The power = %d",p);
}
#include<stdio.h>
#include<conio.h> 8. A PROGRAM TO CHECK A NUMBER IS A PERFECT SQUARE
void main()
#include<stdio.h> #include<conio.h>
#include<conio.h> void main()
#include<math.h> {
void main() int x,d,count=0;
{ clrscr();
int x,z; printf("Enter a Number ");
float y; scanf("%d",&x);
clrscr(); printf("\nThe Factors of the number are ");
printf("Enter a Number "); for(d=1;d<=x;d++)
scanf("%d",&x); {
y=sqrt(x); if(x%d==0)
z=y; {
if(y-z==0) printf("%d\t",d);
printf("%d is a Perfect Square ",x); count++;
else }
printf("%d is Not a Perfect Square ",x); }
} printf("\nThe Total Factors of are : %d",count);
}
9. A PROGRAM TO PRINT ALL THE PERFECT SQUARES BETWEEN
THE GIVEN RANGE 11. A PROGRAM TO CHECK A NUMBER IS PRIME NUMBER

#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
#include<math.h> void main()
void main() {
{ int x,d,count=0;
int n1,n2,num,z; clrscr();
float y; printf("Enter a Number ");
clrscr(); scanf("%d",&x);
printf("Enter the range initial value "); for(d=1;d<=x;d++)
scanf("%d",&n1); {
printf("Enter the range final value "); if(x%d==0)
scanf("%d",&n2); count++;
printf("The Perfect Square between the Range are “); }
for(num=n1;num<=n2;num++) if(count==2 || count==1)
{ printf("The Number is Prime");
y=sqrt(num); else
z=y; printf("The Number is Not Prime");
if(y-z==0) }
printf("%d\t ",num);
} 11. A PROGRAM TO PRINT ALL THE PRIME NUMBERS BETWEEN 1 to
} 1000.

10. A PROGRAM TO PRINT AND COUNT THE FACTORS OF A #include<stdio.h>


NUMBER #include<conio.h>
void main()
#include<stdio.h> {
int n1,n2,x,d,count; clrscr();
clrscr(); printf("The Perfect Number Between the Range are ” );
n1=1; for(num=1;num<=1000;num++)
n2=1000; {
printf("\nThe Prime Numbers between the Range are “); sum=0;
for(x=n1;x<=n2;x++) for(d=1;d<num;d++)
{ {
count=0; if(num%d==0)
for(d=1;d<=x;d++) sum=sum+d;
{ }
if(x%d==0) if(sum==num)
count++; printf("%d\t",num);
} }
if(count==2 || count==1) }
printf("%d\t",x);
} 14. A PROGRAM TO PRINT THE LARGEST AND THE SMALLEST
} DIGIT OF AN ENTERED NUMBER

12. A PROGRAM TO CHECK A NUMBER IS A PERFECT NUMBER #include<stdio.h>


#include<conio.h>
#include<stdio.h> void main()
#include<conio.h> {
void main() int min,max,num,d;
{ clrscr();
int x,d,sum=0; printf("Enter a Number ");
clrscr(); scanf("%d",&num);
printf("Enter a Number "); min=9;
scanf("%d",&x); max=0;
for(d=1;d<x;d++) while(num!=0)
{ {
if(x%d==0) d=num%10;
sum=sum+d; if(d<min)
} min=d;
if(sum==x) if(d>max)
printf("The Number is a Perfect Number "); max=d;
else num=num/10;
printf("The Number is Not a Perfect Number "); }
} printf("\nThe Maximum Digit = %d",max);
printf("\nThe Minimum Digit = %d",min);
13. A PROGRAM TO PRINT ALL THE PERFECT NUMBERS BETWEEN }
1 to 1000
15. A PROGRAM TO PRINT THE PRIME FACTORS OF AN ENTERED
#include<stdio.h> NUMBER
#include<conio.h>
void main() #include<stdio.h>
{ #include<conio.h>
int num,d,sum; void main()
{ void main()
int d,num,k,count; {
clrscr(); int x,y,i,p=0;
printf("Enter a Number "); printf("Enter the Value of x and y ");
scanf("%d",&num); scanf("%d %d",&x,&y);
printf("\nThe Prime Factors are "); for(i=1;i<=y;i++)
for(d=1;d<=num;d++) p=p+x;
{ printf("The Multiplication Result = %d",p);
if(num%d==0) getch();
{ }
for(k=2;k<d;k++)
{ 18. A PROGRAM TO PERFORM THE ADDITION OF TWO NUMBERS
if(d%k==0) WITHOUT USING + OPERATOR.
break;
} #include<stdio.h>
if(k==d) #include<conio.h>
printf("%d\t",d); void main()
} {
} int x,y,i,p;
} printf("Enter the Value of x and y ");
scanf("%d %d",&x,&y);
16. A PROGRAM TO PRINT THE FIBONACCI SERIES p=x;
for(i=1;i<=y;i++)
#include<stdio.h> p++;
#include<conio.h> printf("The Addition Result = %d",p);
void main() getch();
{ }
int n,ft,st,i;
printf(“Enter the No. of Terms “); 19. A PROGRAM TO COMPUTE THE QUOTIENT & REMAINDER OF
scanf(“%d”,&n); DIVISION OF TWO NUMBER.
ft = 0;
st = 1; #include<stdio.h>
printf(“%d\t%d”,ft,st); #include<conio.h>
for(i=1;i<=n-2;i++) void main()
{ {
nt = ft+st; int x,y,q=0;
printf(“%d\t”,nt); printf("Enter the Value of x and y ");
ft=st; scanf("%d %d",&x,&y);
st=nt; while(x>=y)
} {
} x=x-y;
q++;
17. A PROGRAM TO PERFORM MULTIPLICATION OF TWO NUMBER }
WITHOUT USING * OPERATOR. printf("The Quotient of Division = %d",q);
printf("\nThe Remainder of Division = %d",x);
#include<stdio.h> getch();
#include<conio.h> }
odd++;
20. A PROGRAM TO PRINT THE ARMSTRONG NUMBERS BETWEEN 1 printf("Want To Continue(Y/N) ");
TO 1000. scanf(" %c",&ch);
}while(ch=='Y');
#include<stdio.h> printf("The Total Postive Value = %d",positive);
#include<conio.h> printf("\nThe Total Negative Value = %d",negative);
void main() printf("\nThe Total Even Value = %d",even);
{ printf("\nThe Total Odd Values = %d",odd);
int x,temp,d,sum; getch();
printf("The Armstrong Numbers Between Range are "); }
for(x=1;x<=1000;x++)
{ 22. A PROGRAM TO CONVERT A BINARY NUMBER TO ITS DECIMAL
temp=x; EQUIVALENT
sum=0;
while(temp!=0) #include<stdio.h>
{ #include<conio.h>
d=temp%10; #include<math.h>
sum=sum+d*d*d; void main()
temp=temp/10; {
} int bin,dec=0;
if(sum==x) int i,d;
printf("%d\t",x); printf("Enter a Binary Value ");
} scanf("%d",&bin);
getch(); i=0;
} while(bin!=0)
{
21. A PROGRAM TO ENTER THE NUMBERS UNTIL THE USER WANTS d=bin%10;
AND AT THE END DISPLAY THE COUNT OF POSTIVE & NEGATIVE & dec=dec+pow(2,i);
ODD & ZERO. bin=bin/10;
i++;
#include<stdio.h> }
#include<conio.h> printf("The Decimal Equivalent = %d",dec);
void main() getch();
{ }
int num,positive=0,negative=0,odd=0,even=0;
char ch; 23. A PROGRAM TO GENERATE THE MULTIPLICATION TABLE OF
do ANY NUMBER.
{
printf("Enter a Value "); #include<stdio.h>
scanf("%d",&num); #include<conio.h>
if(num>0) void main()
positive++; {
else int num,i,value;
negative++; printf("Enter a Number ");
if(num%2==0) scanf("%d",&num);
even++; for(i=1;i<=10;i++)
else {
value=num*i; case 6:
printf("\n%d X %d = %d",num,i,value); printf("SIX ");
} break;
getch(); case 7:
} printf("SEVEN ");
break;
24. A PROGRAM TO CONVERT A NUMBER INTO ITS WORD case 8:
EQUIVALENT. printf("EIGHT ");
break;
#include<stdio.h> case 9:
#include<conio.h> printf("NINE ");
void main() break;
{ }
int num,rev=0,d; rev=rev/10;
printf("Enter a Value "); }
scanf("%d",&num); getch();
/* Reverse The Value of num */ }
while(num!=0)
{ 25. A PROGRAM TO PRINT THE GIVEN FIGURE
d=num%10; 1
rev=rev*10+d; 1 2
num=num/10; 1 2 3
} 1 2 3 4
1 2 3 4 5
while(rev!=0)
{
d=rev%10; #include<stdio.h>
switch(d) #include<conio.h>
{ void main()
case 0: {
printf("ZERO "); int i,j,n,x;
break; clrscr();
case 1: printf("Enter The Number of Lines ");
printf("ONE "); scanf("%d",&n);
break; for(i=1
case 2: ;i<=n;i++)
printf("TWO "); {
break; x=1;
case 3: for(j=1;j<=n-i;j++)
printf("THREE "); printf(" ");
break; for(j=1;j<=2*i-1;j++)
case 4: {
printf("FOUR "); if(j%2==0)
break; printf(" ");
case 5: else
printf("FIVE "); {
break; printf("%d",x);
x++; A
} A B
} A B C
printf("\n"); A B C D
} A B C D E
getch();
} #include<stdio.h>
#include<conio.h>
void main()
26 A PROGRAM TO PRINT THE GIVEN FIGURE {
* int i,j,n;
*** char x;
***** clrscr();
******* printf("Enter The Number of Lines ");
********* scanf("%d",&n);
********* for(i=1;i<=n;i++)
******* {
***** x='A';
*** for(j=1;j<=n-i;j++)
* printf(" ");
#include<stdio.h> for(j=1;j<=i;j++)
#include<conio.h> {
void main() printf("%c ",x);
{ x++;
int n,i,j; }
printf("Enter the Number of Lines "); printf("\n");
scanf("%d",&n); }
for(i=1;i<=n/2;i++) getch();
{ }
for(j=1;j<=n-i;j++)
printf(" "); 28 A PROGRAM TO PRINT THE GIVEN FIGURE */
for(j=1;j<=2*i-1;j++) A
printf("*"); B A
printf("\n"); C B A
} D C B A
for(i=n/2+1;i>=1;i--) E D C B A
{
for(j=1;j<=n-i;j++) #include<stdio.h>
printf(" "); #include<conio.h>
for(j=1;j<=2*i-1;j++) void main()
printf("*"); {
printf("\n"); int i,j,n;
} char x;
getch(); clrscr();
} printf("Enter The Number of Lines ");
scanf("%d",&n);
27. A PROGRAM TO PRINT THE GIVEN FIGURE */ for(i=1;i<=n;i++)
{ 0 1 0
x=64+i; 1 0 1 0
for(j=1;j<=i;j++) 1 0 1 0 1
{ 0 1 0 1 0 1
printf("%c ",x);
x--; #include<stdio.h>
} #include<conio.h>
printf("\n"); void main()
} {
getch(); int x=1,n,i,j;
} printf(“Enter the Number of Lines “);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
29. A PROGRAM TO PRINT THE GIVEN FIGURE {
1 for(j=1;j<=i;j++)
2 1 {
3 2 1 printf(“%d “,x);
4 3 2 1 if(x==1)
5 4 3 2 1 x=0;
else
#include<stdio.h> x=1;
#include<conio.h> }
void main() printf(“\n”);
{ }
int i,j,n; getch();
int x; }
clrscr();
printf("Enter The Number of Lines "); 31. A PROGRAM TO PRINT THE GIVEN FIGURE
scanf("%d",&n); A
for(i=1;i<=n;i++) A B A
{ A B C B A
x=i; A B C D C B A
for(j=1;j<=n-i;j++)
printf(" "); #include<stdio.h>
for(j=1;j<=i;j++) #include<conio.h>
{ void main()
printf("%d ",x); {
x--; int n,i,j;
} char ch;
printf("\n"); printf("Enter the Number of Lines ");
} scanf("%d",&n);
getch(); for(i=1;i<=n;i++)
} {
ch='A';
30. A PROGRAM TO PRINT THE FIGURE for(j=1;j<=n-i;j++)
1 printf(" ");
0 1 for(j=1;j<=i;j++)
{ }
printf("%c",ch); else
ch++; {
} for(j=1;j<=i;j++)
ch=ch-2; {
for(j=1;j<=i-1;j++) printf("%c ",ch);
{ ch++;
printf("%c",ch); }
ch--; for(j=1;j<=2*n-1-2*i;j++)
} printf(" ");
printf("\n"); ch=ch-1;
} for(j=1;j<=i;j++)
getch(); {
} printf("%c ",ch);
ch--;
32. A PROGRAM TO PRINT THE FIGURE }
}
A B C D E D C B A printf("\n");
A B C D D C B A }
A B C C B A getch();
A B B A }
A A
33. A PROGRAM TO EVALUATE THE SERIES
#include<stdio.h> 1 + 22 + 33 + 44 + ……….
#include<conio.h>
void main() #include<stdio.h>
{ #include<conio.h>
int n,i,j; void main()
char ch; {
printf("Enter the Number of Lines "); int n,i,j;
scanf("%d",&n); int sum=0,tv;
for(i=n;i>=1;i--) printf(“Enter the Number of terms “);
{ scanf(“%d”, &n);
ch='A'; for(i=1;i<=n;i++)
if(i==n) {
{ /* Term Value = i i */
for(j=1;j<=i;j++) tv=1;
{ for(j=1;j<=i;j++)
printf("%c ",ch); tv=tv*i;
ch++; sum=sum+tv;
} }
ch=ch-2; printf(“The Sum of the Series = %d”,sum);
for(j=1;j<=i-1;j++) getch();
{ }
printf("%c ",ch);
ch--; 34. A PROGRAM TO EVALUATE THE GIVEN SERIES
} 1 / 1! + 2 / 2! + 3 / 3! + 4 / 4! + ............
#include<stdio.h> sum = sum-tv;
#include<conio.h> else
void main() sum = sum +tv;
{ k = k+2;
int n,i,j; }
float sum=0,tv; printf(“The Sum of the series = %f “,sum);
int f; }
printf(“Enter the Number of terms “);
scanf(“%d”, &n); 36. A PROGRAM TO EVALUATE THE SERIES
for(i=1;i<=n;i++) 1 + (1+2) + (1+2+3) + (1+2+3+4) + ………….
{
/* The Term Value = i/i! */ #include<stdio.h>
f=1; #include<conio.h>
for(j=1;j<=i;j++) void main()
f=f*j; {
tv = (float)i/f; int n,i,j;
sum = sum +tv; int sum=0,tv;
} printf(“Enter the Number of terms “);
printf(“The Sum of the series = %f “,sum); scanf(“%d”, &n);
getch(); for(i=1;i<=n;i++)
} {
/* Term Value =  i */
35 A PROGRAM TO EVALUATE THE GIVEN SERIES tv=0;
x – x3 / 3! + x5 / 5! – x7 / 7! + ............ for(j=1;j<=i;j++)
tv=tv+j;
#include<stdio.h> sum=sum+tv;
#include<conio.h> }
void main() printf(“The Sum of the Series = %d”,sum);
{ getch();
int n,i,j,k; }
float sum=0,tv;
int p,f;
printf(“Enter the Number of terms “); 37. A PROGRAM TO PRINT AND COUNT THE NUMBER BETWEEN 1 TO
scanf(“%d”, &n); 100 NOT DIVISIBLE BY 2,3 AND 5.
printf(“Enter the value of x “);
scanf(“%d”,&x);
k=1; #include<stdio.h>
for(i=1;i<=n;I++) #include<conio.h>
{ void main()
p=1; {
for(j=1;j<=k;j++) int num,count=0;
p=p*x; clrscr();
f=1; for(num=1;num<=100;num++)
for(j=1;j<=k;j++) {
f = f*j; if(num%2!=0 && num%3!=0 && num%5!=0)
tv = (float)p/f; {
if(i%2==0) printf("%d ",num);
count++; getch();
} }
}
printf("\nTotal Number between 1 to 100 not divisible
by 2,3,5 =%d",count); 39. A PROGRAM TO PRINT THE MULTIPLICATION TABLE FROM 1 TO
getch(); 20.
}
#include<stdio.h>
#include<conio.h>
38. A PROGRAM TO PRINT THE NUMBERS BETWEEN 1 TO 1000 void main()
WHICH IS EXACTLY DIVISIBLE BY 4 AND IF DIVISIBLE BY 5 AND 6 {
REMAINDER OBTAINED BE 4. int i,j;
clrscr();
for(i=1;i<=20;i++)
#include<stdio.h> {
#include<conio.h> for(j=1;j<=10;j++)
void main() printf("%4d ",i*j);
{ printf("\n");
int num; }
clrscr(); getch();
for(num=1;num<=1000;num++) }
{
if(num%4==0 && num%5==4 && num%6==4)
printf("%d ",num);
}

You might also like