Source Codes C / C++ / DS: Perfect Number
Source Codes C / C++ / DS: Perfect Number
Source Codes C / C++ / DS: Perfect Number
Source codes C /
C++ / DS
1. PERFECT
NUMBER.
void main()
{
int
n,i=1,sum=0
;
clrscr();
printf("\nEnt
er a
number:-");
scanf("%d",
&n);
while(i<n)
{
if(n
%i==0)
sum=sum+i;
i++;
}
User Login if(sum==n)
Cquest ID printf("\nThe
no %d is a
perfect
number",i);
else
Submit
printf("\nThe
no %d is not
a perfect
number",i);
getch();
}
2.
ARMSTRO
NG
NUMBER.
void
main()
{
int
num,r,su
m=0,temp
;
clrscr();
printf("\n
Enter a
number:-"
);
scanf("%
d",&num)
;
temp=nu
m;
while(nu
m!=0)
{
r=num
%10;
num=num
/10;
sum=sum
+(r*r*r);
}
if(sum==t
emp)
printf("\n
The
number
%d is an
armstrong
number",t
emp);
else
printf("\n
The
number
%d is not
an
armstrong
number",t
emp);
getch();
3. STRONG
NUMBER
void
main()
{
int
num,i,f,r,s
um=0,tem
p;
clrscr();
printf("\n
Enter a
number");
scanf("%
d",&num)
;
temp=nu
m;
while(nu
m)
{
i=1,f=1;
r=num
%10;
while(i<=
r)
{
f=f*i;
i++;
}
sum=sum
+f;
num=num
/10;
}
if(sum==t
emp)
printf("%
d is a
strong
number",t
emp);
else
printf("%
d is not a
strong
number",t
emp);
getch();
}
4. PRIME
NUMBER.
void
main()
{
int
num,i,cou
nt=0;
clrscr();
printf("\n
Enter 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("\n
Enter a
number:")
;
scanf("%
d",&num)
;
while(nu
m)
{
r=num
%10;
sum=sum
*10+r;
num=num
/10;
}
printf("\n
Reverse
number=
%d",sum)
;
getch();
}
6. SUM OF
THE
DIGITS OF
A NUMBER
void
main()
{
int
num,sum
=0,r;
clrscr();
printf("\n
Enter a
number:")
;
scanf("%
d",&num)
;
while(nu
m)
{
r=num
%10;
num=num
/10;
sum=sum
+r;
}
printf("su
m=
%d",sum)
;
getch();
}
7.
PALINDRO
ME
NUMBER.
void
main()
{
int
num,r,su
m=0,temp
;
clrscr();
printf("\n
Enter a
number:")
;
scanf("%
d",&num)
;
temp=nu
m;
while(nu
m)
{
r=num
%10;
num=num
/10;
sum=sum
*10+r;
}
if(temp==
sum)
printf("\n
%d is a
palindrom
e",temp);
else
printf("\n
%d is not
a
palindrom
e",temp);
getch();
}
8. G.C.D
OF TWO
NUMBERS
void
main()
{
int
n1,n2;
clrscr();
printf("\n
Enter two
numbers:"
);
scanf("%
d
%d",&n1,
&n2);
while(n1!
=n2)
{
if(n1>n2)
n1=n1-
n2;
else
n2=n2-
n1;
}
printf("\n
GCD=
%d",n1);
getch();
}
9. L.C.M OF
TWO
10. SWAP
TWO
VARIABLE
S
WITHOUT
USING
THIRD
VARIABLE
void
main()
{
int a,b;
clrscr();
printf("\n
Enter two
numbers:"
);
scanf("%
d
%d",&a,
&b);
printf("\n
Before
swapping
a=%d b=
%d",a,b);
a=a^b;
b=b^a;
a=a^b;
printf("\n
After
swapping
a=%d b=
%d",a,b);
getch();
}
11.
FLOYD’S
TRIANGLE
1
23
456
void main()
{
int i,j,r,k=1;
clrscr();
printf("\nEnt
er the
range:");
scanf("%d",
&r);
printf("\nFL
OYD'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("\n
Enter a
number:")
;
scanf("%
d",&num)
;
while(i<=
num)
{
k=0;
if(num
%i==0)
{
j=1;
while(j<=
i)
{
if(i
%j==0)
k++;
j++;
}
if(k==2)
printf("\n
%d is a
prime
factor",i);
}
i+
+;
}
getch();
}
13.
MULTIPLIC
ATION
TABLE
void main()
{
int r,i,j,k;
clrscr();
printf("\nEnt
er the
number
range:-");
scanf("%d",
&r);
for(i=1;i<=r;i
++)
{
for(j=1;j<=1
0;j++)
printf(" %d*
%d=
%d",i,j,i*j);
printf("\n");
}
getch();
}
14.
FACTORIA
L OF A
NUMBER
void
main()
{
int
i=1,f=1,n
um;
clrscr();
printf("\n
Enter a
number:")
;
scanf("%
d",&num)
;
while(i<=
num)
{
f=f*i;
i+
+;
}
printf("\n
Factorial
of %d is:
%d",num,
f);
getch();
}
15.
FIBONACC
I SERIES
void
main()
{
int
i=0,j=1,k
=2,r,f;
clrscr();
printf("En
ter the
number
range:");
scanf("%
d",&r);
printf("\n
FIBONA
CCI
SERIES:
");
printf("%
d
%d",i,j);
while(k<r
)
{
f=i+j;
i=j;
j=f;
printf("
%d",j);
k+
+;
}
getch();
}
16.
17.
CHECKING
LEAP
YEAR
void
main()
{
int year;
clrscr();
printf("En
ter 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.
CONVERSI
19.
CONVERSI
ON OF
BINARY TO
DECIMAL
void
main()
{
long int
no,n=0,j=
1,rem,no1
;
clrscr();
printf("En
ter any
number
any
binary
form->");
scanf("%l
d",&no);
no1=no;
while(no!
=0)
{
rem=no
%10;
n=n+rem
*j;
j=j*2;
no=no/10;
}
printf("\n
The value
of binary
no. %ld is
->
%ld",no1,
n);
getch();
}
20.
SWAPING
OF TWO
ARRAYS
void
main()
{
int
a[10],b[1
0],c[10],i;
clrscr();
printf("En
ter First
array->");
for(i=0;i<
10;i++)
scanf("%
d",&a[i]);
printf("\n
Enter
Second
array->");
for(i=0;i<
10;i++)
scanf("%
d",&b[i]);
printf("Ar
rays
before
swapping
");
printf("\n
First
array->");
for(i=0;i<
10;i++)
{
printf("%
d",a[i]);
}
printf("\n
Second
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("\n
Arrays
after
swapping
");
printf("\n
First
array->");
for(i=0;i<
10;i++)
{
printf("%
d",a[i]);
}
printf("\n
Second
array->");
for(i=0;i<
10;i++)
{
printf("%
d",b[i]);
}
getch();
}
21.
FINDING
NCR
FACTOR
void
main()
{
int
n,r,ncr;
clrscr();
printf("En
ter any
two
numbers-
>");
scanf("%
d
%d",&n,
&r);
ncr=fact(
n)/(fact(r)
*fact(n-
r));
printf("Th
e NCR
factor of
%d and
%d is
%d",n,r,n
cr);
getch();
}
int
fact(int n)
{
int i=1;
while(n!
=0)
{
i=i*n;
n--;
}
return i;
}
22.
PASCAL’S
TRIANGLE
void
main()
{
int
line,i,j,k;
clrscr();
printf("En
ter 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.
CONVERSI
ON FROM
UPPERCA
SE TO
LOWER
CASE
void
main()
{
char
str[20];
int i;
clrscr();
printf("En
ter any
string-
>");
scanf("%s
",str);
printf("Th
e string
is->
%s",str);
for(i=0;i<
=strlen(str
);i++)
{
if(str[i]>=
65&&str[
i]<=90)
str[i]=str[i
]+32;
}
printf("\n
The string
in
uppercase
is->
%s",str);
getch();
}
24.
CONVERSI
ON FROM
LOWER
CASE TO
UPPER
CASE
void
main()
{
char
str[20];
int i;
clrscr();
printf("En
ter any
string-
>");
scanf("%s
",str);
printf("Th
e string
is->
%s",str);
for(i=0;i<
=strlen(str
);i++)
{
if(str[i]>=
97&&str[
i]<=122)
str[i]=str[i
]-32;
}
printf("\n
The string
in
lowercase
is->
%s",str);
getch();
}
25.
DELETE
THE
VOWELS
FROM A
STRING
void
main()
{
char
str[20],s[2
0];
int i,j=0;
clrscr();
printf("En
ter any
string-
>");
scanf("%s
",str);
printf("Th
e 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("\n
The 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("En
ter the
First
matrix-
>");
for(i=0;i<
3;i++)
for(j=0;j<
3;j++)
scanf("%
d",&a[i]
[j]);
printf("\n
Enter the
Second
matrix-
>");
for(i=0;i<
3;i++)
for(j=0;j<
3;j++)
scanf("%
d",&b[i]
[j]);
printf("\n
The 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("\n
The
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("\n
The
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();
}
28. COPY
DATA
FROM ONE
FILE TO
ANOTHER
FILE
#include"
stdio.h"
void
main()
{
FILE
*p,*q;
char
file1[20],f
ile2[20];
char ch;
clrscr();
printf("\n
Enter the
source
file name
to be
copied:");
gets(file1)
;
p=fopen(f
ile1,"r");
if(p==NU
LL)
{
printf("ca
nnot open
%s",file1)
;
exit(0);
}
printf("\n
Enter the
destinatio
n file
name:");
gets(file2)
;
q=fopen(f
ile2,"w");
if(q==NU
LL)
{
printf("ca
nnot open
%s",file2)
;
exit(0);
}
while((ch
=getc(p))!
=EOF)
putc(ch,q)
;
printf("\n
COMPLE
TED");
fclose(p);
fclose(q);
getch();
}
30. SUM
OF THE
SERIES
1+2+3+------
---+n
void
main()
{
int r;
clrscr();
printf("\n
Enter the
number
range: ");
scanf("%
d",&r);
printf("\n
Sum 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("\n
Enter the
range: ");
scanf("%l
d",&r);
printf("\n
Sum 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("\n
Enter the
number
range: ");
scanf("%
d",&r);
printf("\n
Sum 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("\n
Enter the
size of the
array: ");
scanf("%
d",&size);
printf("\n
Enter %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("\n
Biggest:
%d",big);
getch();
}
34.
SECOND
LARGEST
NUMBER
IN AN
UNSORTE
D ARRAY
main()
{
int
un[10], i,
big1,
big2;
printf("En
ter 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("Se
cond
largest:
%d\n",
big2);
return 0;
}
35.
SECOND
SMALLEST
NUMBER
IN AN
UNSORTE
D ARRAY
main()
{
int
un[10], i,
s1, s2;
clrscr();
printf("En
ter 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("\n
Second
smallest:
%d", s2);
return 0;
}
36.
MULTIPLIC
ATION OF
MATRICES
void
main()
{
int a[5]
[5],b[5]
[5],c[5]
[5],i,j,k,su
m=0,m,n,
o,p;
clrscr();
printf("\n
Enter the
row and
column of
first
matrix");
scanf("%
d
%d",&m,
&n);
printf("\n
Enter the
row and
column of
second
matrix");
scanf("%
d
%d",&o,
&p);
if(n!=o)
{
printf("M
atrix
mutiplicat
ion is not
possible")
;
printf("\n
Column
of first
matrix
must be
same as
row of
second
matrix");
}
else
{
printf("\n
Enter the
First
matrix-
>");
for(i=0;i<
m;i++)
for(j=0;j<
n;j++)
scanf("%
d",&a[i]
[j]);
printf("\n
Enter the
Second
matrix-
>");
for(i=0;i<
o;i++)
for(j=0;j<
p;j++)
scanf("%
d",&b[i]
[j]);
printf("\n
The 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("\n
The
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+
+)//colum
n of
second
matrix
{
sum=0;
for(k=0;k
<n;k++)
sum=sum
+a[i]
[k]*b[k]
[j];
c[i]
[j]=sum;
}
}
}
printf("\n
The
multiplica
tion of
two
matrix
is\n");
for(i=0;i<
m;i++)
{
printf("\n"
);
for(j=0;j<
p;j++)
{
printf("%
d\t",c[i]
[j]);
}
}
getch();
}
37. SUM
OF
DIAGONAL
ELEMENTS
OF A
MATRIX
void
main()
{
int a[10]
[10],i,j,su
m=0,m,n;
clrscr();
printf("\n
Enter the
row and
column of
matrix");
scanf("%
d
%d",&m,
&n);
printf("\n
Enter the
First
matrix-
>");
for(i=0;i<
m;i++)
for(j=0;j<
n;j++)
scanf("%
d",&a[i]
[j]);
printf("\n
The
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("\n
Enter the
row and
column of
matrix");
scanf("%
d
%d",&m,
&n);
printf("\n
Enter the
First
matrix-
>");
for(i=0;i<
m;i++)
for(j=0;j<
n;j++)
scanf("%
d",&a[i]
[j]);
printf("\n
The
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++)
{
printf("%
d\t",b[i]
[j]);
}
}
getch();
}
39.
CONVERSI
ON OF
FAREHNIT
E TO
CENTIGRA
DE
void
main()
{
float c,f;
clrscr();
printf("En
ter temp.
in
farehnite"
);
scanf("%f
",&f);
c=(5*(f-
32))/9;//F
ormula
for
conversio
n
printf("Th
e temp. in
centigrad
e is->
%f",c);
getch();
}
40.
COUNTING
DIFFEREN
T
CHARACT
ERS IN A
STRING
main()
{
int
a[26],A[2
6],i,c=0;
char
str[100];
clrscr();
puts("Ent
er 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)
printf("\n
%c occurs
%d
times",i+
97,a[i]);
}
for(i=0;i<
26;i++)
{
if(A[i]!
=0)
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("Ent
er 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(te
mp,str[i]);
strcpy(str[
i],str[j]);
strcpy(str[
j],temp);
}
}
printf("Th
e 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("\n
Enter size
of the
array: ");
scanf("%
d",&s);
printf("\n
Enter %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]=te
mp;
}
}
}
printf("\n
The array
after
sorting is:
");
for(i=0;i<
s;i++)
printf("
%d",a[i]);
getch();
}
43.
SELECTIO
N SORT
void
main()
{
int
s,i,j,temp,
a[20];
clrscr();
printf("\n
Enter size
of the
array :");
scanf("%
d",&s);
printf("\n
Enter %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("\n
The 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("\n
Enter size
of the
array: ");
scanf("%
d",&s);
printf("\n
Enter %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((te
mp<a[j])
&&(j>=0)
)
{
a[j+1]=a[j
];
j=j-1;
}
a[j+1]=te
mp;
}
printf("\n
After
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(c
h);
fclose(p);
getch();
}
46.
FACTORIA
L OF A
NUMBER
USING
RECURSIO
N
void
main()
{
int
num,f;
clrscr();
printf("\n
Enter a
number:
");
scanf("%
d",&num)
;
f=fact(nu
m);
printf("\n
Factorial
of %d is:
%d",num,
f);
getch();
}
int
fact(int n)
{
if(n==1)
return 1;
else
return(n*f
act(n-1));
}
47. GCD
OF A
NUMBER
USING
RECURSIO
N
void
main()
{
int
n1,n2,gcd
;
clrscr();
printf("\n
Enter two
numbers:
");
scanf("%
d
%d",&n1,
&n2);
gcd=findg
cd(n1,n2);
printf("\n
GCD of
%d and
%d is:
%d",n1,n
2,gcd);
getch();
}
int
findgcd(i
nt x,int y)
{
while(x!
=y)
{
if(x>y)
return
findgcd(x
-y,y);
else
return
findgcd(x,
y-x);
}
return x;
}
48. SUM
49. POWER
OF A
NUMBER
void
main()
{
int
pow,num,
i=1;
long int
sum=1;
clrscr();
printf("\n
Enter a
number:
");
scanf("%
d",&num)
;
printf("\n
Enter
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
RECURSIO
N
void
main()
{
int
pow,num;
long int
res;
long int
power(int,
int);
clrscr();
printf("\n
Enter a
number:
");
scanf("%
d",&num)
;
printf("\n
Enter
power: ");
scanf("%
d",&pow)
;
res=powe
r(num,po
w);
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
)
{
sum=sum
*num;
power(nu
m,pow-
1);
}
else
return
sum;
}
52.
CONCATE
NATION OF
TWO
STRINGS
void
main()
{
int
i=0,j=0;
char
str1[20],st
r2[20];
clrscr();
puts("Ent
er first
string");
gets(str1);
puts("Ent
er second
string");
gets(str2);
printf("Be
fore
concatena
tion the
strings
are\n");
puts(str1);
puts(str2);
while(str1
[i]!='\0')
{
i+
+;
}
while(str2
[j]!='\0')
{
str1[i+
+]=str2[j+
+];
}
str1[i]='\0'
;
printf("Af
ter
concatena
tion the
strings
are\n");
puts(str1);
getch();
}
53.
CONCATE
NATION OF
TWO
STRINGS
USING
POINTER
void
main()
{
int
i=0,j=0;
char
*str1,*str
2,*str3;
clrscr();
puts("Ent
er first
string");
gets(str1);
puts("Ent
er second
string");
gets(str2);
printf("Be
fore
concatena
tion the
strings
are\n");
puts(str1);
puts(str2);
while(*str
1)
{
str3[i+
+]=*str1+
+;
}
while(*str
2)
{
str3[i+
+]=*str2+
+;
}
str3[i]='\0'
;
printf("Af
ter
concatena
tion the
strings
are\n");
puts(str3);
getch();
}
54.
ACROMATI
C
STRING(PR
INTING
INITIALS)
void
main()
{
char
str[20];
int i=0;
clrscr();
puts("Ent
er a
string");
gets(str);
printf("%
c",*str);
while(str[
i]!='\0')
{
if(str[i]==
' ')
{
i++;
printf("%
c",*(str+i)
);
}
i+
+;
}
getch();
}
55. LINEAR
SEARCH
void
main()
{
int
a[10],i,n,
m,c=0;
clrscr();
printf("En
ter the
size of an
array->");
scanf("%
d",&n);
printf("\n
Enter the
elements
of the
array->");
for(i=0;i<
n;i++)
{
scanf("%
d",&a[i]);
}
printf("\n
The
elements
of an
array are-
>");
for(i=0;i<
n;i++)
{
printf("
%d",a[i]);
}
printf("\n
Enter the
number to
be search-
>");
scanf("%
d",&m);
for(i=0;i<
n;i++)
{
if(a[i]==
m)
{
c=1;
break;
}
}
if(c==0)
printf("\n
The
number is
not in the
list");
else
printf("\n
The
number is
found");
getch();
}
56.
BINARY
SEARCH
void
main()
{
int
a[10],i,n,
m,c=0,l,u,
mid;
clrscr();
printf("En
ter the
size of an
array->");
scanf("%
d",&n);
printf("\n
Enter the
elements
of the
array->");
for(i=0;i<
n;i++)
{
scanf("%
d",&a[i]);
}
printf("\n
The
elements
of an
array are-
>");
for(i=0;i<
n;i++)
{
printf("
%d",a[i]);
}
printf("\n
Enter the
number to
be search-
>");
scanf("%
d",&m);
l=0,u=n-
1;
while(l<=
u)
{
mid=(l+u)
/2;
if(m==a[
mid])
{
c=1;
break;
}
else
if(m<a[mi
d])
{
u=mid-1;
}
else
l=mid+1;
}
if(c==0)
printf("\n
The
number is
not in the
list");
else
printf("\n
The
number is
found");
getch();
}
57. BINARY
SEARCH
THROUGH
RECURSSI
ON
void
main()
{
int
a[10],i,n,
m,c,l,u;
clrscr();
printf("En
ter the
size of an
array->");
scanf("%
d",&n);
printf("\n
Enter the
elements
of the
array->");
for(i=0;i
<n;i++)
{
scanf("%
d",&a[i]);
}
printf("\n
The
elements
of an
array are-
>");
for(i=0;i<
n;i++)
{
printf("
%d",a[i]);
}
printf("\n
Enter the
number to
be search-
>");
scanf("%
d",&m);
l=0,u=n-
1;
c=binary(
a,n,m,l,u);
if(c==0)
printf("\n
The
number is
not in the
list");
else
printf("\n
The
number is
found");
getch();
}
int
binary(int
a[],int
n,int m,int
l,int u)
{
int
mid,c=0;
if(l<=u)
{
mid=(l+u)
/2;
if(m==a[
mid])
{
c=1;
}
else
if(m<a[mi
d])
{
return
binary(a,n
,m,l,mid-
1);
}
else
return
binary(a,n
,m,mid+1,
u);
}
else
return c;
}
58.
REMOVE
DUPLICAT
E
ELEMENTS
IN AN
ARRAY
void
main()
{
int
arr[50];
int *p;
int
i,j,k,size,n
;
clrscr();
printf("\n
Enter size
of the
array: ");
scanf("%
d",&n);
printf("\n
Enter %d
elements
into the
array:
",n);
for(i=0;i<
n;i++)
scanf("%
d",&arr[i]
);
size=n;
p=arr;
for(i=0;i<
size;i++)
{
for(j=0;j<
size;j++)
{
if(i==j)
{
continue;
}
else
if(*(p+i)=
=*(p+j))
{
k=j;
size--;
while(k
< size)
{
*(p+k)=*(
p+k+1);
k++;
}
j=0;
}
}
}
printf("\n
The array
after
removing
duplicates
is: ");
for(i=0;i
< size;i+
+)
{
printf("
%d",arr[i]
);
}
getch();
}
59.
REVERSE
A NUMBER
USING
RECURSIO
N
void
main()
{
int
num,rev;
clrscr();
printf("\n
Enter a
number :"
);
scanf("%
d",&num)
;
rev=rever
se(num);
printf("\n
After
reverse
the no is :
%d",rev);
getch();
}
int
sum=0,r;
reverse(in
t num)
{
if(num)
{
r=num
%10;
sum=sum
*10+r;
reverse(n
um/10);
}
else
return
sum;
}
60.
CONVERSI
ON FROM
DECIMAL
TO OCTAL
void
main()
{
int
i=0,j=0,re
m=0,a[10
],b[10];
long int
num;
clrscr();
printf("\n
Enter a
number :"
);
scanf("%l
d",&num)
;
while(nu
m)
{
if(num<8)
{
a[j+
+]=num;
break;
}
else
{
a[j+
+]=num
%8;
num=num
/8;
}
}
for(i=j-
1;i>=0;i--
)
b[rem+
+]=a[i];
printf("\n
Octal
equivalen
t :");
for(j=0;j<
rem;j++)
printf("%
d",b[j]);
getch();
}
61. QUICK
SORT
void
main()
{
int
x[20],size
,i;
clrscr();
printf("\n
Enter size
of the
array :");
scanf("%
d",&size);
printf("\n
Enter %d
elements :
",size);
for(i=0;i<
size;i++)
scanf("%
d",&x[i]);
quicksort(
x,0,size-
1);
printf("\n
Sorted
elements :
");
for(i=0;i<
size;i++)
printf("
%d",x[i]);
getch();
}
quicksort(
int
x[10],int
first,int
last)
{
int
pivot,j,te
mp,i;
if(first<la
st)
{
pivot=firs
t;
i=first;
j=last;
while(i<j)
{
while(x[i]
<=x[pivot
]&&i<last
)
i++;
while(x[j]
>x[pivot])
j--;
if(i<j)
{
temp=x[i]
;
x[i]=x[j];
x[j]=temp
;
}
}
temp=x[p
ivot];
x[pivot]=
x[j];
x[j]=temp
;
quicksort(
x,first,j-
1);
quicksort(
x,j+1,last)
;
}
}
62.
DELETE
ELEMENT
FROM AN
ARRAY AT
DESIRED
POSITION
void
main()
{
int
a[50],i,po
s,size;
clrscr();
printf("\n
Enter size
of the
array: ");
scanf("%
d",&size);
printf("\n
Enter %d
elements
in to the
array:
",size);
for(i=0;i<
size;i++)
scanf("%
d",&a[i]);
printf("\n
Enter
position
where to
delete: ");
scanf("%
d",&pos);
i=0;
while(i!
=pos-1)
i+
+;
while(i<1
0)
{
a[i]=a[i+1
];
i+
+;
}
size--;
for(i=0;i<
size;i++)
printf("
%d",a[i]);
getch();
}
63. INSERT
AN
ELMENT IN
AN ARRAY
AT
DESIRED
POSITION
void
main()
{
int
a[50],size
,num,i,po
s,temp;
clrscr();
printf("\n
Enter size
of the
array: ");
scanf("%
d",&size);
printf("\n
Enter %d
elements
in to the
array:
",size);
for(i=0;i<
size;i++)
scanf("%
d",&a[i]);
printf("\n
Enter
position
and
number to
insert: ");
scanf("%
d
%d",&po
s,&num);
i=0;
while(i!
=pos-1)
i+
+;
temp=size
++;
while(i<te
mp)
{
a[temp]=a
[temp-1];
temp--;
}
a[i]=num;
for(i=0;i<
size;i++)
printf("
%d",a[i]);
getch();
}
64.
GREATEST
AMONG 3
NUMBERS
USING
BINARY
MINUS
void
main()
{
int a,b,c;
clrscr();
printf("\n
Enter 3
numbers:
");
scanf("%
d %d
%d",&a,
&b,&c);
if(a-b>0
&& a-
c>0)
printf("\n
Greatest
is a :
%d",a);
else
if(b-c>0)
printf("\n
Greatest
is b :
%d",b);
else
printf("\n
Greatest
is c :
%d",c);
getch();
}
65. A 5
DIGIT
NUMBER
IS INPUT
THROUGH
THE KEY
BOARD.TH
E OUTPUT
IS A NEW
NUMBER
ADDING 1
TO EACH
OF ITS
DIGITS.
[EXAMPLE:
Input-
12395
Output-
23406]
void
main()
{
long int
num;
int
add(long
int);
clrscr();
printf("\n
Enter a 5
digit
number:
");
scanf("%l
d",&num)
;
add(num)
;
getch();
}
add(long
int num)
{
long int
r;
if(num)
{
r=num
%10;
r=r+1;
if(r==10)
r=0;
add(num/
10);
printf("%
d",r);
}
else
return;
}
66.
GREATEST
AMONG 3
NUMBERS
USING
CONDITIO
NAL
OPERATO
R
void
main()
{
int
a,b,c,big;
clrscr();
printf("\n
Enter 3
numbers:"
);
scanf("%
d %d
%d",&a,
&b,&c);
big=(a>b
&&a>c?
a:b>c?
b:c);
printf("\n
The
biggest
number
is:
%d",big);
getch();
}
67.
PROGRAM
68. PRINT
GIVEN
STRING
FROM
FIRST
OCCURRE
NCE OF
GIVEN
CHARACT
ER
#include"
string.h"
#include"
stdio.h"
void
main()
{
char *p;
char
s[20],s1[1
];
clrscr();
printf("\n
Enter a
string: ");
scanf("%
[^\n]",s);
fflush(stdi
n);
printf("\n
Enter
character:
");
gets(s1);
p=strpbrk
(s,s1);
printf("\n
The string
from the
given
character
is:
%s",p);
getch();
}
69. COUNT
THE
NUMBER
OF
OCCURRE
NCES OF
ANY TWO
VOWELS
IN
SUCCESSI
ON IN A
LINE OF
TEXT.
#include
"stdio.h"
int
isvowel(c
har chk);
int main()
{
char
text[1000
], chk;
int
count;
count =
0;
while( (te
xt[count]
=
getchar())
!= '\n' )
count++;
text[count
] = '\0';
count =
0;
while
( (chk =
text[count
]) != '\0' )
{
if
( isvowel(
chk) )
{
if
( (chk =
text[+
+count])
&&
isvowel(c
hk) )
{
putchar(te
xt[count
-1]);
putchar(te
xt[count])
;
putchar('\
n');
}
}
else
+
+count;
}
return 0;
}
int
isvowel(c
har chk)
{
if ( chk
== 'a' ||
chk == 'e'
|| chk ==
'i' || chk
== 'o' ||
chk == 'u'
)
return 1;
return 0;
}
70. PRINT
PRIME
NUMBERS
BETWEEN
1-300
USING
BREAK
AND
CONTINUE
#include
"math.h"
#include
"stdio.h"
main()
{
int i, j;
i = 1;
while ( i
< 300 )
{
j =
2;
while ( j <
sqrt(i) )
{
if ( i
% j == 0 )
break;
else
{
++j;
continue;
}
}
if ( j >
sqrt(i) )
printf("%
d\t", i);
++i;
}
return 0;
}
71.
CALCULAT
E AREA OF
A CIRCLE
#include
"stdio.h"
#define PI
3.141
int main()
{
float r, a;
printf("Ra
dius: ");
scanf("%f
", &r);
a = PI *
r * r;
printf("%f
\n", a);
return 0;
}
72.
AMICABLE
PAIRS
[Two
numbers
are said to
be
amicable if
sum of
proper
divisors of
1st is equal
to the 2nd
and vice
versa. It is
assumed
that these
two
numbers
shoud be
distinct,
e.g. 3 is
equal to
the sum of
its proper
divisors 1
and 2 but
can not
form an
amicable
pair.]
#include
"stdio.h"
int main()
{
long int
range,
test, chk,
div, sum,
n1, n2;
printf("In
put range
to check
amicable
numbers:
");
scanf("%l
d",
&range);
test = 0;
while ( +
+test <
range )
{
sum = div
= 0;
while ( +
+div <=
test/2 )
{
if
( test %
div == 0 )
sum
+= div;
}
chk =
sum;
sum = div
= 0;
while ( +
+div <=
chk/2 )
{
if
( chk %
div == 0 )
sum
+= div;
}
if
( sum ==
test )
{
if
( test ==
chk )
continue;
n1 =
test;
if
( n1 ==
n2)
continue;
n2 =
chk;
printf("%
d\t%d\n",
n1, n2);
}
}
return 0;
}
73.
PASSING
ONE-
DIMENSIO
NAL
ARRAY TO
A
FUNCTION
#include
"stdio.h"
#define N
5
void
fstore1D(i
nt a[], int
a_size);
void
fretrieve1
D(int a[],
int
a_size);
void
fedit1D(i
nt a[], int
a_size);
int main()
{
int a[N];
printf("In
put data
into the
matrix:\n"
);
fstore1D(
a, N);
fretrieve1
D(a, N);
fedit1D(a,
N);
fretrieve1
D(a, N);
return 0;
}
void
fstore1D(i
nt a[], int
n)
{
int i;
for ( i =
0; i < n; +
+i )
scanf("%
d",
&a[i]);
}
void
fretrieve1
D(int a[],
int n)
{
int i;
for ( i =
0; i < n; +
+i )
printf("%
6d ",
a[i]);
printf("\n"
);
}
void
fedit1D(i
nt a[], int
n)
{
int i, q;
for ( i =
0; i < n; +
+i )
{
printf("Pr
ev. data:
%d\nEnte
r 1 to edit
0 to
skip.",
a[i]);
scanf("%
d", &q);
if
( q == 1 )
{
printf("En
ter new
value: ");
scanf("%
d",
&a[i]);
}
}
}
74.
PASSING
2-
DIMENSIO
NAL
ARRAY TO
A
FUNCTION
#include
"stdio.h"
#define M
3
#define N
5
void
fstore2D(i
nt a[][N]);
void
fretrieve2
D(int a[]
[N]);
int main()
{
int a[M]
[N];
printf("In
put data
in matrix
(%d X
%d)\n",
M, N);
fstore2D(
a);
fretrieve2
D(a);
return 0;
}
void
fstore2D(i
nt a[][N])
{
int i, j;
for ( i =
0; i < M;
++i )
{
for ( j = 0;
j < N; ++j
)
scanf("%
d", &a[i]
[j]);
}
}
void
fretrieve2
D(int a[]
[N])
{
int i, j;
for ( i =
0; i < M;
++i )
{
for ( j = 0;
j < N; ++j
)
printf("%
6d ", a[i]
[j]);
printf("\n"
);
}
}
75. DRAW
THE
FOLLOWIN
G
PYRAMID
1
0 1
1 0 1
0 1 0
1
void main()
{
int i,j;
clrscr();
for(i=1;i<10;
i++)
{
for(j=i;j>=1;j
--)
{
printf("%d",j
%2);
}
printf("\n");
}
getch();
}
76.
CREATE A
FILE AND
STORE
DATA IN IT
#include"st
dio.h"
void main()
{
FILE *fp;
char ch;
clrscr();
fp=fopen("fil
e.txt","w");
printf("\nEnt
er data to
be stored in
to the file:");
while((ch=g
etchar())!
=EOF)
putc(ch,fp);
fclose(fp);
getch();
}
77.
WRITING
OF
STRINGS
TO A FILE
#include"std
io.h"
void main()
{
FILE *p;
char
str[70];
if((p=fopen("
string.txt","w
"))==NULL)
{
printf("\nUn
able to open
file
string.txt");
exit(1);
}
else
printf("\nEnt
er a set of
strings,Pres
s just enter
key to
finish\n: ");
while(strlen(
gets(str))>0)
{
fputs(str,p);
fputs("\n",p);
}
fclose(p);
getch();
}
78.
READING
OF
STRINGS
FROM A
FILE
#include"std
io.h"
void main()
{
char
str[70];
FILE *p;
clrscr();
if((p=fopen("
string.txt","r"
))==NULL)
{
printf("\nUn
able t open
file
string.txt");
exit(1);
}
while(fgets(
str,70,p)!
=NULL)
puts(str);
fclose(p);
}
79.
WRITING
OF ENTIRE
ARRAY TO
A FILE
#include"std
io.h"
void main()
{
FILE *p;
int i,a[10];
if((p=fopen("
myfile.dat","
wb"))==NUL
L)
{
printf("\nUn
able to open
file
myfile.dat");
exit(1);
}
printf("\nEnt
er ten
values, one
value on
each
line\n");
for(i=0;i<10;
i++)
scanf("%d",
&a[i]);
fwrite(a,size
of(a),1,p);
fclose(p);
getch();
}
80.
CONCATE
NATE
MANY
FILES AND
STORE
THEM IN A
FILE
NAMED
FILES.
[Use
command
line
arguments
to pass the
file names]
#include"st
dio.h"
void
concatenate
(FILE
*fp1,FILE
*fp2,char
*argv[],int
argc);
void
main(int
argc,char
*argv[])
{
FILE
*fp1,*fp2;
concatenate
(fp1,fp2,arg
v,argc);
getch();
}
void
concatenate
(FILE
*fp1,FILE
*fp2,char
**argv,int
argc)
{
int i,ch;
fp2=fopen("f
iles","a");
for(i=1;i<arg
c-1;i++)
{
fp1=fopen(a
rgv[i],"r");
while((ch=g
etc(fp1))!
=EOF)
putc(ch,fp2)
;
}
}
81. COUNT
NO OF
OCCURRE
NCES OF A
CHARACT
ER IN A
STRING
USING
getchar()
AND
putchar().
#include"std
io.h"
main()
{
char
name[20];
int
i,count=0;
clrscr();
for(i=0;i<20
;i++)
{
name[i]=get
char();
putchar(na
me[i]);
if(name[i]=='
\n')
break;
else
if(name[i]=='
a')
count++;
}
printf("\nTh
e number of
a's in
name :
%d",count);
getch();
return 0;
}
For registration and
other queries please
contact us at: 0674-
6539680
Email at :
cquest@lakshyatrain
ing.org
© Reserved to www.lakshyatraining.org