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

Innovative Engineers’ and Teachers’ Education Society’s

BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

INDEX
Course Name : C Programming Course Code :FEL 204FE SEM II

Sr.
LAB
NO. Expt. Expt. Course
Name of the Experiment Outco PO
No. No. Outcome
me
1 Write a C program to find the sum and average of
three numbers. Read the input data from the
A CO1 LO1
keyboard by using printf() and scanf() or I/O
1 function
2 To evaluate area of a triangle using the formula
B area=sqrt(s(s-a)(s-b)(s-c)) where a, b, c are the sides CO1 LO1
of the triangle and s=(a+b+c)/2
3
Write a C program to evaluate algebraic expression
A CO1 LO1
(ax+b)/(ax-b) using operator concept
2
4
Write a program to find greatest among 3 numbers
B CO2 LO2
using if else if statement
5
Write a C program to find the roots of a quadratic
A CO2 LO2
equation using if else if statement
6 i) Write a C program to display Fibonacci series
3 using for loop.
ii) Program to print Fibonacci series using do-while
B CO2 LO2
7 loop
To generate the first n terms of the Fibonacci
sequence using while loop
8
Write a C program to Check whether given number
A CO2 LO2
is Armstrong Number or Not using while loop.
4
9
Write a C program to find the sum of individual
B CO2 LO2
digits of a positive integer using while loop
10 5 Write a C program to generate all the prime numbers
A between 1 and n is a value supplied by the user using CO2 LO2
nested for loop.
11 B Write a C program to perform arithmetic calculator CO2 LO2
using switch statement. (Consider the operators +,-

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

,*,/,% and use Switch Statement.)


12
Write a C program to find the GCD of two given
A CO3 LO3
integers using non-recursive function
6 Write a C program to find the GCD of two given
B integers by using the recursive function CO3 LO3

13
A Write a C Program to print the given patterns after CO2 LO2
7 accepting number of rows from user.
14
Write a C Program to demonstrate Call by value
B CO3 LO5
mechanism using function.
15
Write a C program to find the factorial of a given
A CO3 LO3
integer using recursive function.
8
16
Write a C program to find both the largest and
B CO4 LO4
smallest number in a list of integers using 1D Array.
17
Write a C Program to Sort the Array in an Ascending
A CO4 LO4
Order.
9
18
Write a C program to perform addition of two
B CO4 LO4
matrices using 2D Array.
19
Program to print product of two matrices using 2D
A CO4 LO4
Array
10
20 Write a C program using user defined functions to
B determine whether the given string is palindrome or CO4 LO4
not.
21
C program to count digits, spaces, special characters,
11 CO4 LO4
alphabets in a string

Sub In-charge
H.O.D
Prof.Aditi Warange Applied Science and humanities

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

Experiment No.1
Aim: Write a C program to find the sum and average of three numbers. Read the
input data from the keyboard by using printf() and scanf() or I/O function
Introduction
C is a programming language developed at AT&T’s BELL Laboratory of USA in 1972. Dennis Ritchie
designed it. Because of its reliability. C is very popular. C is highly portable & it is well suited for
structured programming. C program consists of collection of functions.
Hardware Requirement: Desktop Computer / laptop computer
Software Requirement: Linux Operating System with GCC/ TURBOC IN WINDOWS OS
GCC
It is released by the Free Software Foundation. GCC is a Linux-based C compiler usually operated
via the command line. It often comes distributed with a linux installation, so if you are running Unix
or a Linux variant you likely have it on your system. You can invoke gcc on a source code file simply
by typing :-
Gcc filename
The default executable output of gcc is "a.out", which can be run by typing “. /a. Out”. It is also
possible to specify a name for the executable file at the command line by using the syntax.
-o output file, as shown in the following example : -
Gcc filename -o output file
Again, you can run your program with "./output file". (The. / is there to ensure you run the program
for the current working directory.)
Note: If you need to use functions from the math library (generally functions from math.h such as
sin or sqrt), then you need to explicitly ask it to link with that library with the -l flag and the
library 'm':
gcc filename -o outputfile–lm

Turbo C/C++
Open Turbo C from your Desktop or Programs menu. Select “File” from Menu bar and
select
option “New” and Save C program in filename .C extension.

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

To do compiling – Select -> Compile from menu and click-> compile.


If the compilation is success – you will see a “success” message. Else you will see the number of
errors.
To RUN the program – you may select ->Run from menu and click -> Run
Now you will see the output screen.

Program Description:
In this program, we have used the built-in print() and scanf() function are used to print the data on
the screen and read the data from the keyboard respectively.

Algorithm:
Step 1: Start
Step 2: Declare variables num1, num2, num3 and sum, average.
Step 3: Read values num1, num2, num3
Step 4: Add num1, num2, num3 and assign the result to sum.
sum num1+num2 +num3
Average sum/3
Step 5: Display sum and average
Step 6: Stop

Flow Chart:

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

Program:

#include<stdio.h>
int main( )
{
int a,b,c;
int sum,average;
printf("Enter any three integers: ");
scanf("%d%d %d", &a, &b, &c);
sum = a+b+c;
average = sum/3;
printf("Sum and average of three integers: %d %d",sum,average);
return 0;
}

SAMPLE INPUT:

Enter any three integers: 2 4 5

EXPECTED OUTPUT:

Sum and average of three integers: 11 3

Conclusion: The program is compiled, executed and the output is verified

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

Experiment No.2
Aim: To evaluate area of a triangle using the formula area=sqrt(s(s-
a)(s-b)(s-c)) where a,b,c are the sides of the triangle and s=(a+b+c)/2
Objectives: Study about basic building blocks such as constants, variables, keywords,
operators, expressions and input output statements in C language.
Tools/Equipments: Editors Visual studio with gcc or Turbo C editor in windows ,desktop or
laptop computer.

Algorithm
Step1:start
Step2:inputa,b,c
Step3:s=(a+b+c)/2
Step4:A=sqrt(s*(s-a)(s-b)(s-c))
Step5:PrintA
Step 5:stop

Flowchart

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

Procedure
Save program in a file, Compile program, debug errors, Execute or Run program with necessary
inputs. Verify the outputs obtained.

Program
/*To evaluate area of triangle (sqrt(s(s-a)(s-b)(s-c)*/
#include<math.h>
#include<stdio.h>
void main()
{
int a,b,c;
float s,area;
clrscr();
printf("enter the values of a,b,c");
scanf("%d%d%d",&a,&b,&c);
s=(a+b+c)/2.0;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("the area of a trangle is =%f",area);
getch();
}

Input Output
Ent er the values of a,b,c
15
20
30
The area of a triangle is = 133.317

Conclusion: The program is compiled, executed and the output is verified

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

Experiment No.3
Aim: Write a C program to evaluate algebraic expression (ax+b)/(ax-b)
using operator concept
Tools/Equipments: Editors like Visual studio with gcc or Turbo C editor in windows ,desktop or
laptop computer.

Algorithm:
Step 1: start
Step 2: input a, b, x, s
Step 3: s= (a*x+b)/(a*x-b)
Step 4: Result s
Step 5: stop

Flow Chart:

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

Program:
#include<stdio.h>
#include<conio.h>
int main( )
{
int a,b,x;
float s;
clrscr();
printf(“enter the values of a, b, x”);
scanf(“%d %d %d”, &a, &b, &x);
s=(a*x+b)/(a*x-b);

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

printf(“The value of s=%f ”,s);


getch();
return 0:
}

Input:
enter the values of a,b,x
132

Output:
The value of s= 5

Conclusion: The program is compiled, executed and the output is verified

Experiment No.4
Aim: Write a program to find greatest among 3 numbers using if else if
statement
Tools/Equipments: Editors Visual studio with gcc or Turbo C editor in windows, desktop or
laptop computer.

Theory :
If the Boolean expression evaluates to true, then the if block will be executed, otherwise, the else
block will be executed.
C programming language assumes any non-zero and non-null values as true, and if it is either
zero or null, then it is assumed as false value.
The if else ladder statement in C programming language is used to test set of conditions in
sequence. An if condition is tested only when all previous if conditions in if-else ladder is false.

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

If any of the conditional expression evaluates to true, then it will execute the corresponding code
block and exits whole if-else ladder.

Syntax
if(condition_expression_One) {
statement1;
} else if (condition_expression_Two) {
statement2;
} else if (condition_expression_Three) {
statement3;
} else {
statement4;
}

Algorithm
Step1: start
Step2: input a,b,c
Step3: if (a>b) and (a>c) go to step 4 otherwise go to step 5
Step4: display a is greater go to 8
Step5: if (b>c) go to step 6 otherwise step 7
Step 6: display b is greater, go to 8
Step 7: display c is greater
Step 8: stop

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

Flowchart

Procedure
Save program in a file, Compile program, debug errors, Execute or Run programwith necessary
inputs. Verify the outputs obtained.

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

Program
/*Program to find greatest among 3 numbers*/
void main()
{
int a,b,c;
clrscr();
printf("enter the values of a,b and c");
scanf("%d%d%d",&a,&b,&c);
if(a>b && a>c)
printf("%d is greatest of %d %d %d", a,a,b,c);
else
if(b>c)
printf("%d is greatest of %d %d %d",b,a,b,c);
else
printf("%d is gratest of %d %d %d",c,a,b,c);
getch();
return 0;
}

Input Output
Enter the values of a,b and c
10
30
20
30 is greatest of 10 30 20

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

Conclusion: The program is compiled, executed and the output is verified

Experiment No.5
Aim: Write a C program to find the roots of a quadratic equation using if
else if statement.
Tools/Equipments: Editors Visual studio with gcc or Turbo C editor in windows, desktop or
laptop computer.

Algorithm:
1. Read a,b,c values
2. Initialize d =b*b-4*a*c
3. if d==0
a. then print “roots are real and equal”
b. r1=-b/2*a,r2 =r1
4. else if d>0
a. then print “roots are real and distinct”
b. r1=(-b+sqrt(d))/2*a, r2=(-b-sqrt(d))/2*a
5. else if d<0
a. then print “roots are complex”
b. rp=-b/2a, imp=sqrt(d)/2*a
c. print r1 and r2.

Flow Chart:

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,d,r1,r2,imp,rp;
clrscr();
printf(“Enter a,b,c:”);
scanf(“%f%f%f”,&a,&b,&c);
d=b*b-4.0*a*c;
if(d= =0)
{
Printf(“roots are real and equal”);
r1=-b/2*a;
r2=r1;
printf(“root1=%f”,r1);
printf(“root2=%f”,r2);
}
else if(d>0)
{
Printf(“roots are real and unequal”);
r1=(-b+sqrt(d))/2*a;
r2=(-b-sqrt(d))/2*a;
printf(“root1=%f”,r1);
printf(“root2=%f”,r2);
}
else if(d<0)

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

{
d=-d;
printf(“roots are complex”);
rp=-b/2*a;
imp=sqrt(d)/2*a;
printf(“root1=%f+i%f”,rp,imp);
printf(“root2=%f-i%f”,rp,imp);
}
getch();
}

Result:
Enter a,b& c: 1 5 3
Roots are real & unequal

Conclusion: The program is compiled, executed and the output is verified

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

Experiment No.6
Aim: Write a C program to display Fibonacci series using for loop.
Tools/Equipments: Editors Visual studio with gcc or Turbo C editor in windows, desktop or
laptop computer.
Theory:-A Fibonacci sequence is defined as follows: the first and second terms in the sequence are
0 and 1. Subsequent terms are found by adding the preceding two terms in the sequence. Write a C
program to generate the first n terms of the sequence.

Algorithm:
1. Read the number of terms n
2. Initialize a 0, b 1
3. print a and b values
4. for i 3 to n
a. increment the i value
b. c a+b
c. print c value
d. a b
e. b c

Flow Chart

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a=0,b=1,c,n,i;
clrscr();
printf(“Enter no. of terms:”);
scanf(“%d”, &n);
printf(“The Fibonacci sequence is:”);
printf(“%d%d”, a,b);
for(i=3;i<=n;i++)
{
c=a+b;
printf(“%d”,c);
a=b;
b=c;
}
getch();
}

Result:
Enter no of items: 5
The Fibonacci sequence is
01123

Conclusion: The program is compiled, executed and the output is verified

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

Experiment No.7
Aim: To generate the first n terms of the Fibonacci sequence using while
loop
Tools/Equipments: Editors Visual studio with gcc or Turbo C editor in windows, desktop or
laptop computer..

Theory:
Fibonacci Sequence is defined as follows: the first and second terms in the sequence are 0 and 1.
Subsequent terms are found by adding the preceding two terms in the sequence
Initial Fibonacci numbers are 0 and 1. Next number can be generated by adding two numbers. So
0+1=1. Therefore next number can be generated by adding two previous .so Fibonacci series is 0 1
1 2 3 5 ……

ALGORITHM:
Step 1 : Start
Step 2 : Read n
Step 3 : Initialize f0 0, f1 1, f 0
Step 4 :i=0
Step 5 : while(i<=n) do as follows
printf("%d\t",f0);
f=f0+f1;
f0=f1;
f1=f;
i=i+1;
If not go to step 6
Step 6 : Stop

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

FLOWCHART:

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int f0,f1,f,n,i;
clrscr();
printf("ENTER THE VALUE FOR n \n");
scanf("%d",&n);
f0=0;
f1=1;
printf("FIBONACCI SEQUENCE FOR THE FIRST %d TERMS:\n",n);
i=0;
while(i<n)
{
printf("%d\t",f0);
f=f0+f1;
f0=f1;
f1=f;
i=i+1;
}
}
INPUT:
ENTER THE VALUE FOR n
10

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

OUTPUT:
FIBONACCI SEQUENCE FOR THE FIRST 10 TERMS:
0 1 1 2 3 5 8 13 21 34

Conclusion: The program is compiled, executed and the output is verified


Experiment No.8
Aim: Write a C program to check whether given number is Armstrong
Number or not using while loop.
Tools/Equipments: Editors Visual studio with gcc or Turbo C editor in windows, desktop or
laptop computer..
Theory:- A number is called Armstrong number when the sum of the nth power of each digit is
equal to the given number.

Algorithm:
Armstrong number
Step 1: start
Step 2:read n
Step 3:assign sum =0,I=m=n, count =0
Step 4:if m>0 repeat
Step 4.1:m=m/10
Step 4.2:count++
Step 4.3:until the condition fail
Step5: if I>0 repeat step 4 until condition fail
Step 5.1:rem=I%10
Step 5.2: sum=sum+ pow( rem, count)
Step 5.3: I=I/10
Step 6:if n=sum print Armstrong otherwise print not armstrong
Step 7:stop

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

FLOWCHART:

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

Program:
#include <stdio.h>
int main()
{
int n, n1, rem, num=0;
printf("Enter a positive integer: ");
scanf("%d", &n);
n1=n;
while(n1!=0)
{
rem=n1%10;
num+=rem*rem*rem;
n1/=10;
}
if(num==n)
printf("%d is an Armstrong number.",n);
else
printf("%d is not an Armstrong number.",n);
}

Input:
Enter a positive integer: 371

Output:
371 is an Armstrong number

Conclusion: The program is compiled, executed and the output is verified

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

Experiment No.9
Aim: Write a C program to find the sum of individual digits of a positive
integer using while loop.
Tools/Equipment: Editors Visual studio withgcc or Turbo C editor in windows ,desktop or
laptop computer.

Theory:
Sum of the individual digits means adding all the digits of a number.This sum of digits program
allows the user to enter any positive integer. Then c program will divide the given number into
individual digits and add those individuals (Sum) digits using While Loop.

Algorithm:
1. Readthenumbern

2. Initializesum=0

3. whilen>0

4. d=n%10
5. sum=sum+d
6. n=n/10
7.printsum.

Flowchart:

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int n, sum=0,d;
clrscr();
printf(“Enter any integer:”);
scanf(“%d”, &n);
while(n>0)
{
d=n%10;
sum=sum+d;
n=n/10;
}
Printf(“sum of individual digits is %d”,sum);
getch();
}

Result:
Enter any integer: 1234
Sum of individual digits is: 10

Conclusion: The program is compiled, executed and the output is verified

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

Experiment No.10
Aim: Write a C program to generate all the prime numbers between 1
and n is a value supplied by the user using nested for loop.
Tools/Equipment: Editors Visual studio with gcc or Turbo C editor in windows, desktop or
laptop computer.

Theory:
Definition of prime number:
A natural number greater than one which has not any other divisors except 1 and itself is called
prime number. In other word we can say which has only two divisors 1 and number itself. For
example: 5
Their divisors are 1 and 5.
Note: 2 is only even prime number.
Logic for prime number in c
We will take a loop and divide number from 2 to number/2. If the number is not divisible by any
of the numbers then we will print it as prime number.

Algorithm:
1. Read n value
2. Initialize count=0
3. for i ==2 to n
a. for j ==1 to i
b. if i mod j is equal to 0
c. then increment count
d. if count is equal to 2
e. then print i value.

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

Flow chart:

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

Program:
#incloude<stdio.h>
#Include<conio.h>
void main()
{
int i, j, n, count=0;
clrscr();
printf(“Enter the limit:”);
scanf(“%d”, &n);
printf(“The prime numbers are:”);
for(i=2;i<=n;i++)
{
for(j=1;j<=i;j++)
{
if(i%j==0)
count++;
}
if(count==2)
printf(“%d\t”, i);
}
getch();
}

Result:

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

Enter the limit: 4


The prime numbers are:
2357

Conclusion: The program is compiled, executed and the output is verified


Experiment No.11
Aim: Write a C program to perform arithmetic calculator using switch
statement. (Consider the operators +,-,*,/,% and use Switch Statement.)
Tools/Equipment: Editors Visual studio with gcc or Turbo C editor in windows, desktop or
laptop computer.

Theory:
Acalculator is a small electronic device used to perform various arithmetic operations like
addition, subtraction, multiplication, division, percentage, etc. It makes our calculations easier
and faster. It is a portable device that can use anywhere to perform simple mathematical
operations. We use a scientific or sophisticated calculator in some situations, where we need to
solve complex calculations like trigonometry functions, exponential operators, degrees, radians,
log functions, hyperbolic functions etc. Let's discuss the various ways to create a calculator
program in the C language.

Algorithm:
Step 1: Read a,b
Step 2: Print “Menu Options”
Step 3: while Begin
Step 4: Read ch
Step 5: switch(ch)
Begin
Step 6:
case 1:
Begin
Calculate c = a+b

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

Print “c”
break;
End
case 2:
Begin
Calculate c = a-b
Print “c”
break;
End
case 3:
Begin
Calculate c = a*b
Print “c”
break;
End
case 4:
Begin
Calculate c = a/b
Print “c”
break;
End
case 5:
Begin
Calculate c = a%b
Print “c”
break;
End
default:
Print “Invalid choice”

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

End

Flowchart

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
inta,b,c,ch; clrscr();
printf("ENTER TWO VALUES FOR a & b\n"); scanf("%d %d",&a,&b);
while(1) {
printf("MENU OPTIONS \n");
printf("************\n");
printf("1.Addition\n");
printf("2.Subtraction\n");
printf("3.Multiplication\n");
printf("4.Division\n");

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

printf("5.Modulus\n");
printf(“6.Exit\n”);
printf("\n");
printf("ENTER UR CHOICE\n");
scanf("%d",&ch);
switch(ch)
{
case 1: c=a+b;
printf("The addition of %d and %d is..%d\n",a,b,c); break;
case 2: c=a-b;
printf("The subtraction of %d and %d is..%d\n",a,b,c); break;
case 3: c=a*b;
printf("The multiplication of %d and %d is..%d\n",a,b,c); break;
case 4: c=a/b;
printf("The division of %d and %d is..%d\n",a,b,c); break;
case 5: c=a%b;
printf("The modulus of %d and %d is..%d\n",a,b,c); break;
case 6:exit(0); default:printf("INVALID CHOICE\n"); }
}
getch();
}

INPUT:
ENTER TWO VALUES FOR a & b: 20 16

OUTPUT:
MENU OPTIONS
1.Addition 2.Subtraction 3.Multiplication 4.Division 5.Modulus
6.Exit
ENTER UR CHOICE 1

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

The addition of 20 and 16 is..36

Conclusion: The program is compiled, executed and the output is verified

Experiment No.12
Part A
Aim: Write a C program to find the GCD of two given integers using non-
recursive function or function.
Tools/Equipment: Editors like gedit, vs editor in linux with gcc (or TurboC editor in
windows) in desktop or laptop computer.

Theory
A function is a group of statements that together perform a task. Every C program has at least one
function, which is main(), and all the most trivial programs can define additional functions.
You can divide up your code into separate functions. How you divide up your code among different
functions is up to you, but logically the division is such that each function performs a specific task.
A function declaration tells the compiler about a function's name, return type, and parameters.
A function definition provides the actual body of the function.
The C standard library provides numerous built-in functions that your program can call. For
example, strcat() to concatenate two strings, memcpy() to copy one memory location to another
location, and many more functions.
A function can also be referred as a method or a sub-routine or a procedure, etc.

Defining a Function
The general form of a function definition in C programming language is as follows:-
return_typefunction_name( parameter list )
{
body of the function
}

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

A function definition in C programming consists of a function header and a function body. Hereare all
the parts of a function

Description:
GCD means Greatest Common Divisor. i.e the highest number which divides the given number
Ex: GCD(12,24) is 12
Formula: GCD= product of numbers/ LCM of numbers

Algorithm:
Step 1: start
Step 2: read a,b
Step 3: call sub program g=GCD(a,b)
Step 4: print the g value
Step 5: stop
Sub program:
Step 1: initialize the p=1, q, remainder
Step 2: remainder=p-(p/q*q)
Step 3: remainder=0 return q else goto step 4
Step 4: GCD(q,remainder) return to main program

Flowchart:

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
intgcdnonrecursive(intm,int n)
{
int remainder;
remainder=m-(m/n*n);
if(remainder==0)
return n;
else
gcdnonrecursive(n,remainder);

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

}
void main()
{
inta,b,igcd;
clrscr();
printf("enter the two numbers whose gcd is to be found:");
scanf("%d%d",&a,&b);
printf("GCD of %d",gcdnonrecursive(a,b));
getch();
}

Output:
1. enter the two numbers whose gcd is to be found:5,25
GCD of a,b is : 5

Conclusion: The program is compiled, executed and the output is verified

Part B
Aim: Write a C program to find the GCD of two given integers by using
the recursive function
Algorithm:
Main program:
Step 1: start
Step 2: read a,b
Step 3: call the sub program GCD(a,b) for print the value
Step 4: stop
Sub program: GCD(n,m)
Step 1: if n>m return GCD(n,m)

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

Step 2: if n==0 return m else goto step 3


Step 3: return GCD (n,m%n)
Step 4: return to main program

Flow CHART:

Program:
#include<stdio.h>
#include<conio.h>
intgcdrecursive(intm,int n)
{
if(n>m)
returngcdrecursive(n,m);
if(n==0)
return m;
else

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

returngcdrecursive(n,m%n); // return to the main program


}
void main()
{
Int a,b,I gcd; clrscr();
printf("enter the two numbers whose gcd is to be found:");
scanf("%d%d",&a,&b);
printf("GCD of a,b is %d",gcdrecursive(a,b)); // return to the sub program getch();
}

Input:
Enter the two numbers whose gcd is to be found: 5 25

Output:
GCD of a,b is : 5

Conclusion: The program is compiled, executed and the output is verified

Experiment No.13
Aim: Write a C Program to print the given patterns after accepting
number of rows from user.
Tools/Equipment: Editors Visual studio with gcc or Turbo C editor in windows, desktop or
laptop computer.

Algorithm:
1. Take the number of rows as input and store in the variable number.
2. Firstly decrement the variable number by 1 and assign this value to the variable count.
3. Use this variable count as terminator in the for loop to print ” “.
4. Decrement count by 1.
5. Use another for loop starting from 1 to (2*k-1) to print “*”.

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

6. Do steps 3, 4, and 5 inside the for loop starting from 1 to variable number.
7. Steps 2-6 are used to print half of the diamond pattern.
8. For the next half, assign the variable count by 1.
9. Use this variable count as terminator in the for loop to print ” “.
10. Increment count by 1.
11. Use another for loop starting from 1 to (2*(number-k)-1) to print “*”.
12. Do steps 8-11 inside the for loop starting from 1 to value (number-1).

Program:
1) C Program to Print Diamond Pattern

#include <stdio.h>
int main()
{
int n, c, k;
printf("Enter number of rows\n");
scanf("%d", &n);
for (k = 1; k <= n; k++)
{
for (c = 1; c <= n-k; c++)
printf(" ");
for (c = 1; c <= 2*k-1; c++)
printf("*");

printf("\n");
}
for (k = 1; k <= n - 1; k++)
{
for (c = 1; c <= k; c++)
printf(" ");
for (c = 1 ; c <= 2*(n-k)-1; c++)
printf("*");
printf("\n");

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

}
return 0;
}
Output:
Enter number of rows
5
*
***
*****
*******
*********
*******
*****
***
*

Conclusion: The program is compiled, executed and the output is verified

2) C Program to Print Number Pyramid


#include<stdio.h>
int main()
{
intn,i,j,ele;
printf("Enter no. of rows: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

//P1 start here


//first we print spaces n-i times
for(j=1;j<=n-i;j++)
{
printf(" ");
}
//then we print elements starting from i to 2*i-1, which can be observed in each row of the pattern.
for(j=i;j<2*i;j++)
{
printf("%d ", j);
}
//P2 starts here
//each row starts with 2*(i-1) and then decrease i-1 times
ele=2*(i - 1);
for(j=1;j<=i-1;j++)
{
printf("%d ", ele--);
}
//we finish each row and then print a new line
printf("\n");
}
return 0;
}

Output:
Enter no. of rows: 5
1
232
34543
4567654

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

567898765

Conclusion: The program is compiled, executed and the output is verified

Experiment No.14
Aim: Write a C Program to demonstrate Call by value mechanism using
function.
Tools/Equipment: Editors Visual studio with gcc or Turbo C editor in windows, desktop or
laptop computer.

Theory

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

A function is a group of statements that together perform a task.


Function call by value is the default way of calling a function in C programming. Before we discuss
function call by value

Actual parameters: The parameters that appear in function calls.


Formal parameters: The parameters that appear in function declarations

Call by value

Call by value in C
o In call by value method, the value of the actual parameters is copied into the formal
parameters. In other words, we can say that the value of the variable is used in the function
call in the call by value method.
o In call by value method, we cannot modify the value of the actual parameter by the formal
parameter.
o In call by value, different memory is allocated for actual and formal parameters since the
value of the actual parameter is copied into the formal parameter.
o The actual parameter is the argument which is used in the function call whereas formal
parameter is the argument which is used in the function definition.
o When we pass the actual parameters while calling a function then this is known as function
call by value. In this case the values of actual parameters are copied to the formal
parameters. Thus operations performed on the formal parameters don’t reflect in the actual
parameters

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

Call by Value Example: Swapping the values of the two variables


As mentioned above, in the call by value the actual arguments are copied to the formal arguments,
hence any operation performed by function on arguments doesn’t affect actual parameters.

Why variables remain unchanged even after the swap?


The reason is same – function is called by value for num1 & num2. So actually var1 and var2 gets
swapped (not num1 & num2). As in call by value actual parameters are just copied into the formal
parameters.

Flow Chart

Program:
#include <stdio.h>
void swap(int , int); //prototype of the function
int main()
{
int a = 10;

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
swap(a,b);

printf("After swapping values in main a = %d, b = %d\n",a,b);


}
void swap (int a, int b)
{
int temp;
temp = a;
a=b;
b=temp;
printf("After swapping values in function a = %d, b = %d\n",a,b); // Formal parameters, a =
20, b = 10
}

Output
Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 10, b = 20

Conclusion: The program is compiled, executed and the output is verified

Call by reference Example: Swapping the values of the two


variables
Program:
#include <stdio.h>
void swap(int *, int *); //prototype of the function
int main()

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
swap(&a,&b);
printf("After swapping values in main a = %d, b = %d\n",a,b);
}
void swap (int *a, int *b)
{
int temp;
temp = *a;
*a=*b;
*b=temp;
// Formal parameters, a = 20, b = 10
printf("After swapping values in function a = %d, b = %d\n",*a,*b);
}

Output
Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 20, b = 10

Conclusion: The program is compiled, executed and the output is verified


Experiment No.15
Aim: Write a C program to find the factorial of a given integer using
recursive function.
Tools/Equipment: Editors Visual studio with gcc or Turbo C editor in windows, desktop or
laptop computer

Theory:

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

Recursion is the process of repeating items in a self-similar way. In programming languages,if a


program allows you to call a function inside the same function, then it is called a recursivecall of the
function.
void recursion() {
recursion(); /* function calls itself */
}
int main() {
recursion();
}
The C programming language supports recursion, i.e., a function to call itself. But while using
recursion, programmers need to be careful to define an exit condition from the function; otherwise
it will go into an infinite loop. Recursive functions are very useful to solve many mathematical
problems, such as calculating the factorial of a number, generating Fibonacci series, etc.

ALGORITHM:
main program
Step 1: start
Step 2: read n
Step 3: call sub program as f=fact (n)
Step 4: print f value
Step 5: stop
Sub program:
Step 1: initialize the f
Step 2: if n= = 0 or n == 1 return 1 to main program if not goto step 3
Step 3: return n*fact (n-1) to main program

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

PROGRAM:
#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
intn,res;
clrscr();
printf("ENETR A NUMBER:\n");
scanf("%d",&n);
res=fact(n);

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

printf("THE FACTORIAL OF A GIVEN NUMBER IS..%d",res);


getch();
}
int fact(int n)
{
int r;
if(n==0)
return(1);
else
{
r=n*fact(n-1);
return(r);
}
}
INPUT:
ENTER A VALUE FOR n
5
OUTPUT:
THE FACTORIAL OF A GIVEN NUMBER IS..120

Conclusion: The program is compiled, executed and the output is verified

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

Experiment No.16
Aim: Write a C program to find both the largest and smallest number in
a list of integers using 1D Array.
Tools/Equipment: Editors Visual studio with gcc or Turbo C editor in windows, desktop or
laptop computer.

ALGORITHM:
Step 1: start
Step 2: read n
Step 3: initialize i=0
Step 4: if i<n do as follows. If not, goto step 5
Read a[i]
Increment i
Goto step 4
Step 5: small=a[0], large=a[0]
Step 6: initialize i=0
Step 7: if i<n do as follows. Ifnot, goto step 8
If a[i]<small
Assign small=a[i]
If a[i]>large
Assign large=a[i]
Increment i goto Step 7
Step 8: print small, large
Step 9: stop

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

Flow Chart:

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

Program:
#include<stdio.h>
#include<conio.h>
void main()
{ int a[10],i,n,small,large;
clrscr();
printf("Enter The Array Size:");
scanf("%d",&n);
printf("ENTER ELEMENTS OF ARRAY");
for(i=0;i<n;i++) // read the elements of an array
scanf("%d",&a[i]);
small=a[0];
large=a[0];
for(i=0;i<n;i++)// read the elements of an array
{ if(a[i]<small)// check the condition for minimum value
small=a[i];
if(a[i]>large)//check the condition for maximum value
large=a[i];
}
printf("largest value is:%d\n",large);
printf("smallest value is:%d\n",small);
getch();
}
INPUT:
Enter The Array Size:10
ENTER THE ELEMENTS OF ARRAY
7 10 9 8 6 5 2 3 4 1
OUTPUT: largest value is : 10

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

smallest value is : 1
Conclusion: The program is compiled, executed and the output is verified

Experiment No.17
Aim: Write a C Program to Sort the Array in an Ascending Order.
Tools/Equipment: Editors Visual studio with gcc or Turbo C editor in windows, desktop or
laptop computer.
Program:
C Program to Sort the Array in an Ascending Order
#include <stdio.h>
void main()
{
int i, j, a, n, number[30];
printf("Enter the value of N \n");
scanf("%d", &n);
printf("Enter the numbers \n");
for (i = 0; i < n; ++i)
scanf("%d", &number[i]);
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (number[i] > number[j])
{
a = number[i];
number[i] = number[j];
number[j] = a;
}
}

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

}
printf("The numbers arranged in ascending order are given below \n");
for (i = 0; i < n; ++i)
printf("%d\n", number[i]);
}

Output:
Enter the value of N
6
Enter the numbers
3
78
90
456
780
200
The numbers arranged in ascending order are given below
3
78
90
200
456
780

Conclusion: The program is compiled, executed and the output is verified

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

Experiment No.18
Aim: Write a C program to perform addition of two matrices using 2D
Array.
Tools/Equipment: Editors Visual studio with gcc or Turbo C editor in windows, desktop or
laptop computer.

ALGORITHM:
Step 1: Start
Step21: for i is 0 to 2 by step 1
for j is 0 to 2 by step 1
Step 3: Read a[i][j],b[i][j]
Step 4: goto step 2
Step 5: calculate c[i][j]=a[i][j]+b[i][j]
Step 6: goto step 2
Step 7: Print c[i][j]
Step 8: Stop

Flow Chart:

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3];
inti,j;
clrscr();
printf("ENTER A MATRIX\n");
for(i=0;i<3;i++)
{

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
}
printf("ENTER B MATRIX\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&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(" After addition of two matrices :\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

INPUT:
ENTER a MATRIX
123
456
789
ENTER b MATRIX
111
111
111

OUTPUT:
After addition of two matrices is..
234
567
8 9 10

Conclusion: The program is compiled, executed and the output is verified

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

Experiment No.19
Aim: Program to print product of two matrices using 2D Array
Objectives: Study about arrays-two dimensional - in C language.
Tools/Equipments: Editors like gedit, vs editor in linux with gcc (or Turbo C editor in windows) in
desktop or laptop computer.

THEORY
The simplest form of multidimensional array is the two-dimensional array. A two-dimensional
array is, a list of one-dimensional arrays. To declare a two-dimensional integer array of size[x][y],
you would write something as follows :-
typearrayName [ x ][ y ];
Where type can be any valid C data type and arrayName will be a valid C identifier. A two
dimensional array can be considered as a table which will have x number of rows and y number of
columns.

Fig : A 3 x 4 Table

Algorithm
Step 1: start
Step 2: Read rows and columns of two matrices
Step 3: Read elements of first and second matrix to arrays A[ ][ ] and B[ ][ ]
Step 4: if columns [A] ≠ rows [B]
Then error "incompatible dimensions – Multiplication Not possible"
Repeat step 2 if required.
Step 5: For i varies from 1 to rows [A]

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

Step 6: For j varies from 1 to columns [B]


Step 7: initialize C[i,j]=0
Step 8: For k varies from 1 to columns [A]
Step 9: C[i, j]=C[i, j]+A[i, k]*B[k, j] , Repeat 5to 9 till the end of loops.
Step 10:Print result as C[ ][ ]
Step 11: Stop

Flowchart

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

Procedure:

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

Save program in a file, Compile program, debug errors, Execute or Run programwith necessary
inputs. Verify the outputs obtained.

Program:
/* Program to print product of two matrices*/
#include <stdio.h>
int main()
{
int m, n, p, q, i, j, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
printf("Enter the number of rows and columns of first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the number of rows and columns of second matrix\n");
scanf("%d%d", &p, &q);
if (n != p)
printf("Matrices with entered orders can't be multiplied with each other.\n");
else
{ printf("Enter the elements of first matrix\n");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", &first[i][j]);
printf("Enter the elements of second matrix\n");
for (i = 0; i < p; i++)
for (j = 0; j < q; j++)
scanf("%d", &second[i][j]);
for (i = 0; i < m; i++) {
for (j = 0; j < q; j++) {
multiply[i][j]=0;
for (k = 0; k < p; k++) {
multiply[i][j] = multiply[i][j] + first[i][k]*second[k][j];

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

}
}
}
printf("Product of entered matrices:-\n");
for (i = 0; i < m; i++) {
for (j = 0; j < q; j++)
printf("%d\t", multiply[i][j]);
printf("\n");
}
}
return 0;
}

Input Output
Enter the number of rows and columns of first matrix 3 3
Enter the number of rows and columns of second matrix 3 3
Enter the elements of first matrix
124521452
the elements of second matrix
124521452
Product of entered matrices
10 18 28
50 18 7
40 45 14

Conclusion: The program is compiled, executed and the output is verified

Experiment No.20

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

Aim: Write a C program using user defined functions to determine


whether the given string is palindrome or not.
Tools/Equipment: Editors Visual studio with gcc or Turbo C editor in windows, desktop or
laptop computer.

Theory :
Palindrome means string on reversal should be same as original
Ex: madam on reversal is also madam

Algorithm:
Step 1: start
Step 2: read string A
Step 3: copy string A into B
Step 4: reverse string B
Step 5: compare A &B
If A equals B to got step 6
Else go to step 7
Step 6:print given string A is palindrome
Step 7:print given string is not palindrome
Step 8: stop

Flow Chart:

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

Program:
#include <stdio.h>
#include <string.h>
void main()
{
char string[25], reverse_string[25] = {'\0'};
int i, length = 0, flag = 0;
printf("Enter a string \n");
gets(string);

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

for (i = 0; string[i] != '\0'; i++)


{
length++;
}
printf("The length of the string '%s' = %d\n", string, length);
for (i = length - 1; i >= 0 ; i--)
{
reverse_string[length - i - 1] = string[i];
}
for (flag = 1, i = 0; i < length ; i++)
{
if (reverse_string[i] != string[i])
flag = 0;
}
if (flag == 1)
printf ("%s is a palindrome \n", string);
else
printf("%s is not a palindrome \n", string);
}

Input:
Enter a string
madam

Output:
The length of the string 'madam' = 5
madam is a palindrome

Conclusion: The program is compiled, executed and the output is verified

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

Experiment No.21
Aim: C program to count digits, spaces, special characters, alphabets in a
string
Tools/Equipment: Editors Visual studio with gcc or Turbo C editor in windows, desktop or
laptop computer.

Program:
#include <stdio.h>
int main()
{
charstr[100];
intcountDigits,countAlphabet,countSpecialChar,countSpaces;
int counter;
//assign all counters to zero
countDigits=countAlphabet=countSpecialChar=countSpaces=0;
printf("Enter a string: ");
gets(str);
for(counter=0;str[counter]!=NULL;counter++)
{
if(str[counter]>='0' &&str[counter]<='9')
countDigits++;
else if((str[counter]>='A' &&str[counter]<='Z')||(str[counter]>='a' &&str[counter]<='z'))
countAlphabet++;
else if(str[counter]==' ')
countSpaces++;
else
countSpecialChar++;

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

printf("\nDigits: %d \nAlphabets: %d \nSpaces: %d \nSpecial Characters: %d", count


Digits, count Alphabet, countSpaces, countSpecial Char);

return 0;
}

Input:
madam

Output:
String is palindrome

Conclusion: The program is compiled, executed and the output is verified

Applied Science and humanities Subject: CP (FE SEM-II) FEL204


Innovative Engineers’ and Teachers’ Education Society’s
BHARAT COLLEGE OF ENGINEERING, BADLAPUR (W)

Applied Science and humanities Subject: CP (FE SEM-II) FEL204

You might also like