0% found this document useful (0 votes)
74 views

Looping Questions in C and Answers

This document provides code snippets and output for 14 looping questions in C programming. The snippets cover topics like do-while loops, while loops, for loops, continue statements, break statements, nested loops, function calls in loops, macros, and more. Each snippet is numbered and includes the output. For example, snippet 1 uses a do-while loop and extern variable to output the value 10. The snippets provide examples of different loop constructs and how to use them to solve problems.

Uploaded by

Linh Kurt
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

Looping Questions in C and Answers

This document provides code snippets and output for 14 looping questions in C programming. The snippets cover topics like do-while loops, while loops, for loops, continue statements, break statements, nested loops, function calls in loops, macros, and more. Each snippet is numbered and includes the output. For example, snippet 1 uses a do-while loop and extern variable to output the value 10. The snippets provide examples of different loop constructs and how to use them to solve problems.

Uploaded by

Linh Kurt
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 35

Looping questions in c and answers

(1)
extern int x;
void main(){
clrscr();
do{
do{
printf("%o",x);
}
while(!-2);
}
while(0);
getch();
}
int x=8;
Output: 10
(2)
void main(){
int i=2,j=2;
clrscr();
while(i+1?--i:j++)
printf("%d",i);
getch();
}
Output: 1
(3)
void main(){
int x=011,i;
clrscr();
for(i=0;i<x;i+=3){
printf("Start ");

continue;
printf("End");
}
getch();
}
Output:Start Start Start
(4)
void main(){
int i,j;
i=j=2,3;
clrscr();
while(--i&&j++)
printf("%d %d",i,j);
getch();
}
Output: 1 3
(5)
void main(){
static int i;
clrscr();
for(++i;++i;++i) {
printf("%d ",i);
if(i==4) break;
}
getch();
}
Output: 2 4
(6)
void main(){
int i=1;
clrscr();
for(i=0;i=-1;i=1) {
printf("%d ",i);

if(i!=1) break;

}
getch();

Output: -1
(7)
void main(){
int i=1;
clrscr();
for(i=0;i=-1;i=1) {
printf("%d ",i);
if(i!=1) break;
}
getch();
}
Output: -1
(8)
int r();
void main(){
clrscr();
for(r();r();r()) {
printf("%d ",r());
}
getch();
}
int r(){
int static num=7;
return num--;
}
Output: 5 2
(9)
#define p(a,b) a##b
#define call(x) #x

void main(){
clrscr();
do{
int i=15,j=3;
printf("%d",p(i-+,+j));
}
while(*(call(625)+3));
//printf("%c",*("a"+1));
getch();
}
Output: 11
(11)
int i=40;
extern int i;
void main(){
clrscr();
do{
printf("%d",i++);
}
while(5,4,3,2,1,0);
getch();
}
Output: 11
(12)
char _x_(int,...);
void main(){
char (*p)(int,...)=&_x_;
clrscr();
for(;(*p)(0,1,2,3,4); )
printf("%d",!+2);
getch();
}
char _x_(int a,...){
static i=-1;

return i+++a;

Output: 0
(13)
void main(){
int i;
clrscr();
for(i=10;i<=15;i++){
while(i){
do{
printf("%d ",1);
if(i>>1)
continue;
}while(0);
break;
}
}
getch();
}
Output: 1 1 1 1 1 1
(14)
void main(){
char c=125;
clrscr();
do
printf("%d ",c);
while(c++);
getch();
}
Output: finite time
(15)
void main(){
int x=123;

int i={
printf("c" "++")
};
for(x=0;x<=i;x++){
printf("%x ",x);
}
getch();

Output: c++0 1 2 3
Links to this post
0 comments

c interview questions and answers

1. PERFECT NUMBER.
void main()
{
int n,i=1,sum=0;
clrscr();
printf("\nEnter a number:-");
scanf("%d",&n);
while(i<n)
{
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("\nThe no %d is a perfect
number",i);
else
printf("\nThe no %d is not a perfect
number",i);
getch();
}
2. ARMSTRONG NUMBER.
void main()
{
int num,r,sum=0,temp;
clrscr();

printf("\nEnter a number:-");
scanf("%d",&num);
temp=num;
while(num!=0)
{
r=num%10;
num=num/10;
sum=sum+(r*r*r);
}
if(sum==temp)
printf("\nThe number %d is an armstrong
number",temp);
else
printf("\nThe number %d is not an
armstrong number",temp);
getch();
3. STRONG NUMBER
void main()
{
int num,i,f,r,sum=0,temp;
clrscr();
printf("\nEnter a number");
scanf("%d",&num);
temp=num;
while(num)
{
i=1,f=1;
r=num%10;
while(i<=r)
{
f=f*i;
i++;
}
sum=sum+f;
num=num/10;
}
if(sum==temp)
printf("%d is a strong number",temp);

else

printf("%d is not a strong


number",temp);
getch();
}
4. PRIME NUMBER.
void main()
{
int num,i,count=0;
clrscr();
printf("\nEnter a number:");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
if(num%i==0)
count++;
}
if(count==2)
printf("%d is a prime number",num);
else
printf("%d is not a prime number",num);
getch();
}
5. REVERSE A NUMBER
void main()
{
int num,sum=0,r;
clrscr();
printf("\nEnter a number:");
scanf("%d",&num);
while(num)
{
r=num%10;
sum=sum*10+r;
num=num/10;
}
printf("\nReverse number=%d",sum);
getch();
}

6. SUM OF THE DIGITS OF A NUMBER


void main()
{
int num,sum=0,r;
clrscr();
printf("\nEnter a number:");
scanf("%d",&num);
while(num)
{
r=num%10;
num=num/10;
sum=sum+r;
}
printf("sum=%d",sum);
getch();
}
7. PALINDROME NUMBER.
void main()
{
int num,r,sum=0,temp;
clrscr();
printf("\nEnter a number:");
scanf("%d",&num);
temp=num;
while(num)
{
r=num%10;
num=num/10;
sum=sum*10+r;
}
if(temp==sum)
printf("\n%d is a palindrome",temp);
else
printf("\n%d is not a
palindrome",temp);
getch();
}
8. G.C.D OF TWO NUMBERS
void main()
{

int n1,n2;
clrscr();
printf("\nEnter two numbers:");
scanf("%d %d",&n1,&n2);
while(n1!=n2)
{
if(n1>n2)
n1=n1-n2;
else
n2=n2-n1;
}
printf("\nGCD=%d",n1);
getch();

}
9. L.C.M OF TWO NUMBERS.11
void main()
{
int n1,n2,x,y;
clrscr();
printf("\nEnter two numbers:");
scanf("%d %d",&n1,&n2);
x=n1,y=n2;
while(n1!=n2)
{
if(n1>n2)
n1=n1-n2;
else
n2=n2-n1;
}
printf("L.C.M=%d",x*y/n1);
getch();
}
10. SWAP TWO VARIABLES WITHOUT USING THIRD VARIABLE
void main()
{
int a,b;
clrscr();
printf("\nEnter two numbers:");
scanf("%d %d",&a,&b);
printf("\nBefore swapping a=%d b=%d",a,b);

a=a^b;
b=b^a;
a=a^b;
printf("\nAfter swapping a=%d b=%d",a,b);
getch();

}
11. FLOYDS TRIANGLE
1
2 3
4 5 6
void main()
{
int i,j,r,k=1;
clrscr();
printf("\nEnter the range:");
scanf("%d",&r);
printf("\nFLOYD'S TRIANGLE\n\n");
for(i=1;i<=r;i++)
{
for(j=1;j<=i;j++,k++)
printf(" %d",k);
printf("\n");
}
getch();
}
12. PRIME FACTORS OF A NUMBER
void main()
{
int num,i=1,j,k;
clrscr();
printf("\nEnter a number:");
scanf("%d",&num);
while(i<=num)
{
k=0;
if(num%i==0)
{
j=1;
while(j<=i)
{

if(i%j==0)
j++;
}
if(k==2)

k++;

printf("\n%d

is a prime factor",i);
}
i++;
}
getch();
}
13. MULTIPLICATION TABLE
void main()
{
int r,i,j,k;
clrscr();
printf("\nEnter the number range:-");
scanf("%d",&r);
for(i=1;i<=r;i++)
{
for(j=1;j<=10;j++)
printf(" %d*%d=%d",i,j,i*j);
printf("\n");
}
getch();
}
14. FACTORIAL OF A NUMBER
void main()
{
int i=1,f=1,num;
clrscr();
printf("\nEnter a number:");
scanf("%d",&num);
while(i<=num)
{
f=f*i;
i++;
}
printf("\nFactorial of %d is:%d",num,f);

getch();

}
15. FIBONACCI SERIES
void main()
{
int i=0,j=1,k=2,r,f;
clrscr();
printf("Enter the number range:");
scanf("%d",&r);
printf("\nFIBONACCI SERIES: ");
printf("%d %d",i,j);
while(k<r)
{
f=i+j;
i=j;
j=f;
printf(" %d",j);
k++;
}
getch();
}
16. PRINTING ASCII VALUE
void main()
{
int i;
clrscr();
for(i=0;i<=255;i++)
{
printf("%d -> %c ",i,i);
delay(10);
}
getch();
}
17. CHECKING LEAP YEAR
void main()
{
int year;
clrscr();
printf("Enter any year->");
scanf("%d",&year);

if(((year%4==0)&&(year%100!=0))||(year%400==0))
printf("%d is a leap year",year);
else
printf("%d is not a leap year",year);
getch();

}
18. CONVERSION OF DECIMAL TO BINARY
void main()
{
int n,m,no=0,a=1,rem;
clrscr();
printf("Enter any decimal number->");
scanf("%d",&n);
m=n;
while(n!=0)
{
rem=n%2;
no=no+rem*a;
n=n/2;
a=a*10;
}
printf("The value %d in binary is->",m);
printf("%d",no);
getch();
}
19. CONVERSION OF BINARY TO DECIMAL
void main()
{
long int no,n=0,j=1,rem,no1;
clrscr();
printf("Enter any number any binary form->");
scanf("%ld",&no);
no1=no;
while(no!=0)
{
rem=no%10;
n=n+rem*j;
j=j*2;
no=no/10;
}

printf("\nThe value of binary no. %ld is ->


%ld",no1,n);
getch();
}
20. SWAPING OF TWO ARRAYS
void main()
{
int a[10],b[10],c[10],i;
clrscr();
printf("Enter First array->");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
printf("\nEnter Second array->");
for(i=0;i<10;i++)
scanf("%d",&b[i]);
printf("Arrays before swapping");
printf("\nFirst array->");
for(i=0;i<10;i++)
{
printf("%d",a[i]);
}
printf("\nSecond array->");
for(i=0;i<10;i++)
{
printf("%d",b[i]);
}
for(i=0;i<10;i++)
{
//write any swapping technique
c[i]=a[i];
a[i]=b[i];
b[i]=c[i];
}
printf("\nArrays after swapping");
printf("\nFirst array->");
for(i=0;i<10;i++)
{
printf("%d",a[i]);
}
printf("\nSecond array->");

for(i=0;i<10;i++)
{
printf("%d",b[i]);
}
getch();

}
21. FINDING NCR FACTOR
void main()
{
int n,r,ncr;
clrscr();
printf("Enter any two numbers->");
scanf("%d %d",&n,&r);
ncr=fact(n)/(fact(r)*fact(n-r));
printf("The NCR factor of %d and %d is
%d",n,r,ncr);
getch();
}
int fact(int n)
{
int i=1;
while(n!=0)
{
i=i*n;
n--;
}
return i;
}
22. PASCALS TRIANGLE
void main()
{
int line,i,j,k;
clrscr();
printf("Enter the no. of lines");
scanf("%d",&line);
for(i=1;i<=line;i++)
{
for(j=1;j<=line-i;j++)
printf(" ");
for(k=1;k<i;k++)

printf("%d",k);
for(k=i;k>=1;k--)
printf("%d",k);
printf("\n");
}
getch();
}
23. CONVERSION FROM UPPERCASE TO LOWER CASE
void main()
{
char str[20];
int i;
clrscr();
printf("Enter any string->");
scanf("%s",str);
printf("The string is->%s",str);
for(i=0;i<=strlen(str);i++)
{
if(str[i]>=65&&str[i]<=90)
str[i]=str[i]+32;
}
printf("\nThe string in uppercase is->%s",str);
getch();
}
24. CONVERSION FROM LOWER CASE TO UPPER CASE
void main()
{
char str[20];
int i;
clrscr();
printf("Enter any string->");
scanf("%s",str);
printf("The string is->%s",str);
for(i=0;i<=strlen(str);i++)
{
if(str[i]>=97&&str[i]<=122)
str[i]=str[i]-32;
}
printf("\nThe string in lowercase is->%s",str);
getch();

}
25. DELETE THE VOWELS FROM A STRING
void main()
{
char str[20],s[20];
int i,j=0;
clrscr();
printf("Enter any string->");
scanf("%s",str);
printf("The string is->%s",str);
for(i=0;i<=strlen(str);i++)
{
if(str[i]=='a'||str[i]=='e'||
str[i]=='i'||str[i]=='o'||str[i]=='u')
str[i]=' ';
else
s[j++]=str[i];
}
s[j]='\0';
printf("\nThe string without vowel is->%s",s);
getch();
}
26. ADDITION OF MATRICES
void main()
{
int a[3][3],b[3][3],c[3][3],i,j;
clrscr();
printf("Enter the First matrix->");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nEnter the Second matrix->");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
printf("\nThe First matrix is\n");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)

printf("%d\t",a[i][j]);

}
printf("\nThe Second matrix is\n");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",b[i][j]);
}
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];
printf("\nThe Addition of two matrix is\n");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",c[i][j]);
}
getch();
}
27. STRING PALINDROME
#include"string.h"
void main()
{
char *str,*rev;
int i,j;
clrscr();
printf("\nEnter a string:");
scanf("%s",str);
for(i=strlen(str)-1,j=0;i>=0;i--,j++)
rev[j]=str[i];
rev[j]='\0';
if(strcmp(rev,str))
printf("\nThe string is not a
palindrome");
else
printf("\nThe string is a palindrome");
getch();
}

28. COPY DATA FROM ONE FILE TO ANOTHER FILE


#include"stdio.h"
void main()
{
FILE *p,*q;
char file1[20],file2[20];
char ch;
clrscr();
printf("\nEnter the source file name to be
copied:");
gets(file1);
p=fopen(file1,"r");
if(p==NULL)
{
printf("cannot open %s",file1);
exit(0);
}
printf("\nEnter the destination file name:");
gets(file2);
q=fopen(file2,"w");
if(q==NULL)
{
printf("cannot open %s",file2);
exit(0);
}
while((ch=getc(p))!=EOF)
putc(ch,q);
printf("\nCOMPLETED");
fclose(p);
fclose(q);
getch();
}
29. ADDITION & SUBTRACTION OF TWO COMPLEX NUMBERS
void main()
{
int a,b,c,d,x,y;
clrscr();
printf("\nEnter the first complex number:");
scanf("%d%d",&a,&b);
printf("\nEnter the second complex number:");

scanf("%d%d",&c,&d);
if(b<0)
printf("%d-i\n",a-b);
else
printf("d+i\n",a+b);
if(d<0)
printf("d-i\n",c-d);
else
printf("%d+i\n",c+d);
printf("\nADDITION ");
x=a+c;
y=b+d;
if(y>0)
printf("%d-i%d",x,-y);
else
printf("%d+i%d",x,+y);
printf("\n\nSUBTRACTION ");
x=a-c;
y=b-d;
if(y<0)
printf("%d-i%d",x,-y);
else
printf("%d+i%d",x,+y);
getch();
}
30. SUM OF THE SERIES 1+2+3+---------+n
void main()
{
int r;
clrscr();
printf("\nEnter the number range: ");
scanf("%d",&r);
printf("\nSum of the series is: %d",(r*(r+1))/2);
getch();
}
31. SUM OF SQUARES OF THE SERIES 12+22+32+-------+n2
void main()
{
long int r;

clrscr();
printf("\nEnter the range: ");
scanf("%ld",&r);
printf("\nSum of the squares of the series is:
%ld",((r*(r+1))*(2*r+1))/6);
getch();
}
32. SUM OF CUBES OF THE SERIES 13+23+33+--------+n3
void main()
{
int r;
clrscr();
printf("\nEnter the number range: ");
scanf("%d",&r);
printf("\nSum of the cubes of the series is: %d",
(r*(r+1)/2)*(r*(r+1)/2));
getch();
}
33. LARGEST NUMBER IN AN ARRAY
void main()
{
int a[50],size,i,big;
clrscr();
printf("\nEnter the size of the array: ");
scanf("%d",&size);
printf("\nEnter %d elements in to the array: ,
size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);
big=a[0];
for(i=1;i<size;i++)
{
if(big<a[i])
big=a[i];
}
printf("\nBiggest: %d",big);
getch();
}
34. SECOND LARGEST NUMBER IN AN UNSORTED ARRAY

main()
{
int un[10], i, big1, big2;
printf("Enter array elements: ");
for ( i = 0; i < 10; ++i )
scanf("%d", &un[i]);
big1 = un[0];
for ( i = 1; i < 10; ++i )
{
if ( big1 < un[i] )
big1 = un[i];
if ( big1 != un[0] )
big2 = un[0];
else
big2 = un[1];
}
for ( i = 1; i < 10; ++i )
{
if ( big1 != un[i] && big2 < un[i] )
big2 = un[i];
}
printf("Second largest: %d\n", big2);
return 0;
}
35. SECOND SMALLEST NUMBER IN AN UNSORTED ARRAY
main()
{
int un[10], i, s1, s2;
clrscr();
printf("Enter array elements: ");
for ( i = 0; i < 10; ++i )
scanf("%d", &un[i]);
s1 = un[0];
for ( i = 1; i < 10; ++i )
{
if ( s1 > un[i] )
s1 = un[i];
if ( s1 != un[0] )
s2 = un[0];
else

s2 = un[1];

}
for ( i = 1; i < 10; ++i )
{
if ( s1 != un[i] && s2 > un[i] )
s2 = un[i];
}
printf("\nSecond smallest: %d", s2);
return 0;

}
36. MULTIPLICATION OF MATRICES
void main()
{
int a[5][5],b[5][5],c[5][5],i,j,k,sum=0,m,n,o,p;
clrscr();
printf("\nEnter the row and column of first
matrix");
scanf("%d %d",&m,&n);
printf("\nEnter the row and column of second
matrix");
scanf("%d %d",&o,&p);
if(n!=o)
{
printf("Matrix mutiplication is not
possible");
printf("\nColumn of first matrix must
be same as row of second matrix");
}
else
{
printf("\nEnter the First matrix->");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nEnter the Second matrix->");
for(i=0;i<o;i++)
for(j=0;j<p;j++)
scanf("%d",&b[i][j]);

printf("\nThe First matrix is\n");


for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}

}
printf("\nThe Second matrix is\n");
for(i=0;i<o;i++)
{
printf("\n");
for(j=0;j<p;j++)
{
printf("%d\t",b[i][j]);
}
}
for(i=0;i<m;i++)
for(j=0;j<p;j++)
c[i][j]=0;
for(i=0;i<m;i++)//row of first matrix
{
for(j=0;j<p;j++)//column of
second matrix
{
sum=0;
for(k=0;k<n;k+
+)
sum=sum+a[i][k]*b[k][j];
}
}

c[i][j]=sum;

}
printf("\nThe multiplication of two matrix
is\n");
for(i=0;i<m;i++)
{

}
getch();

printf("\n");
for(j=0;j<p;j++)
{
printf("%d\t",c[i][j]);
}

}
37. SUM OF DIAGONAL ELEMENTS OF A MATRIX
void main()
{
int a[10][10],i,j,sum=0,m,n;
clrscr();
printf("\nEnter the row and column of matrix");
scanf("%d %d",&m,&n);
printf("\nEnter the First matrix->");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nThe matrix is\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<m;j++)
{
printf("%d\t",a[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
sum=sum+a[i][j];
}
}
printf("\n\nSum of the diagonal elements of a
matrix is -> ");
printf("%d",sum);
getch();

}
38. TRASPOSE OF A MATRIX
void main()
{
int a[10][10],b[10][10],i,j,k=0,m,n;
clrscr();
printf("\nEnter the row and column of matrix");
scanf("%d %d",&m,&n);
printf("\nEnter the First matrix->");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nThe matrix is\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<m;j++)
{
printf("%d\t",a[i][j]);
}
}
for(i=0;i<m;i++)
for(j=0;j<n;j++)
b[i][j]=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
b[i][j]=a[j]
[i];
printf("\n
%d",b[i][j]);
}
}
printf("\n\nTraspose of a matrix is ->
");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<m;j++)

{
}
getch();

printf("%d\t",b[i][j]);

}
39. CONVERSION OF FAREHNITE TO CENTIGRADE
void main()
{
float c,f;
clrscr();
printf("Enter temp. in farehnite");
scanf("%f",&f);
c=(5*(f-32))/9;//Formula for conversion
printf("The temp. in centigrade is->%f",c);
getch();
}
40. COUNTING DIFFERENT CHARACTERS IN A STRING
main()
{
int a[26],A[26],i,c=0;
char str[100];
clrscr();
puts("Enter a string->");
gets(str);
for(i=0;i<26;i++)
{
a[i]=0;
A[i]=0;
}
for(i=0;str[i]!='\0';i++)
{
c=str[i];
if(c<97)
{
c=c-65;
A[c]++;
}
else
{

c=c-97;
a[c]++;
}

}
for(i=0;i<26;i++)
{
if(a[i]!=0)
times",i+97,a[i]);
}
for(i=0;i<26;i++)
{
if(A[i]!=0)

printf("\n%c occurs %d

printf("\n%c occurs %d

times",i+97,A[i]);
}
getch();
}
41. SORTING OF STRING
void main()
{
int i,j,n;
char str[20][20],temp[20];
clrscr();
puts("Enter the no. of string to be sorted");
scanf("%d",&n);
for(i=0;i<=n;i++)
gets(str[i]);
for(i=0;i<=n;i++)
for(j=i+1;j<=n;j++)
{
if(strcmp(str[i],str[j])>0)
{
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);

}
printf("The sorted string\n");
for(i=0;i<=n;i++)
puts(str[i]);
getch();

}
42. BUBBLE SORT
void main()
{
int s,temp,i,j,a[20];
clrscr();
printf("\nEnter size of the array: ");
scanf("%d",&s);
printf("\nEnter %d elements in to the array:",s);
for(i=0;i<s;i++)
scanf("%d",&a[i]);
for(i=0;i<s-1;i++)
{
for(j=0;j<s-1-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\nThe array after sorting is: ");
for(i=0;i<s;i++)
printf(" %d",a[i]);
getch();
}
43. SELECTION SORT
void main()
{
int s,i,j,temp,a[20];
clrscr();
printf("\nEnter size of the array :");
scanf("%d",&s);

printf("\nEnter %d elements in to the array:");


for(i=0;i<s;i++)
scanf("%d",&a[i]);
for(i=0;i<s;i++)
{
for(j=i+1;j<s;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\nThe array after sorting is: ");
for(i=0;i<s;i++)
printf(" %d",a[i]);
getch();

}
44. INSERTION SORT
void main()
{
int i,j,s,temp,a[20];
clrscr();
printf("\nEnter size of the array: ");
scanf("%d",&s);
printf("\nEnter %d elements in to the array:",s);
for(i=0;i<s;i++)
scanf("%d",&a[i]);
for(i=1;i<s;i++)
{
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0))
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;

}
printf("\nAfter sorting the elements are: ");
for(i=0;i<s;i++)
printf(" %d",a[i]);
getch();

}
45. DISPLAY SOURCE CODE AS OUTPUT
#include"stdio.h"
void main()
{
FILE *p;
char ch;
clrscr();
p=fopen("raja.c","r");
while((ch=getc(p))!=-1)
putchar(ch);
fclose(p);
getch();
}
46. FACTORIAL OF A NUMBER USING RECURSION
void main()
{
int num,f;
clrscr();
printf("\nEnter a number: ");
scanf("%d",&num);
f=fact(num);
printf("\nFactorial of %d is: %d",num,f);
getch();
}
int fact(int n)
{
if(n==1)
return 1;
else
return(n*fact(n-1));
}
47. GCD OF A NUMBER USING RECURSION
void main()
{

int n1,n2,gcd;
clrscr();
printf("\nEnter two numbers: ");
scanf("%d %d",&n1,&n2);
gcd=findgcd(n1,n2);
printf("\nGCD of %d and %d is: %d",n1,n2,gcd);
getch();
int findgcd(int x,int y)

while(x!=y)
{
if(x>y)
else

return findgcd(x-y,y);
return findgcd(x,y-x);

}
return x;
}
48. SUM OF DIGITS OF A NUMBER USING RECURSION
void main()
{
int num,x;
clrscr();
printf("\nEnter a number: ");
scanf("%d",&num);
x=findsum(num);
printf("Sum of the digits of %d is: %d",num,x);
getch();
}
int r,s;
int findsum(int n)
{
if(n)
{
r=n%10;
s=s+r;
findsum(n/10);
}
else

return s;

}
49. POWER OF A NUMBER
void main()
{
int pow,num,i=1;
long int sum=1;
clrscr();
printf("\nEnter a number: ");
scanf("%d",&num);
printf("\nEnter power: ");
scanf("%d",&pow);
while(i<=pow)
{
sum=sum*num;
i++;
}
printf("\n%d to the power %d is:
%ld",num,pow,sum);
getch();
}
50. POWER OF A NUMBER USING RECURSION
void main()
{
int pow,num;
long int res;
long int power(int,int);
clrscr();
printf("\nEnter a number: ");
scanf("%d",&num);
printf("\nEnter power: ");
scanf("%d",&pow);
res=power(num,pow);
printf("\n%d to the power %d is:
%ld",num,pow,res);
getch();
}
int i=1;
long int sum=1;
long int power(int num,int pow)

if(i<=pow)
{
}
else

sum=sum*num;
power(num,pow-1);
return sum;

You might also like