CP - Final Lab Manual Fh2023
CP - Final Lab Manual Fh2023
Sr.
Expt Course LAB
NO Expt.
. Name of the Experiment Outcom Outcom PO
. No.
No. e e
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 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.
5
13
A Write a C Program to print the given patterns after CO2 LO2
Sub In-charge
Prof. P.M. Manegopale Dr. A.Y.Shete
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.
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:
#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:
EXPECTED OUTPUT:
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
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
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:
Input:
enter the values of a,b,x
132
Output:
The value of s= 5
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.
If any of the conditional expression evaluates to true, then it will execute the corresponding code
block and exits whole if-else ladder.
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
Flowchart
Input Output
Enter the values of a,b and c
10
30
20
30 is greatest of 10 30 20
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:
Result:
Enter a,b& c: 1 5 3
Roots are real & unequal
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
Result:
Enter no of items: 5
The Fibonacci sequence is
01123
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
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
Input:
Enter a positive integer: 371
Output:
Department of Humanities and Basic science Subject: CP (FE SEM-II) FEL204
371 is an Armstrong number
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:
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
Flow chart:
Result:
Enter the limit: 4
The prime numbers are:
2357
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
Print “c”
break;
End
case 2:
Begin
Calculate c = a-b
Print “c”
break;
Flowchart
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
The addition of 20 and 16 is..36
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
}
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
Flowchart:
Output:
1. enter the two numbers whose gcd is to be found:5,25
GCD of a,b is : 5
Flow CHART:
Input:
Enter the two numbers whose gcd is to be found: 5 25
Output:
GCD of a,b is : 5
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 “*”.
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(" ");
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");
}
return 0;
}
Output:
Enter number of rows
5
*
***
*****
*******
*********
*******
*****
***
*
Output:
Enter no. of rows: 5
1
232
34543
4567654
567898765
Theory
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
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.
Flow Chart
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
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
Theory:
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();
}
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
Experiment No.16
Department of Humanities and Basic science Subject: CP (FE SEM-II) FEL204
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
Program:
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
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:
OUTPUT:
After addition of two matrices is..
234
567
8 9 10
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]
Step 6: For j varies from 1 to columns [B]
Step 7: initialize C[i,j]=0
Flowchart
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
Experiment No.20
Aim: Write a C program using user defined functions to determine
whether the given string is palindrome or not.
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:
Input:
Enter a string
madam
Output:
The length of the string 'madam' = 5
madam is a palindrome
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++;
}
return 0;
}
Input:
madam
Output:
String is palindrome