Semester2/C-Lang.
/Practical Experiments Page 1 of 7
INDEX 1. FINDING AREA OF TRIANGLE
Aim: A program to find area of triangle by giving three sides.
Variables Used: a, b, c, s, area
SNO NAME OF EXPERIMENT DATE
1. Finding Area of Triangle Algorithm:
Step 1: Start
2. Generating First ‘n’ terms of sequence Step 2: Read a, b, c
3. Generating Fibonacci series a+b+c
Step 3: Let s =
4. Sum of digits of a number 2
5. Finding Armstrong Number Step 4: Let area = s ( s − a)( s − b)( s − c)
6. Finding perfect number Step 5: Display area
7. Generating list of prime numbers Step 6: Stop
8. Finding factorial using recursion PROGRAM:
9. Finding Largest and Smallest numbers
/* Program to find area of a triangle */
10. Perform various string operations #include <stdio.h>
11. Student Result Processing using structures #include <conio.h>
#include <math.h>
12. Matrix Multiplication main()
{
float a,b,c,s,area;
clrscr( );
printf("Enter a,b,c values ");
scanf("%f%f%f",&a,&b,&c);
s = (a+b+c)/2;
area = sqrt(s*(s-a)*(s-b)*(s-c));
printf("Area of triangle = %f ",area);
}
OUTPUT:
I BSc/Practical Experiments Page 2 of 7
2. GENERATING FIRST ‘N’ TERMS OF SEQUENCE 3. GENERATING FIBONACCI SERIES
Aim: A program to generate first ‘n’ terms of sequence
Aim: A program to generate Fibonacci sequence
Variables Used: n, a
Variables used: n, a, b, c, i
Algorithm:
Algorithm:
Step 1: Start
Step 1: Start
Step 2: Read n
Step 2: Read n
Step 3: Let a=0
Step 3: Let a=1
Step 4: Let b=1
Step 4: Repeat while a<=n
Step 5: Display a, b
Display a
Step 6: Repeat for i = 2 to n
Let a=a+1
Let c = a + b
End while
display c
Step 5: Stop
Let a=b
Let b=c
PROGRAM:
Endfor
Step 7: Stop
/* Program to first n terms of sequence*/
PROGRAM:
#include <stdio.h>
/* Program to generate Fibonacci series */
#include <conio.h>
#include <stdio.h>
main()
#include <conio.h>
{
main( )
int a, n;
{
clrscr();
int n, a, b, c, i;
printf("Enter n value ");
clrscr();
scanf("%d",&n); printf("Enter n value ");
a=1;
scanf("%d",&n);
while (a<=n)
a=0;
{ printf(“%d \t”,a); b=1;
a++; printf(“Fibonacci Series …\n”);
}
printf(“%d \t %d”,a,b);
}
for(i=2;i<=n;i++)
{
OUTPUT:
c=a+b;
printf("\t %d”,c);
a=b;
b=c;
}
}
OUTPUT:
Semester2/C-Lang./Practical Experiments Page 3 of 7
4. FINDING SUM OF DIGITS OF A NUMBER 5. FINDING ARMSTRONG NUMBER
Aim: A program to find the sum of individual digits of a Aim: A program to check whether the given number is Armstrong or not.
number. Def: “If the sum of cubes of digits of a number is equal to that number
Variables Used: num, d, sum then it is called Armstrong number”. Eg.: 1, 153, 370 etc.
Algorithm: Variables Used: num, d, sum, temp
Step 1: Start Algorithm:
Step 2: Read num Step 1: Start
Step 3: sum=0 Step 2: Read num
Step 4: Repeat while num > 0 Step 3: temp = num
Let d=num%10 Step 4: sum=0
Let sum=sum+d Step 5: Repeat while num>0
Let num=num/10 Let d=num%10
End while Let sum=sum+d*d*d
Step 5: Stop Let num=num/10
End while
PROGRAM: Step 6: If sum=temp then
/* Program to find sum of digits of a number */ display “Armstrong”
#include <stdio.h> else
#include <conio.h> display “Not Armstrong”
main( ) Endif
{ Step 7: Stop
int num, d, sum;
clrscr(); PROGRAM:
printf("Enter a number "); /* Program to check Armstrong number or not */
scanf("%d",&num); #include <stdio.h>
sum = 0; #include <conio.h>
while (num > 0) main( )
{ {
d = num % 10; int num, d, temp, sum;
sum = sum + d; clrscr();
num = num / 10; printf("Enter a number ");
} scanf("%d",&num);
printf(“Sum of digits=%d”,sum); temp = num;
} sum = 0;
while (num>0)
OUTPUT: {
d = num % 10;
sum = sum + d * d * d;
num = num / 10;
}
if (sum = = temp)
printf(“Armstrong Number");
else
printf("Not Armstrong");
}
OUTPUT:
I BSc/Practical Experiments Page 4 of 7
6. FINDING PERFECT NUMBER 7. GENERATING LIST OF PRIME NUMBERS
Aim: A program to check whether the given number is Perfect or not. Aim: A program to generate list of prime numbers
Def: “If the sum of factors of a number is double to the number then it Variables Used: n, num, a, c
is called Perfect number”. Eg.: 6, 28 etc.
Variables Used: num, sum, a Algorithm:
Step 1: Start
Algorithm: Step 2: Read n
Step 1: Start Step 3: Repeat for num=1 to n
Step 2: Read num c=0
Step 3: sum=0 Repeat for a=1 to num
Step 4: Repeat for a=1 to num If (num%a = = 0) then c=c+1
If (num%a = = 0) then sum=sum+a Endfor
Endfor if c=2 then display num
Step 5: if sum=2*num then Endfor
display “Perfect Number” Step 4: Stop
else
display “Not Perfect” PROGRAM:
endif /* Program to generate a list of prime numbers */
Step 6: Stop #include <stdio.h>
#include <conio.h>
PROGRAM: main( )
/* Program to check whether Perfect number or not */ {
#include <stdio.h> int n, num, a, c;
#include <conio.h> clrscr();
main( ) printf("Enter n value ");
{ scanf("%d",&n);
int num, sum, a; printf(“Prime numbers are ...\n”);
clrscr(); for(num=1;num<=n;num++)
printf("Enter a number "); {
scanf("%d",&num); c=0;
sum=0; for(a=1;a<=num;a++)
for(a=1;a<=num;a++) {
{ if (num % a = = 0)
if (num % a = = 0) c = c + 1;
sum = sum + a; }
} if (c = = 2)
if (sum = = num) printf(“%d \t”,num);
printf(“Perfect Number"); }
else }
printf("Not Perfect");
}
OUTPUT:
OUTPUT:
Semester2/C-Lang./Practical Experiments Page 5 of 7
8. FINDING FACTORIAL USING RECURSION 9. FINDING LARGEST NUMBER
AIM: A program to find factorial of a number using recursive Aim: A program to find largest in the list of numbers
function Variables Used: n, i, large
VARIABLES USED: num, f Algorithm:
ALGORITHM: Step 1: Start
Step 1: Start Step 2: Read n
Step 2: Read num Step 3: Repeat for i = 1 to n do
Step 3: Let f = fact(num) /* Calling fact() function */ Read a[i]
Step 4: Display f End for
Step 5: Stop Step 4: Let large = a[1]
ALGORITHM: FACT(N) Step 5: Repeat for i = 1 to n do
Step 1: If (n<=1) then If (a[i]>large) then large = a[i]
Return(1) Endfor
Else Step 6: Display large
Return(n*fact(n-1)) Step 7: Stop
Endif
PROGRAM: PROGRAM:
/* Program to find factorial of a number using recursive /* Program to find largest and smallest numbers */
function */ #include <stdio.h>
#include <stdio.h> #include <conio.h>
#include <conio.h> main()
long int fact(int n) {
{ int a[100], n, i, j, large;
if (n<=1) clrscr();
return(1); printf("How many values? ");
else scanf("%d",&n);
return(n*fact(n-1)); printf("Enter %d values \n", n);
} for(i=1;i<=n;i++)
main() scanf("%d",&a[i]);
{ large = a[1];
int num; for(i=2;i<=n;i++)
long int f; {
clrscr(); if (a[i]>large)
printf("Enter a number "); large = a[i];
scanf("%d",&num); }
f = fact(num); printf(“Largest Number =%d”,large);
printf("Factorial = %ld",f); }
}
OUTPUT:
OUTPUT:
I BSc/Practical Experiments Page 6 of 7
10. PERFORM VARIOUS STRING OPERATIONS 11. STUDENT RESULT PROCESSING USING STRUCTURES
Aim: A program to find result of student using structures
Aim: A program to perform various string operations
Variables used: htno, sub1, sub2, sub3, tot, avg of structure st
Variables Used: str, n
Algorithm:
Algorithm:
Step 1: Start
Step 1: Start
Step 2: Read str
Step 2: Read htno, sub1, sub2, sub3
Step 3: n = strlen(str)
Step 3: Let tot = sub1+ sub2+ sub3
Step 4: Display n
Step 4: Let avg = tot / 3.0
Step 5: strupr(str)
Step 5: if (sub1>=35 and sub2>=35 and sub3>=35) then
Step 6: Display str
Display “Result: Pass”
Step 7: strrev(str)
Else
Step 8: Display str
Display ”Result: Fail”
Step 9: Stop
Endif
Step 6: Stop
PROGRAM:
PROGRAM:
/* Program to perform various operations on a string */
/* Program to find result of student using structures */
#include<stdio.h>
#include <stdio.h>
#include<conio.h>
#include <conio.h>
#include<string.h>
main( )
main()
{
{
struct student
char str[100];
{
int n;
long int htno;
clrscr();
int sub1, sub2, sub3, tot;
printf("Enter a string :");
float avg;
gets(str);
} st;
n = strlen(str);
clrscr( );
printf(“Length of string is %d”,n); printf("Enter Hall ticket number ");
strupr(str);
scanf("%ld",&st.htno);
printf("Uppercase string is %s”,str); printf("Enter Marks in 3 subjects ");
strrev(str);
scanf("%d %d %d",&st.sub1, &st.sub2, &st.sub3);
printf("Reversed string is %s”,str); st.tot=st.sub1+st.sub2+st.sub3;
} st.avg=st.tot/3.0;
printf("\n Total Marks = %d",st.tot);
OUTPUT:
printf("\n Average = %f",st.avg);
if (st.sub1>=35 && st.sub2>=35 && st.sub3>=35)
printf("\n Result: Pass");
else
printf("\n Result: Fail");
}
OUTPUT:
Semester2/C-Lang./Practical Experiments Page 7 of 7
12. MATRIX MULTIPLICATION
PROGRAM:
Aim: A program to find multiplication of two matrices
/* Program to find multiplication of two matrices */
Variables Used: a, b, c, i, j, k
#include<stdio.h>
#include<conio.h>
Algorithm:
main( )
Step 1: Start
{
Step 2: [Reading first matrix values in 2x2 order]
int a[10][10],b[10][10],c[10][10]={0}, i, j, k;
Repeat for i=1 to 2 do
clrscr( );
Repeat for j=1 to 2 do
printf("Enter 1st matrix values in 2x2 order \n");
Read a[i][j]
for(i=1;i<=2;i++)
Endfor
for(j=1;j<=2;j++)
Endfor
Step 3: [Reading second matrix values in 2x2 order] scanf("%d",&a[i][j]);
Repeat for i=1 to 2 do printf("Enter 2nd matrix values in 2x2 order \n");
Repeat for j=1 to 2 do for(i=1;i<=2;i++)
Read b[i][j] for(j=1;j<=2;j++)
Endfor scanf("%d",&b[i][j]);
Endfor printf("Multiplication of Matrices is \n");
Step 4: [Calculating matrix multiplication] for(i=1;i<=2;i++)
Repeat for i=1 to 2 do {
Repeat for j=1 to 2 do for(j=1;j<=2;j++)
c[i][j]=0 {
Repeat for k=1 to 2 do for(k=1;k<=2;k++)
c[i][j] = c[i][j] + a[i][k]*b[k][j] c[i][j] = c[i][j] + a[i][k] * b[k][j];
Display c[i][j] printf("%d \t",c[i][j]);
endfor }
Endfor printf("\n");
Endfor }
Step 5: Stop getch( );
}
OUTPUT: