Sie sind auf Seite 1von 124

Programming

lab(C)

RCC
INSTITUTE
OF
INFORMATION
TECHNOLOGY

ASSIGNMENTS

ON

PROGRAMMING
LAB(C)

DONE BY

HAREKRISHNA
PAL
ROLL:
06

DEPERTMENT: MCA
YEAR: FIRST
SEMESTER: 1st
GROUP: 1

PAPER CODE: MCA193

1 Created
by
Krishna
Pal
Programming
lab(C)

CONTENTS

Sl.
No.
Assignment
Names
Date
of
Commencement
Date
of
Submission
Teacher�s
Signature
1.
Enter radius of a circle and find the
area and circumference.
___.___.2010 ___.___.2010
2. Find the largest of three numbers. ___.___.2010 ___.___.2010
3.
Enter a number and check whether it is
odd or even.
___.___.2010 ___.___.2010
4.
Write a programme to find the roots of a
Quadratic Equation. ___.___.2010 ___.___.2010
5.
Write a programme to find the sum of
natural numbers from 1 to a given
numbers.
___.___.2010 ___.___.2010
6.
Write a programme to find the factorial of
a number.
___.___.2010 ___.___.2010
7.
Write a programme to print the prime
numbers from 1 to a given numbers.
___.___.2010 ___.___.2010
8.
Write a programme to print the perfect
numbers from 1 to a given numbers. ___.___.2010 ___.___.2010
9.
Write a programme to print the Fibonacci
series: 1,1,2,3,5,8,13,..........N ___.___.2010 ___.___.2010
10.
Write a programme to enter some
numbers and find how many of them are
odd/even and find the average of odd
and even numbers.
___.___.2010 ___.___.2010
11.
Enter some numbers and find the
maximum and minimum without array.
___.___.2010 ___.___.2010
12
Write a programme to find the L.C.M.
and H.C.F of two given numbers.
___.___.2010 ___.___.2010
13.
Write a programme to find the GCD
using iteration and recursions function. ___.___.2010 ___.___.2010
14.
Write a programme to accept a number
and check whether it is Armstrong or not.
___.___.2010 ___.___.2010
15.
To find the sum of the digits of a given
number.
___.___.2010 ___.___.2010
16.
To print the palindrome numbers from
100 to 1000.
___.___.2010 ___.___.2010
17. S= 1 + X + X^3 + X^5 + .............N. ___.___.2010 ___.___.2010
18.
S= 1 + (1 + 2) + (1 + 2 + 3) + .........N.
___.___.2010 ___.___.2010
19.
S= 1+ 2/2! + 3/3! + 4/4! +......N.
___.___.2010 ___.___.2010
20. S= 1 + X + X^2 + X^3/3! + X^4/4! +.......N ___.___.2010 ___.___.2010

2 Created
by
Krishna
Pal
Programming
lab(C)

Sl.
No.
Assignment
Names
Date
of
Commencement
Date
of
Submission
Teacher�s
Signature
21. Sin X= X -X^3/3! + X^5/5! -X^7/7! +..N ___.___.2010 ___.___.2010
22.
Enter some numbers and store them in
an array, then enter a number and check
whether it exists in the array or not.
___.___.2010 ___.___.2010
23.
Enter some numbers and store them in
an array, now create two array of odd
and even numbers.
___.___.2010 ___.___.2010
24.
Write a programme to change a Decimal
numbers to Binary numbers.
___.___.2010 ___.___.2010
25.
Write a programme to change decimal
numbers to Octal and Hexadecimals.
___.___.2010 ___.___.2010
26.
Write a programme to change Binary
numbers to Decimals.
___.___.2010 ___.___.2010
27.
Write a programme to enter elements in
a 2D Array and display in Matrix from.
___.___.2010 ___.___.2010
28.
Write a programme to sort some
numbers using bubble sort and Selection
sort separately.
___.___.2010 ___.___.2010
29
Write a programme to find the sum of
each row in a 2D Array.
___.___.2010 ___.___.2010
30.
Write a programme to find the sum of
each column of a 2D Array.
___.___.2010 ___.___.2010
31.
Write a programme to find the sum of the
diagonals of a 2D Array.
___.___.2010 ___.___.2010
32.
Write a programme to find the sum and
multiplication of two matrices.
___.___.2010 ___.___.2010
33.
Write a programme to find the transpose
of a matrix.
___.___.2010 ___.___.2010
34.
Write a programme to accept a sentence
and find the number of words.
___.___.2010 ___.___.2010
35.
Write a programme to accept a word and
count the number of vowels.
___.___.2010 ___.___.2010
36.
Write a programme to accept a sentence
and find number of vowels, consonants,
numbers, spaces and special characters.
___.___.2010 ___.___.2010
37.
Write a programme to accept a word and
find the sum of its ASCII values.
___.___.2010 ___.___.2010
38.
Write a programme to accept a word and
print it in reverse forms.
___.___.2010 ___.___.2010
39.
Write a programme to accept a word and
check whether it is Palindrome or not.
___.___.2010 ___.___.2010
40.
Write a programme to accept a sentence
and find the occurrence of each
character.
___.___.2010 ___.___.2010

3 Created
by
Krishna
Pal
Programming
lab(C)

Sl.
No.
Assignment
Names
Date
of
Commencement
Date
of
Submission
Teacher�s
Signature
41.
Write a programme to accept a name of
person and print the 1st letter of the first
and middles (if any) and the title as it is in
examples: Raja Ram Das .
R. R. Das or
Rahul Ray .
R. Ray.
___.___.2010 ___.___.2010
42.
Write a programme to accept a string as
a command lone argument and then find
its length.
___.___.2010 ___.___.2010
43.
Write a programme to accept text from
keyboard and store them in a file and
then display the content of the saved file.
___.___.2010 ___.___.2010
44.
Write a programme to accept 10
students� names and marks of three
subjects then calculate the total marks
and store them in a file.
___.___.2010 ___.___.2010

4 Created
by
Krishna
Pal
Programming
lab(C)

.
1.
Enter
radius
of
a
circle
and
find
the
area
and
circumference.
.
####Definition : Given the radius of a circle, the area and circumference can be
calculated
using the formula. Area=pi*r*r and circumference=2*pi*r where, r is the radius of
the circle,
pi is approximately 3.142.
.
####Flow Chart :
INPUT r
Define
PI=3.142
a=PI*r*r
c=2*PI*r
PRINT a
PRINT c
STOP
START
5 Created
by
Krishna
Pal
Programming
lab(C)

.
####Programme :
/*Directives to include standard library and standard input-output header files in
this
program*/

#include<stdio.h>
#include<conio.h>

#define PI 3.142 /* define value of PI */

void main() /*Main function started here*/

{
float r,a,c; /*Declaration of variable*/
clrscr(); /*Clear the console screen*/

printf("\nEnter the radius of circle:\t");


scanf("%f",&r);
a=PI*r*r; /*Calculating area */
printf("\nThe area of the circle:\t%f",a);
c=2*PI*r; /* Calculating circumference */
printf("\nThe circumference of the circle: %f",c);
getch();

.
####Output :
Enter the radius of circle: 2

The area of the circle: 12.568000


The circumference of the circle: 12.568000

Enter the radius of circle: 1

The area of the circle: 3.142000


The circumference of the circle: 6.284000

6 Created
by
Krishna
Pal
Programming
lab(C)

.
2.
Find
the
largest
of
three
numbers.
.
####Definition : Given three values, checking and print the number which is largest
among
them.
.
####Flow Chart :
No No
Yes Yes
INPUT a, b, c
PRINT Max
STOP
START
Is
a>b and
a>c
Is
b>a and
b>c
Max=a Max=b Max=c
7 Created
by
Krishna
Pal
Programming
lab(C)

.
####Programme :
/*Directives to include standard library and standard input-output header files in
this
program*/

#include<stdio.h>
#include<conio.h>

void main() /* Main function started here */

{
int a,b,c,max; /* Declaration of variable */
clrscr(); /* Clear the console screen */
printf("\nEnter three numbers:\t");
scanf("%d%d%d",&a,&b,&c); /* read data from keyboard */
max=((a>b && a>c)?a:((b>a && b>c)?b:c)); /* calculating maximum */
printf("\nThe largest number is:\t%d",max); /* Printing maximum */
getch();

.
####Output :
Enter three numbers: 5
8
0

The largest number is: 8

Enter three numbers: -2


-5
-10

The largest number is: -2

8 Created
by
Krishna
Pal
Programming
lab(C)

.
3.
Enter
a
number
and
check
whether
it
is
Odd
or
Even.
.
####Definition : This concept begins with integers. An even number is an integer
that is
"evenly divisible" by 2, i.e., divisible by 2 without remainder; an odd number is
an integer
that is not evenly divisible by 2.
.
####Flow Chart :
#0 =0
START
Input N
Is
N%2
Print EvenPrint Odd
STOP

9 Created
by
Krishna
Pal
Programming
lab(C)

.
####Programme :
/*Directives to include standard library and standard input-output header files in
this
program*/

#include<stdio.h>
#include<conio.h>

void main() /* Main function started here */

{
int n; /* Declaration of variable */
clrscr(); /* Clear the console screen */
printf("\nEnter the number:\t");
scanf("%d",&n); /* read data from keyboard */
if(n%2==0) /* checking for Odd/Even */

printf("\nThe given number[%d] is: EVEN",n);


else
printf("\nThe given number[%d] is: ODD",n);
getch();
}

.
####Output :
Enter the number: 8
The given number[8] is: EVEN

Enter the number: 7


The given number[7] is: ODD

10 Created
by
Krishna
Pal
Programming
lab(C)

.
4.
Write
a
programme
to
find
the
roots
of
Quadratic
Equations.
.
####Definition : A real number X will be called a solution or a root if it
satisfies the given
equation, meaning a*(X^2)+ b*X+ c=0.
.
####Flow Chart :
<0 >0
=0
START
Input a,b,c
Is d
STOP
Print r1.r2
d = (b*b)-(4*a*c)
r1=r2= � b/ (2*a)
d =d * ( -1)
temp=sqrt(d)
t1=-b/(2*a)
t2=temp/(2*a)
r1=t1+t2
r2=t1-t2
temp=sqrt(d)
r1=(-b+temp)/(2*a)
r2=(-b-temp)/(2*a)
11 Created
by
Krishna
Pal
Programming
lab(C)

.
####Programme :
/*Directives to include standard library and standard input-output header files in
this
program*/

#include<stdio.h>
#include<conio.h>
#include<math.h>

void main() /* Main function started here */

{
float a,b,c,d,temp,r1,r2; /* Declaration of variable */
clrscr(); /* Clear the console screen */
printf("\nEnter the value of a\t:");
scanf("%f",&a); /* read the values of a,b & c from keyboard */
printf("Enter the value of b\t:");
scanf("%f",&b);
printf("Enter the value of c\t:");
scanf("%f",&c);

d=((b*b)-(4*c*a)); //Calculating discriminant

if(d==0)

{
printf("\nRoots are real and equal\n");
r1=-b/(2*a);
printf("\tX1=%f\n\tX2=%f",r1,r1);

}
else if(d>0)
{

printf("\nRoots are real and distinct\n");


temp=sqrt(d);
r1=(-b+temp)/(2*a);
r2=(-b-temp)/(2*a);
printf("\n\tX1=%f\n\tX2=%f",r1,r2);

}
else
{

printf("\nRoots are Imaginary\n");


d*=-1;
temp=sqrt(d);
r1=-b/(2*a);
r2=temp/(2*a);
printf("\n\tX1=(%f + %fi)\n\tX2=(%f -%fi)",r1,r2,r1,r2);

}
getch();
}

12 Created
by
Krishna
Pal
Programming
lab(C)

.
####Output :
Enter the value of a :2
Enter the value of b :3
Enter the value of c :4

Roots are Imaginary

X1=(-0.750000 + 1.198958i)
X2=(-0.750000 -1.198958i)

Enter the value of a :3


Enter the value of b :6
Enter the value of c :3

Roots are real and equal


X1=-1.000000
X2=-1.000000

Enter the value of a :3


Enter the value of b :7
Enter the value of c :1

Roots are real and distinct

X1=-0.152873
X2=-2.180460

13 Created
by
Krishna
Pal
Programming
lab(C)

.
5.
Write
a
programme
to
find
the
sum
of
natural
numbers
from
1
to
a
given
numbers.

.
####Definition : The act or process of adding, especially the process of computing
with sets of
numbers so as to find their sum.
.
####Flow Chart :
START
Input N
Sum = 0I=1

No
Yes
Is
I <= N
STOP
Print Sum
Sum = Sum +I
I = 1 + 1
14 Created
by
Krishna
Pal
Programming
lab(C)

.
####Programme :
/*Directives to include standard library and standard input-output header files in
this
program*/

#include<stdio.h>
#include<conio.h>

void main() /* Main function started here */

{
int n,sum=0,i; /* Declaration of variable */
clrscr(); /* Clear the console screen */
printf("\n\nEnter the limit\t:"); /* read limit of no. from user */
scanf("%d",&n);
for(i=1;i<=n;i++)

sum+=i; /* Calculating Sum */


printf("\nThe sum of the natural numbers within given limit is\t:%d",sum);
getch();

.
####Output :
Enter the limit :5
The sum of the natural numbers within given limit is :15
Enter the limit :0
The sum of the natural numbers within given limit is :0
Enter the limit :10
The sum of the natural numbers within given limit is :55

15 Created
by
Krishna
Pal
Programming
lab(C)

.
6.
Write
a
programme
to
find
the
factorial
of
a
number.
.
####Definition : The factorial of a positive integer N, denoted by N!, is the
product of all
positive integers equal to and less than N i.e. N!=1 * 2 * 3 *4 �N
.
####Flow Chart :
START
INPUT n
i = 1 is
i ++ i <= n
Fact = Fact * i
PRINT Fact
Fact = 1
STOP

16 Created
by
Krishna
Pal
Programming
lab(C)

.
####Programme :
/*Directives to include standard library and standard input-output header files in
this
program*/

#include<stdio.h>
#include<conio.h>

void main() /* Main function started here */

{
int n,fact=1,i; /* Declaration of variable */
clrscr() ; /* Clear the console screen */
printf("\n\nEnter the number\t:");
scanf("%d",&n);
for(i=1;i<=n;i++)

fact*=i; /* Calculating factorial */


printf("\nThe factorial of the given number is\t:%d",fact);
getch();

.
####Output :
Enter the number :0
The factorial of the given number is :1
Enter the number :1
The factorial of the given number is :1
Enter the number :5
The factorial of the given number is :120

17 Created
by
Krishna
Pal
Programming
lab(C)

.
7.
Write
a
programme
to
print
the
prime
numbers
from
1
to
a
given
numbers.

.
####Definition : An integer greater than 1 that is not evenly divisible by any
number other than
itself.
.
####Flow Chart :
START

Input N

18
No
Yes
Yes
No
Yes
#0
=0
#0
=0
Flag = 0
J = 2
Is
I <= N
STOP
Print I
I = 1
Is
J <= I/2
is
I % J
Flag = 1
J = J +1
is
Flag
I = I +1
Created
by
Krishna
Pal
Programming
lab(C)

.
####Programme :
/*Directives to include standard library and standard input-output header files in
this
program*/

#include<stdio.h>
#include<conio.h>

void main() /* Main function started here */

{
int n,i,j,flag; /* Declaration of variable */
clrscr(); /* Clear the console screen */
printf("\n\nEnter the limit\t:"); /* read limit of no. from user */
scanf("%d",&n);
printf("\nThe prime numbers within the given limit:\n");
for(i=1;i<=n;i++)
{ flag=0;

for(j=2;j<=i/2;j++)

{ if(i%j==0) /* Checking for prime */


{ flag=1;
break;
}
}

if(flag==0)

printf("%d\t",i);
}
getch();

.
####Output :
Enter the limit :10

The prime numbers within the given limit:


12357

Enter the limit :20

The prime numbers within the given limit:


1 2 3 5 7 11 13 17 19

19 Created
by
Krishna
Pal
Programming
lab(C)

.
8.
Write
a
programme
to
print
the
perfect
numbers
from
1
to
a
given
numbers.

.
####Definition : A perfect number is a number equal to the sum of all its proper
divisors(divisors smaller than the number) including 1.
.
####Flow Chart :
START

Input N

20
No
Yes
Yes
No
Yes
#0
=0
No
Yes
S = 0
J = 1
Is
I <= N
STOP
Print I
I = 1
Is
J <= I/2
is
I % J
S = S + J
J = J +1
is
S = I
I = I +1
Created
by
Krishna
Pal
Programming
lab(C)

.
####Programme :
/*Directives to include standard library and standard input-output header files in
this
program*/

#include<stdio.h>
#include<conio.h>

void main() /* Main function started here */

{
int n,i,s,j; /* Declaration of variable */
clrscr(); /* Clear the console screen */
printf("\n\nEnter the limit\t:");
scanf("%d",&n); /* read limit of no. from user */
printf("\nThe Perfect numbers within the given limit:\n");
for(i=1;i<=n;i++)
{

s=0;
for(j=1;j<=i/2;j++)
{

if(i%j==0) /* Checking for divisibility */

s+=j; /* adding the divisor */


}
if(s==i)
printf("%d\t",i);

}
getch();

.
####Output :
Enter the limit :10

The Perfect numbers within the given limit:


6

Enter the limit :50

The Perfect numbers within the given limit:


6 28

21 Created
by
Krishna
Pal
Programming
lab(C)

.
9.
Write
a
programme
to
print
the
Fibonacci
series
:
1,1,2,3,5,8,13,..........N.

.
####Definition : Sequence of numbers in which 1 appears twice as the first two
numbers, and
every subsequent number is the sum of two preceding numbers i.e. Fn = Fn-1 + Fn-2
.
####Flow Chart :
No
Yes
START
INPUT T
is
I <= T
M = 0
N = 1
F = 1
F = M + N
M = N
N = F
STOP
PRINT F
I = 1
I = I + 1
22 Created
by
Krishna
Pal
Programming
lab(C)

.
####Programme :
/*Directives to include standard library and standard input-output header files in
this
program*/

#include<stdio.h>
#include<conio.h>

void main() /* Main function started here */

{
int t,i,m=0,n=1,f=1; /* Declaration & Initialization of variable */
clrscr(); /* Clear the console screen */
printf("\n\nEnter the number of terms\t:");
scanf("%d",&t); /* read no. of terms from user */
printf("\n The Fibonacci Series of %d terms:\n",t);
for(i=1;i<=t;i++)
{

printf("%d\t",f);
f=m+n; /* calculating Fibonacci number */
m=n;
n=f;

}
getch();
}

.
####Output :
Enter the number of terms :10
The Fibonacci Series of 10 terms:
1 1 2 3 5 8 13 21 34 55
Enter the number of terms :20
The Fibonacci Series of 20 terms:
1 1 2 3 5 8 13 21 34 55
89 144 233 377 610 987 1597 2584 4181 6765

23 Created
by
Krishna
Pal
Programming
lab(C)

.
10.
Write
a
programme
to
enter
some
numbers
and
find
how
many
of
them
are
odd/even
and
find
the
average
of
odd
and

even
numbers.

.
####Definition : An even number is an integer that is "evenly divisible" by 2, an
odd number is
an integer that is not evenly divisible by 2. Then count the odd and even number
separately.
And divide the sum of odd numbers by the number of odd number, Similar to even
numbers.
.
####Flow Chart :
Yes
#0 = 0
START
INPUT n
ev = 0, od = 0, sum_ev=0, sum_od=0,
avg_ev=0, avg_od=0
STOP
i =0 is
i ++ i < n
i =1 is
i ++ i <=n
INPUT a[i]
Is
a[i] % 2
sum_ev =sum_ev +a[i]
ev = ev + 1
sum_od =sum_od +a[i]
od = od + 1
PRINT ev, avg_ev, od, avg_od avg_ev = sum_ev / ev, avg_od = sum_od / od
No

24 Created
by
Krishna
Pal
Programming
lab(C)

.
####Programme :
/*Directives to include standard library and standard input-output header files in
this
program*/

#include<stdio.h>
#include<conio.h>

void main() /* Main function started here */

{
int a[100],i,n,ev=0,od=0; /* Declaration & Initialization of variable */
float sum_ev=0,sum_od=0,avg_ev=0,avg_od=0;
clrscr(); /* Clear the console screen */
printf("\n\nEnter the number of terms[<100]\t:");
scanf("%d",&n); /* read no. of terms from user */
if(n>100) exit(0);
for(i=0;i<n;i++)
{

printf("\nEnter the %dth number\t:",i+1);


scanf("%d",&a[i]);
}

for(i=0;i<n;i++)

{
if(a[i]%2==0) /* checking for even/odd */
{ sum_ev+=a[i]; /* summing up even */

ev++; /* increment no. of even */


}
else
{ sum_od+=a[i]; /* summing up Odd */

od++; /* increment no. of Odd */

}
}
if(ev>0)

avg_ev=sum_ev/ev; /* calculating average */


if(od>0)
avg_od=sum_od/od;

printf("\nThe number of Even is\t:%d",ev);


printf("\nThe average of even numbers is\t:%f",avg_ev);

printf("\n\nThe number of Odd is\t:%d",od);


printf("\nThe average of even numbers is\t:%f",avg_od);
getch();

25 Created
by
Krishna
Pal
Programming
lab(C)

.
####Output :
Enter the number of terms[<100] :10

Enter the 1th number :2


Enter the 2th number :3
Enter the 3th number :4
Enter the 4th number :55
Enter the 5th number :6
Enter the 6th number :9
Enter the 7th number :2
Enter the 8th number :1
Enter the 9th number :46

Enter the 10th number :7

The number of Even is :5


The average of even numbers is :12.000000

The number of Odd is :5


The average of even numbers is :15.000000

Enter the number of terms[<100] :3


Enter the 1th number :1
Enter the 2th number :78
Enter the 3th number :90
The number of Even is :2

The average of even numbers is :84.000000


The number of Odd is :1
The average of even numbers is :1.000000

26 Created
by
Krishna
Pal
Programming
lab(C)

.
11.
Enter
some
numbers
and
find
the
maximum
and
minimum
without
array.

.
####Programme :
/*Directives to include standard library and standard input-output header files in
this
program*/

#include<stdio.h>
#include<conio.h>

void main() /*Main function started here*/

int n,i,max,min,m; /*Declaration of variable*/

clrscr();

printf("\n\nHow many numbers do you have? ");

scanf("%d",&n);

printf("\nEnter 1st number:\t");

scanf("%d",&m);

max=min=m;

for(i=2;i<=n;i++)

printf("\nEnter %dth number:\t",i);

scanf("%d",&m);

if(m<min) /* Checking for minimum number */

min=m;
if(m>max) /* Checking for maximum number */

max=m;

printf("\n\nMaximum is=%d & Minimum is=%d",max,min);

getch();
}

27 Created
by
Krishna
Pal
Programming
lab(C)

.
####Output :

How many numbers do you have? 5


Enter 1st number: 1
Enter 2th number: -5
Enter 3th number: -2
Enter 4th number: 0
Enter 5th number: 4

Maximum is=4 & Minimum is=-5

How many numbers do you have? 6


Enter 1st number: 0
Enter 2th number: 2
Enter 3th number: 2
Enter 4th number: 5
Enter 5th number: 3
Enter 6th number: 7

Maximum is=7 & Minimum is=0

28 Created
by
Krishna
Pal
Programming
lab(C)

.
12.
Write
a
programme
to
find
the
L.C.M.
and
H.C.F
of
two
given
numbers.

.
####Programme :
#include<stdio.h>
#include<conio.h>

void main()

int m,n,r,i=1;

clrscr();

printf("\n\nEnter two numbers:\t");

scanf("%d%d",&m,&n);

r=m;

while(r%n!=0)

r=m*(++i); /* calculating LCM */

printf("\nThe LCM of [%d & %d] is= %d",m,n,r);

/* Calculating HCF using formulae: (number1*number2)/LCM of number1 & number2 */


printf("\nThe HCF of [%d & %d] is= %d",m,n,(m*n)/r);

/* this is the general process of calculating HCF */


/* if(m<=n && n%m==0)
r=m;
else if(n<=m && m%n==0)
r=n;
else
r=1; */

getch();
}

.
####Output :
Enter two numbers: 2
9

The LCM of [2& 9] is= 18


The HCF of [2 &9] is= 1

Enter two numbers: 6


2

The LCM of [6& 2] is= 6


The HCF of [6 &2] is= 2

29 Created
by
Krishna
Pal
Programming
lab(C)

.
13.
Write
a
programme
to
find
the
GCD
using
iteration
and
recursions
function.

.
####Programme :
#include<stdio.h>
#include<conio.h>

int gcd(int,int); /* prototype declaration of gcd function */

void main()

int m,n;

clrscr();

printf("\n\nEnter two numbers:\t");

scanf("%d%d",&m,&n);

printf("\nThe GCD of [%d & %d] is= %d",m,n,gcd(m,n)); /* calling gcd function */

getch();
}

int gcd(int x,int y) /* function definition of gcd starts here */


{
if (y%x==0)
return(x);
return(gcd(y%x,x)); /* calling recursively */
}

.
####Output :
Enter two numbers: 2
7

The GCD of [2 &7] is= 1


Enter two numbers: 8
2

The GCD of [8 &2] is= 2

Enter two numbers: 3


12

The GCD of [3 &12] is= 3

30 Created
by
Krishna
Pal
Programming
lab(C)

.
14.
Write
a
programme
to
accept
a
number
and
check
whether
it
is
Armstrong
or
not.

.
####Definition : An Armstrong number or a plus perfect number is a number that is
the sum of
its own digits each raised to the power of the number of digits.
.
####Programme :
#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()

{
int n,d=0,temp,s=0;
clrscr();
printf("\n\nEnter a number:\t");
scanf("%d",&n);

temp=n;
while(temp)
{

temp/=10; /* counting number of digits */

d++;
}
temp=n;
while(temp) /* calculating Armstrong number */
{ s+=pow((temp%10),d);

temp/=10;
}
if (s==n)
printf("\nThe given number is an Armstrong Number.");
else
printf("\nThe given number isn't an Armstrong Number!");
getch();
}

.
####Output :
Enter a number: 153
The given number is an Armstrong Number.

Enter a number: 12
The given number isn't an Armstrong Number!

Enter a number: 7

31 Created
by
Krishna
Pal
Programming
lab(C)

The given number is an Armstrong Number.

.
15.
To
find
the
sum
of
the
digits
of
a
given
number.
.
####Programme :
#include<stdio.h>
#include<conio.h>

void main()

int n,s=0;

clrscr();

printf("\n\nEnter the numbers:\t");

scanf("%d",&n);

while(n)

s+=(n%10);

n/=10;

printf("\nThe sum of the digits of the number is= %d",s);

getch();
}

.
####Output :
Enter the numbers: 100
The sum of the digits of the number is= 1

Enter the numbers: 1010


The sum of the digits of the number is= 2

Enter the numbers: 512


The sum of the digits of the number is= 8

32 Created
by
Krishna
Pal
Programming
lab(C)

.
16.
To
print
the
palindrome
numbers
from
100
to
1000.
.
####Programme :
#include<stdio.h>
#include<conio.h>

void main()

{
int n,s,i;
clrscr();
printf("\n\nThe Palindrome numbers from 100 to 1000 are:\n");
for(i=100;i<=1000;i++)
{

n=i;
s=0;
while(n)
{

s=s*10+(n%10);

n/=10;
}
if(s==i)

printf("%d\t",i);
}
getch();

.
####Output :
The Palindrome numbers from 100 to 1000 are:
101 111 121 131 141 151 161 171 181 191
202 212 222 232 242 252 262 272 282 292
303 313 323 333 343 353 363 373 383 393
404 414 424 434 444 454 464 474 484 494
505 515 525 535 545 555 565 575 585 595
606 616 626 636 646 656 666 676 686 696
707 717 727 737 747 757 767 777 787 797
808 818 828 838 848 858 868 878 888 898
909 919 929 939 949 959 969 979 989 999

33 Created
by
Krishna
Pal
Programming
lab(C)

.
17.
S=
1
+
X
+
X^3
+
X^5
+
.............N..
.
####Programme :
#include<stdio.h>
#include<conio.h>
#include<math.h>
/*Directives to include standard library for performing mathematical function pow()
*/

void main()

{
int X,n,i,s=1,k=1;
clrscr();
printf("\n\nEnter the value of X\t:");
scanf("%d",&X);
printf("\nEnter the number of terms\t:");
scanf("%d",&n);
for(i=2;i<=n;i++)
{ s+=pow(X,k);

k+=2;
}
printf("\nThe result is\t:%d",s);
getch();

.
####Output :
Enter the value of X :2
Enter the number of terms
The result is :1
:1
Enter the value of X :2
Enter the number of terms
The result is :171
:5

34 Created
by
Krishna
Pal
Programming
lab(C)

.
18.
S=
1+(1+2)+(1+2+3)+.........N.
.
####Programme :
#include<stdio.h>
#include<conio.h>

void main()

int n,i,s=0,r=0;

clrscr();

printf("\n\nEnter the number of terms\t:");

scanf("%d",&n);

for(i=1;i<=n;i++)

s+=i;

r+=s;

printf("\nThe result is\t:%d",r);

getch();
}

.
####Output :
Enter the number of terms :1
The result is :1

Enter the number of terms :5


The result is :35

35 Created
by
Krishna
Pal
Programming
lab(C)

.
19.
S=
1+
2/2!
+
3/3!
+
4/4!
+......N.
.
####Programme :
#include<stdio.h>
#include<conio.h>

void main()

int n,i;

float s=0,f=1;

clrscr();

printf("\n\nEnter the number of terms\t:");

scanf("%d",&n);

for(i=1;i<=n;i++)

{ f*=i; /* calculating factorial */

s+=(i/f);

printf("\nThe result is\t:%f",s);

getch();
}

.
####Output :
Enter the number of terms
The result is :1.000000
:1
Enter the number of terms
The result is :2.708333
:5

36 Created
by
Krishna
Pal
Programming
lab(C)

.
20.
S=
1
+
X
+
X^2
+
X^3/3!
+
X^4/4!
+.......N.
.
####Programme :
#include<stdio.h>
#include<conio.h>

void main()

{
int x,n;
float s=1,f=1,p=1,i;
clrscr();
printf("\n\nEnter the number of terms\t:");
scanf("%d",&n);
printf("\nEnter the value of X\t:");
scanf("%d",&x);
for(i=1;i<n;i++)
{

f*=i;
p*=x;
s+=(p/f);

}
printf("\nThe result is\t:%f",s);
getch();

.
####Output :
Enter the number of terms
Enter the value of X :2
The result is :1.000000
:1
Enter the number of terms
Enter the value of X :2
The result is :7.000000
:5
37 Created
by
Krishna
Pal
Programming
lab(C)

.
21.
Sin
X=
X
-X^3/3!
+
X^5/5!
-X^7/7!
+..N.
.
####Programme :
#include<stdio.h>
#include<conio.h>

void main()

{
int x,n,i,f=1,sign=1,p=1;
float s=0;
clrscr();
printf("\n\nEnter the number of terms\t:");
scanf("%d",&n);
printf("\nEnter the value of X\t:");
scanf("%d",&x);
for(i=1;i<n*2;i++)
{ f*=i;

p*=x;
if(i%2==0)

continue;
s+=sign*(float)p/f;
sign*=-1;

}
printf("\nThe result is\t:%f",s);
getch();

.
####Output :
Enter the number of terms
Enter the value of X :30
The result is :30.000000
:1
Enter the number of terms
Enter the value of X :2
The result is :0.891059
:5
38 Created
by
Krishna
Pal
Programming
lab(C)

.
22.
Enter
some
numbers
and
store
them
in
an
array,
then
enter
a
no
and
check
whether
it
exists
in
the
array
or
not.

.
####Programme :
#include<stdio.h>
#include<conio.h>

void main()

{
int a[50],n,m,i,flag=0;
clrscr();
printf("\n\nHow many number do you have[<50]?\t");
scanf("%d",&n);
if(n>50)

exit(0); /* conditional exiting from the programme */


for(i=0;i<n;i++)
{

printf("\nEnter %dth number:\t",i+1);

scanf("%d",&a[i]); /* read array elements */


}
printf("\n\nEnter the number that you want to search\t:");
scanf("%d",&m);
for(i=0;i<n;i++)

{ /* performing linear search */


if(a[i]==m)
{
flag=1;
break;
}
}

if (flag==1)
printf("\nThe number has found in the array at %dth position.",i+1);
else
printf("\nEntered number does not exist in the array!");
getch();
}

39 Created
by
Krishna
Pal
Programming
lab(C)

.
####Output :

How many number do you have[<50]? 5


Enter 1th number: 1
Enter 2th number: 2
Enter 3th number: -2
Enter 4th number: 6
Enter 5th number: 4

Enter the number that you want to search :-2


The number has found in the array at 3th position.

How many number do you have[<50]? 6


Enter 1th number: 0
Enter 2th number: 2
Enter 3th number: 6
Enter 4th number: 7
Enter 5th number: 10
Enter 6th number: 12

Enter the number that you want to search :7


The number has found in the array at 4th position.

40 Created
by
Krishna
Pal
Programming
lab(C)

.
23.
Enter
some
numbers
and
store
them
in
an
array,
now
create
two
array
of
odd
and
even
numbers.

.
####Programme :
#include<stdio.h>
#include<conio.h>

void main()

{
int a[50],e[50],o[50],n,i,j=0,k=0;
clrscr();
printf("\n\nHow many numbers do you have[<50]?\t");
scanf("%d",&n);
if(n>50)

exit(0);
for(i=0;i<n;i++)
{

printf("\nEnter %dth number:\t",i+1);


scanf("%d",&a[i]);
if(a[i]==0)

continue;
if(a[i]%2==0)
e[j++]=a[i]; /* inserting elements into array of even */
else

o[k++]=a[i]; /* inserting elements into array of odd */


}
printf("\n\nEven numbers are:\n");
for(i=0;i<j;i++)

printf("%d\t",e[i]);
printf("\nOdd numbers are:\n");
for(i=0;i<k;i++)

printf("%d\t",o[i]);
getch();
}

41 Created
by
Krishna
Pal
Programming
lab(C)

.
####Output :
How many numbers do you have[<50]?
Enter 1th number: 2
5
Enter 2th number: 0
Enter 3th number: 3
Enter 4th number: 7
Enter 5th number: 1
Even numbers are:
2
Odd numbers are:
3 7 1
How many numbers do you have[<50]?
Enter 1th number: 2
6
Enter 2th number: 2
Enter 3th number: 6
Enter 4th number: 9
Enter 5th number: 7
Enter 6th number: 10
Even numbers are:
2 2 6 10
Odd numbers are:
9 7

42 Created
by
Krishna
Pal
Programming
lab(C)

.
24.
Write
a
programme
to
change
a
Decimal
numbers
to
Binary
numbers.

.
####Programme :
#include<stdio.h>
#include<conio.h>

void main()

{
int n,b[20],i=0,j;
clrscr();
printf("\n\nEnter Decimal number\t:");
scanf("%d",&n);
while(n)
{

b[i++]=n%2;

n/=2;
}
printf("\nEquivalent Binary number:\t");
for(j=i-1;j>=0;j--)

printf("%d",b[j]);
getch();
}

.
####Output :
Enter Decimal number :5
Equivalent Binary number: 101
Enter Decimal number :1
Equivalent Binary number: 1
Enter Decimal number :10
Equivalent Binary number: 1010

43 Created
by
Krishna
Pal
Programming
lab(C)

.
25.
Write
a
programme
to
change
decimal
numbers
to
Octal
and
Hexadecimals.

.
####Programme :
#include<stdio.h>
#include<conio.h>

void main()

{
int n,oct[20],hex[20],i=0,j,k=0,temp;
clrscr();
printf("\n\nEnter Decimal number\t:");
scanf("%d",&n);
temp=n;
while(n)
{

oct[i++]=n%8;
/* storing equivalent Octal no. in reverse order */

n/=8;
}
n=temp;
while(n)
{

hex[k++]=n%16; /* storing equivalent Hexadecimal no. in reverse order */

n/=16;
}
printf("\nEquivalent Octal number:\t");
for(j=i-1;j>=0;j--)

printf("%d",oct[j]);
/* printing equivalent Octal no. */

printf("\nEquivalent Hexadecimal number:\t");


for(j=k-1;j>=0;j--)
{

switch(hex[j])

{
case 10:printf("A"); break;
case 11:printf("B"); break;
case 12:printf("C"); break;
case 13:printf("D"); break;
case 14:printf("E"); break;
case 15:printf("F"); break;
default:printf("%d",hex[j]);

}
}
getch();

44
Created
by
Krishna
Pal
Programming
lab(C)

.
####Output :

Enter Decimal number :10

Equivalent Octal number: 12


Equivalent Hexadecimal number: A

Enter Decimal number :30

Equivalent Octal number: 36


Equivalent Hexadecimal number: 1E

Enter Decimal number :50

Equivalent Octal number: 62


Equivalent Hexadecimal number: 32

45 Created
by
Krishna
Pal
Programming
lab(C)

.
26.
Write
a
programme
to
change
Binary
numbers
to
Decimals.

.
####Programme :
#include<stdio.h>
#include<conio.h>
#include<math.h> /*Directives to include standard library for performing
mathematical

function pow() */

void main()

{
int n,d=0,i=0,j;
clrscr();
printf("\n\nEnter Binary number\t:");
scanf("%d",&n);
while(n)
{

d+=(n%10)*pow(2,i++);

n/=10;
}
printf("\nEquivalent Decimal number:\t%d",d);
getch();

.
####Output :
Enter Binary number :101
Equivalent Decimal number: 5

Enter Binary number :1010


Equivalent Decimal number: 10

46 Created
by
Krishna
Pal
Programming
lab(C)

.
27.
Write
a
programme
to
enter
elements
in
a
2D
Array
and
display
in
Matrix
from.

.
####Programme :
#include<stdio.h>
#include<conio.h>

void main()

int a[10][10],i,j,r,c; /* Declaration of 2D array named a */

clrscr();

printf("\n\nEnter number of rows\t:");

scanf("%d",&r);

printf("\nEnter number of columns\t:");

scanf("%d",&c);

printf("\nEnter the elements[Row-Wise]:\n");

for(i=0;i<r;i++) /* reading elements of 2D array from user via keyboard */

for(j=0;j<c;j++)

scanf("%d",&a[i][j]);

clrscr();

printf("\n\nThe elements in Matrix form:\n");

for(i=0;i<r;i++)
{ for(j=0;j<c;j++)

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

printf("\n\n");

getch();
}

.
####Output :
Enter number of rows :3

Enter number of columns :3

Enter the elements[Row-Wise]:


1
2
12
4
5
50
7
0
10

The elements in Matrix form:

1 2 12
4 5 50
7 0 10

47 Created
by
Krishna
Pal
Programming
lab(C)

.
28.
Write
a
programme
to
sort
some
numbers
using
bubble
sort
and
Selection
sort
separately.

.
####Programme :
#include<stdio.h>
#include<conio.h>
#include<ctype.h>

void bubble(int[],int); /* prototype declaration of bubble sort function */


void selection(int[],int); /* prototype declaration of selection sort function */
int i,j,temp;

void main()

int a[20],n;

clrscr();

printf("\n\nEnter the number of elements\t:");

scanf("%d",&n);

if(n>20)

{ printf("\nCould not allocate space!");

getch(); exit(0);}

for(i=0;i<n;i++)

printf("\nEnter %dth number\t:",i+1);

scanf("%d",&a[i]);
}

printf("\n\nFor Bubble sorting=>B ** For Selection sorting=>S");

printf("\nEnter your choice\t:");

fflush(stdin);

switch(toupper(getchar()))

{ case 'B': bubble(a,n); break; /* calling bubble sort function */

case 'S': selection(a,n); break; /* calling selection sort function */

default: printf("\n Wrong choice!"); getch(); exit(0);

printf("\nSorted elements in ascending order:\n");

for(i=0;i<n;i++) /* printing sorted array */

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

48 Created
by
Krishna
Pal
Programming
lab(C)

/* definition of bubble sort function starts here*/


void bubble(int a[],int n)
{ for(i=0;i<n-1;i++)

for(j=0;j<(n-1)-i;j++)
if(a[j]>a[j+1])
{ temp=a[j];

a[j]=a[j+1];
a[j+1]=temp;
}
}

/* definition of selection sort function starts here*/


void selection(int a[],int n)
{ int loc,min;

for(i=0;i<n;i++)

{
min=a[i];
loc=i;
for(j=i+1;j<n;j++)
{ if(a[j]<min)

{
min=a[j];
loc=j;

}
}
if(loc!=i)
{ temp=a[i];

a[i]=a[loc];
a[loc]=temp;
}
}
}

.
####Output :
Enter the number of elements :5
Enter 1th number :1
Enter 2th number :2
Enter 3th number :-3
Enter 4th number :-4
Enter 5th number :0

For Bubble sorting=>B ** For Selection sorting=>S


Enter your choice :b
Sorted elements in ascending order:
-4 -3 0 1 2

49
Created
by
Krishna
Pal
Programming
lab(C)

Enter the number of elements :10

Enter 1th number :1


Enter 2th number :3
Enter 3th number :20
Enter 4th number :35
Enter 5th number :20
Enter 6th number :-50
Enter 7th number :2
Enter 8th number :7
Enter 9th number :8
Enter 10th number :45

For Bubble sorting=>B ** For Selection sorting=>S


Enter your choice :s

Sorted elements in Ascending order:


-50 123 7820 20 35

50 Created
by
Krishna
Pal
Programming
lab(C)

.
29.
Write
a
programme
to
find
the
sum
of
each
row
in
a
2D
Array.

.
####Programme :
#include<stdio.h>
#include<conio.h>

void main()

{
int a[10][10],i,j,r,c;
clrscr();
printf("\n\nEnter number of rows\t:");
scanf("%d",&r);
printf("\nEnter number of columns\t:");
scanf("%d",&c);
printf("\nEnter the elements[Row-Wise]:\n");
for(i=0;i<r;i++)

for(j=0;j<c;j++)

scanf("%d",&a[i][j]);
clrscr();
printf("\n\nThe given 2D Array is:\n");
for(i=0;i<r;i++)
{ for(j=0;j<c;j++)

printf("%4d",a[i][j]);
printf("\n\n");
}

/* Adding up each row elements and store them in last column of the 2D array*/
for(i=0;i<r;i++)
{ a[i][c]=0;
for(j=0;j<c;j++)

a[i][c]+=a[i][j];
}
/* printing the matrix after adding each row elements of the given 2D array */
printf("\n\nThe 2D Array after adding up each row:\n");
for(i=0;i<r;i++)
{ for(j=0;j<=c;j++)

if(j==c)
printf(" =%d",a[i][j]);
else
printf("%4d",a[i][j]);

printf("\n\n");
}
getch();

51 Created
by
Krishna
Pal
Programming
lab(C)

.
####Output :

Enter number of rows :4

Enter number of columns :4

Enter the elements[Row-Wise]:


1
2
30
1
4
6
8
7
9
10
-2
-6
42
0
17
20

The given 2D Array is:


12 30 1

468 7

9 10 -2 -6

42 0 17 20

The 2D Array after adding up each row:


12 30 1 =34

46 8 7 =25

9 10 -2 -6 =11

42 0 17 20 =79

52 Created
by
Krishna
Pal
Programming
lab(C)

.
30.
Write
a
programme
to
find
the
sum
of
each
column
of
a
2D
Array.

.
####Programme :
#include<stdio.h>
#include<conio.h>

void main()

{
int a[10][10],i,j,r,c;
clrscr();
printf("\n\nEnter number of rows\t:");
scanf("%d",&r);
printf("\nEnter number of columns\t:");
scanf("%d",&c);
printf("\nEnter the elements[Row-Wise]:\n");
for(i=0;i<r;i++)

for(j=0;j<c;j++)

scanf("%d",&a[i][j]);
clrscr();
printf("\n\nThe given 2D Array is:\n");
for(i=0;i<r;i++)
{ for(j=0;j<c;j++)

printf("%4d",a[i][j]);
printf("\n\n");
}

/* Adding up each column elements and store them in last column of the 2D array*/
for(j=0;j<c;j++)
{ a[r][j]=0;

for(i=0;i<r;i++)
a[r][j]+=a[i][j];
}

/* printing the matrix after adding each column elements of the given 2D array */
printf("\n\nThe 2D array after adding up each column:\n");
for(i=0;i<=r;i++)
{ for(j=0;j<c;j++)

if(i==r)
printf(" =%d",a[i][j]);
else
printf("%4d",a[i][j]);

printf("\n\n");
}
getch();

53 Created
by
Krishna
Pal
Programming
lab(C)

.
####Output :
Enter number of rows :3

Enter number of columns :4

Enter the elements[Row-Wise]:


1
0
2
3
5
4
6
9
7
8
4
3

The given 2D Array is:

1 0 2 3
5 4 6 9
7 8 4 3

The 2D array after adding up each column:

10 2 3

54 6 9

78 4 3

=13 =12 =12 =15

54 Created
by
Krishna
Pal
Programming
lab(C)

.
31.
Write
a
programme
to
find
the
sum
of
the
diagonals
of
a
2D
Array.

.
####Programme :
#include<stdio.h>
#include<conio.h>

void main()

{
int a[10][10],i,j,r,s=0;
clrscr();
printf("\n\nEnter number of rows[square matrix]\t:");
scanf("%d",&r);
printf("\nEnter the elements[Row-Wise]:\n");
for(i=0;i<r;i++)

for(j=0;j<r;j++)

scanf("%d",&a[i][j]);
clrscr();
printf("\n\nThe given 2D Array is:\n");
for(i=0;i<r;i++)
{ for(j=0;j<r;j++)

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

printf("\n\n");
}
/* Adding up diagonals of the given 2D array */
for(j=0;j<r;j++)
{ for(i=0;i<r;i++)

{ if(i==j)
s+=a[i][j];
}
}
printf("\n\nThe sum of diagonals of the given 2D array:\t%d",s);
getch();

55 Created
by
Krishna
Pal
Programming
lab(C)

.
####Output :

Enter number of rows[square matrix] :3

Enter the elements[Row-Wise]:


1
3
4
7
0
6
8
9
5

The given 2D Array is:


134

706

895

The sum of diagonals of the given 2D array: 6

56 Created
by
Krishna
Pal
Programming
lab(C)

.
32.
Write
a
programme
to
find
the
sum
and
multiplication
of
two
matrices.

.
####Programme :
#include<stdio.h>
#include<conio.h>

int a[10][10],b[10][10],r[50][50],r1,r2,c1,c2,i,j,k,c,flag=0; /*declaration of


global variables */
void add(int a[10][10],int b[10][10]); /* prototype declaration of add function */
void multi(int a[10][10],int b[10][10]); /* prototype declaration of multi
function*/

void main()

{
clrscr();
printf("\n\nEnter the number of rows of 1st Matrix:\t");
scanf("%d",&r1);
printf("\n\nEnter the number of columns of 1st Matrix:\t");
scanf("%d",&c1);
printf("\nEnter the elements of 1st Matrix[Row-wise]=\n");
for(i=0;i<r1;i++)

for(j=0;j<c1;j++)

scanf("%d",&a[i][j]);
printf("\n\nEnter the number of rows of 2nd Matrix:\t");
scanf("%d",&r2);
printf("\n\nEnter the number of columns of 2nd Matrix:\t");
scanf("%d",&c2);
printf("\nEnter the elements of 2nd Matrix[Row-wise]=\n");
for(i=0;i<r2;i++)

for(j=0;j<c2;j++)

scanf("%d",&b[i][j]);
clrscr();
printf("\n1st matrix is\n");
for(i=0;i<r1;i++)
{ for(j=0;j<c1;j++)

printf("%d\t",a[i][j]);
printf("\n"); }
printf("\n2nd matrix is\n");
for(i=0;i<r2;i++)
{ for(j=0;j<c2;j++)

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

57 Created
by
Krishna
Pal
Programming
lab(C)

printf("\nFor Addition=>1\nFor Multiplication=>2\nExit=>Other"

"\n\tEnter your choice:\t");


scanf("%d",&c);
while(c)
{ switch(c)

{
case 1: add(a,b); break; /* calling add() function */
case 2: multi(a,b); break; /* calling multi() function */
default: exit(0);

}
if(flag==0)
{ printf("\n\n The resultant matrix is given bellow:\n");

for(i=0;i<r1;i++)
{ for(j=0;j<c2;j++) /* printing result */

printf("%d\t",r[i][j]);
printf("\n");
}

}
printf("\n\nFor Addition=>1\nFor Multiplication=>2\nExit=>Other"
"\n\tEnter your choice:\t");

scanf("%d",&c);
}
getch();

/* definition of add() function starts here*/


void add(int a[10][10],int b[10][10])
{ if(c1==c2 && r1==r2)

{ for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
r[i][j]=a[i][j]+b[i][j];

flag=0;
}
else
{ printf("\nAddition isn't possible!!");

getch();
flag=1;
}
}

/* definition of multi() function starts here*/


void multi(int a[10][10],int b[10][10])
{ if(c1==r2)

{ for(i=0;i<r1;i++)
for(j=0;j<c2;j++)
{ r[i][j]=0;

for(k=0;k<c1;k++)
r[i][j]+=a[i][k]*b[k][j];
}

flag=0;
}
else
{ printf("\nMultiplication isn't possible!!");

getch();
flag=1;
}
}

58
Created
by
Krishna
Pal
Programming
lab(C)

.
####Output :
Enter the number of rows of 1st Matrix: 3

Enter the number of columns of 1st Matrix: 3

Enter the elements of 1st Matrix[Row-wise]=


1
2
3
0
4
9
6
4
7

Enter the number of rows of 2nd Matrix: 3

Enter the number of columns of 2nd Matrix: 3

Enter the elements of 2nd Matrix[Row-wise]=


1
5
6
3
0
4
5
1
2

1st matrix is
123
049
647

2nd matrix is
156
304
512

59 Created
by
Krishna
Pal
Programming
lab(C)

For Addition=>1
For Multiplication=>2
Exit=>Other

Enter your choice: 1

The resultant matrix is given bellow:

2 7 9
3 4 13
11 5 9

For Addition=>1
For Multiplication=>2
Exit=>Other

Enter your choice: 2

The resultant matrix is given bellow:


22 8 20
57 9 34
53 37 66

For Addition=>1
For Multiplication=>2
Exit=>Other

Enter your choice: 5

Enter the number of rows of 1st Matrix: 2

Enter the number of columns of 1st Matrix: 1

Enter the elements of 1st Matrix[Row-wise]=


2
1

Enter the number of rows of 2nd Matrix: 1

Enter the number of columns of 2nd Matrix: 2

Enter the elements of 2nd Matrix[Row-wise]=


1
2

1st matrix is
2
1

2nd matrix is
12

60 Created
by
Krishna
Pal
Programming
lab(C)

For Addition=>1
For Multiplication=>2
Exit=>Other

Enter your choice: 1

Addition isn't possible!!

For Addition=>1
For Multiplication=>2
Exit=>Other

Enter your choice: 2

The resultant matrix is given bellow:


24
12

For Addition=>1
For Multiplication=>2
Exit=>Other

Enter your choice: 4

61 Created
by
Krishna
Pal
Programming
lab(C)

.
33.
Write
a
programme
to
find
the
transpose
of
a
matrix.
.
####Programme :
#include<stdio.h>
#include<conio.h>

void main()

{
int a[10][10],i,j,r,c,s[10][10];
clrscr();
printf("\n\nEnter number of rows\t:");
scanf("%d",&r);
printf("\nEnter number of columns\t:");
scanf("%d",&c);
printf("\nEnter the elements[Row-Wise]:\n");
for(i=0;i<r;i++)

for(j=0;j<c;j++)

scanf("%d",&a[i][j]);
printf("\n\nThe given matrix is:\n");
for(i=0;i<r;i++)
{ for(j=0;j<c;j++)

printf("%4d",a[i][j]);
printf("\n\n");
}

/* transposing and store the transposed matrix in s[][] */


for(i=0;i<c;i++)
for(j=0;j<r;j++)
s[i][j]=a[j][i];

/* printing the matrix after transposing */


printf("\n\nThe transposed matrix is given bellow:\n");
for(i=0;i<c;i++)
{ for(j=0;j<r;j++)

printf("%4d",s[i][j]);
printf("\n\n");
}
getch();

62 Created
by
Krishna
Pal
Programming
lab(C)

.
####Output :

Enter number of rows :3

Enter number of columns :4

Enter the elements[Row-Wise]:


1
2
3
5
9
4
7
8
10
14
0
6

The given matrix is:


1235

9478

1014 0 6

The transposed matrix is given bellow:


1 910

2 414

370

586

63 Created
by
Krishna
Pal
Programming
lab(C)

.
34.
Write
a
programme
to
accept
a
sentence
and
find
the
number
of
words.

.
####Programme :
/*Directives to include standard library and standard input-output header files in
this
program*/

#include<stdio.h>
#include<conio.h>

int wordcounter(char*); //prototype declaration of wordcounter function

void main()

{
char s1[50];
clrscr();
printf("\nEnter your sentence:\t");
fflush(stdin);
gets(s1);
printf("\nNumber of words in your sentence is=%d",wordcounter(s1));
getch();

int wordcounter(char *s) //definition of wordcounter function

{
int c=1;
while(*s)
{ if(*s==' ')

c++;

s++;
}
return c;
}

.
####Output :
Enter your sentence: Rcc Institute of Information Technology!
Number of words in your sentence is=5

64
Created
by
Krishna
Pal
Programming
lab(C)

#35.Writeaprogrammetoacceptawordandcountthe
number
of
vowels.

.
####Programme :
/*Directives to include standard library and standard input-output header files in
this
program*/

#include<stdio.h>
#include<conio.h>
#include<ctype.h>

int vowelcounter(char*); // prototype declaration of vowelcounter()

void main()

{
char s1[50];
clrscr();
printf("\nEnter your word:\t");
scanf("%s",&s1);
printf("\nNumber of vowels in given word is=%d",vowelcounter(s1));

// Calling vowelcounter()
getch();
}

int vowelcounter(char *s)


// defination of vowelcounter()

{
int c=0;
*s=tolower(*s);
while(*s)
{ if(*s=='a'||*s=='e'|| *s=='i'||*s=='o'||*s=='u')

c++;

s++;
}
return c;

.
####Output :
Enter your word: Institute
Number of vowels in given word is=4
65
Created
by
Krishna
Pal
Programming
lab(C)

#36.Writeaprogrammetoacceptasentenceandfindnumber
of
vowels,
consonants,
numbers,
spaces
and
special

characters.

.
####Programme :
#include<stdio.h>
#include<conio.h>
#include<ctype.h>

void main()

{ char s1[50],*s;

int v,c,n,sp,spcl;

v=c=n=sp=spcl=0;

clrscr();

printf("\nEnter your sentence:\t");

fflush(stdin);

gets(s1);

s=s1;

*s=tolower(*s);

while(*s)

{ if(*s==32) // checking for space

sp++;

else if(*s>=48 && *s<=57) // checking for number

n++;

else if((*s>=65 && *s<=90) || (*s>=97 && *s<=122))

{ if(*s=='a'||*s=='e'|| *s=='i'||*s=='o'||*s=='u') // checking for vowel

v++;
else

c++; // increment no. of consonants

else

spcl++; // increment no. of special character

s++;

printf("\nNumber of Vowels is:\t%d\n\tConsonants is:\t%d"

"\n\tNumbers is:\t%d\n\tSpace is:\t%d\n\tSpecial "


"Character is:\t%d",v,c,n,sp,spcl);
getch();
}

.
####Output :
Enter your sentence: Rcc Institute of Information Technology!

Number of Vowels is: 11


Consonants is: 24
Numbers is: 0
Space is: 4
Special Character is: 1

66 Created
by
Krishna
Pal
Programming
lab(C)

#37.Writeaprogrammetoacceptawordandfindthesumof
its
ASCII
values.

.
####Programme :
/*Directives to include standard library and standard input-output header files in
this
program*/

#include<stdio.h>
#include<conio.h>

int asciicounter(char*); // prototype declaration of asciicounter()

void main()

{ char s1[50];

clrscr();

printf("\nEnter your word:\t");

scanf("%s",&s1);

printf("\nSum of ASCII value for the given word is=%d",asciicounter(s1));

getch();
}

int asciicounter(char *s) // prototype declaration of asciicounter()

{ int sum=0;

while(*s)

{ sum+=*s;

s++;

return sum;
}

.
####Output :
Enter your word: MCA
Sum of ASCII value for the given word is=209

Enter your word: mca


Sum of ASCII value for the given word is=305
67 Created
by
Krishna
Pal
Programming
lab(C)

#38.Writeaprogrammetoacceptawordandprintitinreverse
forms.

.
####Programme :
/*Directives to include standard library and standard input-output header files in
this
program*/

#include<stdio.h>
#include<conio.h>
#include<string.h>

void strev(char*); // prototype declaration of strev ()


void main()
{ char s1[20];

clrscr();
printf("\nEnter a word:\t");
fflush(stdin);
gets(s1);
strev(s1); // calling strev ()
printf("\nReverse of the given word is:\t%s",s1);
getch();

void strev(char *s) // Defination of strev ()

{ char *t;
int i,j=0;
for(i=strlen(s)-1;i>=0;i--)

t[j++]=s[i];
t[j]='\0';
strcpy(s,t);

.
####Output :
Enter a word: Education
Reverse of the given word is: noitacudE

68 Created
by
Krishna
Pal
Programming
lab(C)

#39.Writeaprogrammetoacceptawordandcheckwhetherit
is
Palindrome
or
not.

.
####Programme :
#include<stdio.h>
#include<conio.h>
#include<string.h>

void main()

{ char s[20];

int i=0,j;

clrscr();

printf("\nEnter a word:\t");

fflush(stdin);

gets(s);

j=strlen(s)-1;

while(i<=j)

if(s[i++]!=s[j--])
break;
if(--i==++j)
printf("\nThe given word is Palindrome.");
else
printf("\nThe given word isn't Palindrome!");
getch();
}

.
####Output :
Enter a word: Malayalam
The given word is Palindrome.

Enter a word: Student


The given word isn't Palindrome!

69 Created
by
Krishna
Pal
Programming
lab(C)

#40.Writeaprogrammetoacceptasentenceandfindthe
occurrence
of
each
character..

.
####Programme :
#include<stdio.h>
#include<conio.h>

void main()

{
char s1[50];
int i,j,k=1,m,flag;
clrscr();
printf("\nEnter your sentence:\t");
fflush(stdin);
gets(s1);
printf("\nThe occurrence of\n");
for(i=0;s1[i]!='\0';i++)
{ m=i-1;

flag=0;

while(m>=0)
if(s1[i]==s1[m--]) // is the character was previously checked?
{ flag=1;

break;

}
k=1;
for(j=i+1;s1[j]!='\0';j++) // counting the occurrences

if(s1[i]==s1[j])

k++;
if(flag==0)
printf("\t%c is:\t%d\n",s1[i],k);

}
getch();
}

.
####Output :
Enter your sentence: I Love My India!

The occurrence of
I is: 2
is: 3
L is: 1

o is: 1
v is: 1
e is: 1
M is: 1
y is: 1
n is: 1
d is: 1
i is: 1
a is: 1
! is: 1
70
Created
by
Krishna
Pal
Programming
lab(C)

#41.Writeaprogrammetoacceptanameofpersonandprint
the
1st
letter
of
the
first
and
middles
(if
any)
and
the
title
as
it

is
in
examples:

Raja
Ram
Das
.
R.
R.
Das
or
Rahul
Ray
.
R.
Ray.

.
####Programme :
#include<stdio.h>
#include<conio.h>
#include<string.h>

void main()

{
char name[50],title[20],temp[30];
int i=0,j,k=0,m;
clrscr();
printf("\n\nEnter your full name\t:");
fflush(stdin);
gets(name);
j=strlen(name)-1;
temp[0]=name[0];
for(i=j;i>=0;i--)
{ if(name[i]==' ')

break;

title[k++]=name[i]; // copying title in the reverse order


}
j=0;
for(i=0;name[i]!='\0';i++)

if(name[i]==' ')

{ temp[++j]='.';
temp[++j]=' ';
temp[++j]=name[++i]; // taking the first letter of each word

}
for(i=k-1;i>=0;i--)

temp[j++]=title[i]; // copying the title in right order in temp


temp[j]='\0';
printf("\nYour short name is\t:%s",temp);
getch();

.
####Output :
Enter your full name :Hare Krishna Pal.
Your short name is :H. K. Pal.

Enter your full name :George Bush


Your short name is :G. Bush

Enter your full name :Einstein


Your short name is :Einstein

71
Created
by
Krishna
Pal
Programming
lab(C)

#42.Writeaprogrammetoacceptastringasacommandlone
argument
and
then
find
its
length.

.
####Programme :
#include<stdio.h>

/* taking parameter in the main() function as command line arguments */


void main(int argc,char *argv[])
{

int n=0;

char *p=argv[1];

if(argc==2)

{ while(*p)

{ n++; // finding the length of geven string

p++;

printf("\n\nThe length of the given string is: %d",n);

}
}

.
####Output :
Type EXIT to return to Turbo C++. . .Microsoft(R) Windows DOS
(C)Copyright Microsoft Corp 1990-2001.
C:\TC>lengthof Krishna
The length of the given string is: 7

C:\TC>lengthof Rcciit
The length of the given string is: 6
C:\TC>

72 Created
by
Krishna
Pal
Programming
lab(C)

#43.Writeaprogrammetoaccepttextfromkeyboardand
store
them
in
a
file
and
then
display
the
content
of
the
saved

file.

.
####Programme :
#include<stdio.h>
#include<conio.h>

void main()

{
FILE *fp; // declaration of file pointer
char c;
clrscr();
fp=fopen("fileinC.txt","w"); // opening a new file in write mode
if(fp==NULL) // checking for file opening error
{ printf("\n\nFile opening error!");

getch();

exit(0);
}
printf("\nEnter the text for file?\n[Press Ctrl+Z to terminate]\n");
while((c=getchar())!= EOF) //read character from keyboard

putc(c,fp); //put the entered character into file


fclose(fp); //close the file
fp=fopen("fileinC.txt","r"); //reopen the file in read mode
while((c=getc(fp))!= EOF) //reading single character from file until EOF found

printf("%c",c);
fclose(fp); //finally closing the file
getch();

.
####Output :
Enter the text for file?
[Press Ctrl+Z to terminate]
Name of College: Rcc Institute of Information Technology!
^Z
Name of College: Rcc Institute of Information Technology!

73 Created
by
Krishna
Pal
Programming
lab(C)

#44.Writeaprogrammetoaccept10students�namesand
marks
of
three
subjects
then
calculate
the
total
marks
and

store
them
in
a
file.

.
####Programme :
#include<stdio.h>
#include<conio.h>

struct student // Declaration of structure named student

{
char name[30];
int sub1,sub2,sub3,total;

};

void main()

{
struct student p,st[10],*s; // Declarations of array of structure, pointer to
structure
char c;
int i;
long n;
FILE *fp=fopen("fileinC.txt","w"); //opening a new file named fileinC.txt
if(fp==NULL) //checking for file opening error
{ printf("\n\nFile opening error!");

getch();

exit(0);
}
clrscr();
s=st;

/* store the records of 10 students on structure and then saved to file */


for(i=0;i<10;i++)
{ printf("\nEnter Name of %dth student[Single word]?",i+1);

fflush(stdin);
gets(s->name);
printf("\nEnter Marks of three subjects?");
scanf("%d%d%d",&s->sub1,&s->sub2,&s->sub3);
s->total = s->sub1+s->sub2+s->sub3;
fprintf(fp," %s %d %d %d %d",s->name,s->sub1,s->sub2,s->sub3,s->total);
s++;

}
n=ftell(fp); // store the position of last character in file to n
fclose(fp); //close the file
clrscr();
fp=fopen("fileinC.txt","r"); //reopen the file in read mode
printf("\n\nRecords on the saved file are:\n\n");
printf("Names\t\tSub1\tSub2\tSub3\tTotal\n");
printf("-----\t\t----\t----\t----\t-----\n");
while(ftell(fp)<n)
{ fscanf(fp,"%s %d %d %d %d",p.name,&p.sub1,&p.sub2,&p.sub3,&p.total);

printf("%-15s%5d%8d%8d%8d\n",p.name,p.sub1,p.sub2,p.sub3,p.total);
}
fclose(fp); //finally closing the file
getch();

74 Created
by
Krishna
Pal
Programming
lab(C)

.
####Output :
Enter Name of 1th student[Single word]?Krishna

Enter Marks of three subjects?60


75
80

Enter Name of 2th student[Single word]?Sudeshna

Enter Marks of three subjects?50


85
78

Enter Name of 3th student[Single word]?Rakesh

Enter Marks of three subjects?74


85
67

Enter Name of 4th student[Single word]?Nirmal

Enter Marks of three subjects?74


45
85

Enter Name of 5th student[Single word]?Koel

Enter Marks of three subjects?74


80
90

Enter Name of 6th student[Single word]?Ayan

Enter Marks of three subjects?70


60
75

Enter Name of 7th student[Single word]?Pratik

Enter Marks of three subjects?50


80
70

Enter Name of 8th student[Single word]?Debolina

Enter Marks of three subjects?74


50
80

Enter Name of 9th student[Single word]?Nupur


Enter Marks of three subjects?80
90
70

75 Created
by
Krishna
Pal
Programming
lab(C)

Enter Name of 10th student[Single word]?Rajat

Enter Marks of three subjects?75


85
60

Records on the saved file are:

Names Sub1 Sub2 Sub3 Total


-------
---
---
-----
Krishna 60 75 80 215
Sudeshna 50 85 78 213
Rakesh 74 85 67 226
Nirmal 74 45 85 204
Koel 74 80 90 244
Ayan 70 60 75 205
Pratik 50 80 70 200
Debolina 74 50 80 204
Nupur 80 90 70 240
Rajat 75 85 60 220

TheEndTheEndTheEndTheEnd
R
RRRRe
eeeem
mmmma
aaaar
rrrrk
kkkks
ssss:
::::----
S
SSSSi
iiiig
ggggn
nnnna
aaaat
ttttu
uuuur
rrrre
eeee:
::::
D
DDDDa
aaaat
tttte
eeees
ssss:
::::

76 Created
by
Krishna
Pal

Das könnte Ihnen auch gefallen