0% found this document useful (0 votes)
237 views125 pages

PRACTICAL

pps practical

Uploaded by

Sakaya Detory
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
237 views125 pages

PRACTICAL

pps practical

Uploaded by

Sakaya Detory
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 125

Q1:WRITE A PROGRAM THAT ACCEPTS THE MARKS OF

FIVE SUBJECTS AND FIND SUM AND PERCENTAGE


MARKS OBTAINDE BY STUDENT.

#include<stdio.h>
#include<conio.h>
Void main()
{
float m1,m2,m3,m4,m5,sum,per;
clrscr();
printf(“Enter the marks of five subjects:”);
scanf(“%f%f%f%f%f”,&m1,&m2,&m3,&m4,&m5);
sum=m1+m2+m3+m4+m5;
per=sum/5;
printf(“Sum of five subjects are %f\n”,sum);
printf(“Percentage of five subjects are %f”,per);
getch();
}
OUTPUT

ALGORITHM AND FLOWCHART


• Read five subject marks and store them into 5
different variables.
• Calculate the sum of all subjects and store in total
= m1 + m2+ m3+ m4 + c5.
• Divide the sum of all subjects by the total number
of subjects to find the average i.e. average = total /
5.
• Calculate the percentage using percentage = (total
/ 500) * 100.
• Finally, print resultant values total and percentage.
START

READ
m1,m2,m3,m4,m5,sum,per

Sum =m1+m2+m3+m4+m5

Per=sum/5

Print sum and per

stop
Q2:WRITE A PROGRAM THAT CALCULATES SIMPLE
INTEREST AND COMPOUND INTEREST .THE PRINCIPAL
AMOUNT ,RATE OF INTEREST AND TIME ARE ENTERED
THROUGH KEYBOARD.

#include<stdio.h>
#include<conio.h>
#include<math.h>
Void main()
{
float p,r,t,n,si,ci;
clrscr();
printf(“Enter the principal amount ,rate of
interest,time,number of times interest is
compounded per year:”);
scanf(“%f%f%f%f”,&p,&r,&t,&n);
si=(p*r*t)/100;
ci=p*pow((1+r/n),n*t)-p;
printf(“The value of simple interest is %f\n”,si);
printf(The value of compound interest is %f\n”,ci);
getch();
}

OUTPUT

ALGORITHM AND FLOWCHART


1. START
2. READ p,r,t,n,si,ci
3. Si=(p*r*t)/100
4. Ci=p*pow((1=r/n),n*t)-p
5. Print si and ci
6. Stop
start

READ
p,r,t,n,si,ci

Si=(p*r*t)/100
Ci=p*pow((1=r/n),n*t)-p

Print si and ci

stop
Q3:WRITE A PROGRAM TO CALCULATE AREA AND
CIRCUMFERENCE OF A CIRCLE.

#include<stdio.h>
#include<conio.h>
Void main()
{
float r,area,circum;
printf(“Enter the radius:”);
scanf(“%f”,&r);
area=3.14*r*r;
circum=2*3.14*r;
printf(“The area and circumference is %f and
%f”,area,circum”);
getch();
}
OUTPUT

ALGORITHM AND FLOWCHART


1. Start
2. Read r,area,circum
3. Area=3.14*r*r
4. Circum=2*3.14*r
5. Printarea and circum
6. Stop
start

Read r,area,circum

Area=3.14*r*r
Circum=2*3.14*r

Print area and circum

stop
Q4:WRITE A PROGRAM THAT ACCEPTS TEMPERATURE
IN CENTIGRADE AND CONVERT INTO FAHRENHEIT.

#include<stdio.h>
#include<conio.h>
void main()
{
float c,f;
clrscr();
printf("Enter the value of temperature in
centigrade:\n");
scanf("%f",&c);
f=(9*c)/5+32;
printf("Value of temperature in fahrenheit is
%f:",f);
getch();
}
OUTPUT

ALGORITHM AND FLOWCHART


1. Start
2. Read c,f
3. f=(9*c)/5+32
4. Printf f
5. Stop
start

Read c,f

f=(9*c)/5+32

Print f

stop
Q5: a: WRITE A PROGRAM THAT SWAPS VALUES OF
TWO VARIABLES USING A THIRD VARIABLE.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter values of a and b:");
scanf("%d%d",&a,&b);
printf("Value of a and b before swapping are %d
and %d\n",a,b);
c=a;
a=b;
b=c;
printf("Value of a and b after swapping are %d
and %d",a,b);
getch();
}
OUTPUT

ALGORITHM AND FLOWCHART


1. Start
2. Read a,b,c
3. Print value of a and b before swapping
4. c=a
a=b
b=c
5. print value of a and b after swapping
6. stop
Start

Read a,b,c

Print value of a
and b before
swapping
c=a
a=b
b=c

print value of a and b


after swapping

stop
Q5:b: WRITE A PROGRAM THAT SWAPS VALUES OF
TWO VARIABLES.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter values of a and b:");
scanf("%d%d",&a,&b);
printf("Value of a and b before swapping are %d
and %d\n",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("Value of a and b after swapping are %d
and %d",a,b);
getch();
}
OUTPUT

ALGORITHM
1. start
2. read a,b
3. print value of a and b before swapping
4. a=a+b
b=a-b
a=a-b
5. print value of a and b after swapping
6. stop
FLOWCHART

start

read a,b

print value of a and b


before swapping

a=a+b
b=a-b
a=a-b

print value of a and b


after swapping

stop
Q6:WRITE A PROGRAM THAT CHECKS WHETHER THE
TWO NUMBERS ENTERED BY THE USER ARE EQUAL
OR NOT.

#include<stdio.h>
#include<conio.h>
void main()
{
int num1,num2;
clrscr();
printf("Enter the number1 and number2:");
scanf("%d%d",&num1,&num2);
if(num1==num2)
{
printf("%d is equal to %d\n",num1,num2);
}
else
{
printf("%d is not equal to %d",num1,num2);
}
getch();
}

OUTPUT

ALGORITHM
1. start
2. read num1,num2
3. if num1==num2
print num1 Is equal to num2
else
print num2 is not equal to num1
4. stop
FLOWCHART

start

read num1,num2

num1=
=num2

print num2
Is not equal print num1
to num1 Is equal to
num2

stop
Q7:WRITE APROGRAM TO FIND GREATEST OF THREE
NUMBERS.

#include<stdio.h>
#include<conio.h>
void main()
{
int num1,num2,num3;
clrscr();
printf("Enter the number1,number2 and
number3:");
scanf("%d%d%d",&num1,&num2,&num3);
if((num1>num2)&&(num1>num2))
{
printf("%d is greatest among
%d,%d,%d",num1,num1,num2,num3);
}
else if((num2>num1)&&(num2>num3))
{
printf("%d is greatest among
%d,%d,%d",num2,num1,num2,num3);
}
else
{
printf("%d is greatest among
%d,%d,%d",num3,num1,num2,num3);
}
getch();
}

OUTPUT
ALGORITHM
➢start
➢read num1,num2,num3
➢if (num1>num2)&&(num1<num3)
print num1 is greatest among
num1,num2,num3
else if (num2>num3)&&(num2>num1)
print num2 is greatest among
num1,num2,num3
else
print num3 is greatest among
num1,num2,num3
➢stop
FLOWCHART

start

Read num1,num2,num3

Print num1 is greatest


(num1>num2)&&(nu TRUE among
m1<num3) num1,num2,num3

FALSE

Print num2 is
(num2>num3)&&(num greatest among
1<num2) TRUE num1,num2,num3

FALSE
Print num3 is greatest
among num1,num2,num3

STOP
Q8:WRITE A PROGRAM THAT FINDS WHETHER A
NUMBER IS EVEN OR ODD.

#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrscr();
printf("Enter the number:");
scanf("%d",&num);
if(num%2==0)
{
printf("%d is an even number",num);
}
else
{
printf("%d is an odd number",num);
}
getch();
}

OUTPUT

ALGORITHM
➢start
➢read num
➢if num%2==0
print num is an even number
else
print num is an odd number
➢stop
FLOWCHART
Q9:WRITE A PROGRAM THAT TELLS WHETHER A
GIVEN YEAR IS A LEAP YEAR OR NOT.

#include<stdio.h>
#include<conio.h>
void main()
{
int year;
clrscr();
printf("Enter the year:");
scanf("%d",&year);
if((year%400==0)||(year%4==0)&&(year%100!=0)
)
{
printf("%d is a leap year",year);
}
else
{
printf("%d is not a leap year",year);
}
getch();
}

OUTPUT
ALGORITHM
➢start
➢read year
➢if (year%400==0)||(year%4==0 &&
year%100!=0)
print year given is a leap year
else
print given year is not a leap year
➢stop
FLOWCHART
Q10:WRITE A PROGRAM THAT ACCEPTS MARKS OF
FIVE SUBJECTS AND FINDS PERECNTAGE AND PRINTS
GRADE .

#include<stdio.h>
#include<conio.h>
void main()
{
float m1,m2,m3,m4,m5,sum,per;
clrscr();
printf("Enter the marks of five subjects:\n");
scanf("%f%f%f%f%f",&m1,&m2,&m3,&m4,&m5);
sum=m1+m2+m3+m4+m5;
per=sum/5;
if(per>=90)
{
printf("Secured %f with grade A",per);
}
else if(per>=80&& per<90)
{
printf("Secured %f with grade B",per);
}
else if(per>=60 && per<80)
{
printf("Secured %f with grade C",per);
}
else
{
printf("Secured %f with grade D",per);
}
getch();
}
OUTPUT
ALGORITHM
➢start
➢read m1,m2,m3,m4,m5,per
➢per=((m1+m2+m3+m4+m5)*100)/500);
➢if per>=90
print secured % with grade A
else if per>=80 and per<90
print grade B
else if per>=60 and per<80
print grade C
else
print grade D
➢stop

FLOWCHART
Q11:WRITE A PROGRAM THAT TAKES TWO
OPERANDS AND ONE OPERATOR FROM THE USER
AND PERFORM THE OPERATION AND PRINTS RESULT
BY USING SWITCH STATEMENT.

#include<stdio.h>
#include<conio.h>
void main()
{
int num1,num2,op;
printf("Enter the two numbers:");
scanf("%d%d",&num1,&num2);
printf("Enter the operator:\n 1 for addition \n 2
for subtraction\n 3for multiplication \n 4 for
division\n 5 for modulo:\n");
scanf("%d",&op);
switch(op)
{
case 1:
printf("%d+%d=%d\n",num1,num2,num1+num2);
break;
case 2:
printf("%d-%d=%d\n",num1,num2,num1-num2);
break;
case 3:
printf("%d*%d=%d\n",num1,num2,num1*num2);
break;
case 5:

printf("%d%%%d=%d\n",num1,num2,num1%num
2);
break;
default:
printf("You made a wrong choice");
break;
}
getch();
}
OUTPUT

ALGORITHM
1. Start
2. Read num1, num2, op
3. Print enter 1 for addition \n 2 for subtraction \n 3 for
multiplication \n 4 for division \n 4 for division \n 5 for
modulo
4. Switch(op)
Case 1
Print addition of num 1 and num 2
and break
Case 2
Print subraction of num1 and num2
and break
Case 3
Print multiplication of num1 and
break
Case 4
Print division of num 1 and num 2
and break
Case 5
Print remainder when num 1
divide by num2 and break
Default
Print you made a wrong choice
And break
5. Stop
FLOWCHART
start

Read num1, num2, op

Print enter 1 for addition \n 2 for


subtraction \n 3 for multiplication \n 4
for division \n 4 for division \n 5 for
modulo

Case 1 Print num1 + num2

Print num1 -
Case 2
num2

Case 3 Print num1 * num2

Case 4 Print num1


/num2

Case 5 Print num1 %


num2

You made a
default
wrong choice

Stop
Q12:WRITE A PROGRAM TO PRINT SUM OF ALL
NUMBERS UPTO A GIVEN NUMBER.

#include<stdio.h>
#include<conio.h>
void main()
{
int num,i=0,sum=0;
clrscr();
printf("enter the number:");
scanf("%d",&num);
while(i<=num)
{
sum=sum+i;
i=i+1;
}
printf("sum of numbers upto %d is
%d",num,sum);
getch();
}

OUTPUT

ALGORITHM
➢Start
➢Read num,i,sum
➢Initialise i=0,sum=0
➢Sum=sum+i
I=i+1
➢Repeat steps 4 until i<=num
➢Print sum of numbers
➢Stop
FLOWCHART
Q13:WRITE A PROGRAM TO FIND FACTORIAL OF A
NUMBER.

#include<stdio.h>
#include<conio.h>
void main()
{
int num,fac=1,i=1;
clrscr();
printf("enter the number:");
scanf("%d",&num);
while(i<=num)
{
fac=fac*i;
i=i+1;
}
printf("the factorial of %d is %d",num,fac);
getch();
}
OUTPUT

ALGORITHM
➢Start
➢Read num and intialise fac=1 and i=1
➢fac=fac*i
➢i=i+1
➢repeat steps 3 and 4 until i<=num
➢print factorial of num
➢stop
FLOWCHART
Q14:WRITE A PROGRAM TO PRINT SUM OF EVEN AND
ODD NUMBERS FROM 1 TO N NUMBERS.

#include<stdio.h>
#include<conio.h>
void main()
{
int num,sum_odd=0,sum_even=0,odd=-1,even=0;
clrscr();
printf("enter the number:");
scanf("%d",&num);
while(odd<=num)
{
sum_odd=sum_odd+odd;
odd=odd+2;
}
printf("sum of odd numbers upto %d is
%d\n",num,odd);
while(even<=num)
{
sum_even=sum_even+even;
even=even+2;
}
printf("sum of even numbers upto %d is
%d",num,even);
getch();
}

ALGORITHM
➢ start
➢ read num and initialise
sum_odd=0,sum_even=0,odd=1,even=2
➢ sum_odd=sum_odd+odd
odd=odd+2
➢ sum_even=sum_even+even
even=even+2
➢ repeat steps 3 and 4 until odd<=num
and even<=num
➢ print sum of odd and even numbers
➢ stop
FLOWCHART
Q15:WRITE A PROGRAM TO PRINT FIBONACCI SERIES.

#include<stdio.h>
#include<conio.h>
void main()
{
int num,i=0,f1=0,f2=1,f3=0;
clrscr();
printf("Enter the numebr:");
scanf("%d",&num);
printf("The fibonacci series upto %d terms
are\n",num);
printf("%d\n",f1);
printf("%d\n",f2);
for(i=0;i<num-2;i++)
{
f3=f1+f2;
printf("%d\n",f3);
f1=f2;
f2=f3;
}
getch();
}

OUTPUT

ALGORITHM
➢start
➢read num and initialise i=0,f1=0,f2=1 and f3=0
➢print the Fibonacci series upto n terms is
print f1
print f2
➢f3=f1+f2
print f3
f1=f2
f2=f3
➢repeat step 4 until i<num-2
➢stop
FLOWCHART
Q16:WRITE A PROGRAM TO CHECK WHETHER THE
ENTERED NUMBER IS PRIME OR NOT.

#include<stdio.h>
#include<conio.h>
void main()
{
int num,count=0,f=1,i=1;
clrscr();
printf("enter the number:");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
if(num%i==0)
{
count=count+1;
}
}
if(count==2)
{
printf("%d is a prime number",num);
}
else
{
printf("%d is not a prime number",num);
}
getch();
}

OUTPUT
ALGORITHM
1 start
2 read num and initialise count=0 and i=1
3 if num%i==0
Count =count+1
4 repeat step 3 until i<=num
5 if count ==?
Print it is a prime number
else
print it is not a prime number
6 stop
FLOWCHART
Q17:WRITE A PROGRAM TO FIND THE SUM OF DIGITS
OF THE ENTERED NUMBER.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,num,rem=0,sum=0;
clrscr();
printf("enter the number:");
scanf("%d",&num);
n=num;
while(num>0)
{
rem=num%10;
sum=rem+sum;
num=num/10;
}
printf("sum of digits of %d is %d",n,sum);
getch();
}

OUTPUT

ALGORITHM
1 start
2 read n, num and initialise rem=0 , sum=0
3 rem= num% 10
Sum = rem+ sum
num = num/10
4 repeat step 3 until num>0
5 print the sum of digits
6 stop
FLOWCHART
Q18:WRITE A PROGRAM TO FIND REVERSE OF A
NUMBER.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,num,rem=0,rev=0;
clrscr();
printf("enter the number:");
scanf("%d",&num);
n=num;
while(num>0)
{
rem=num%10;
rev=rem+rev*10;
num=num/10;
}
printf("reverse of %d is %d",n,rev);
getch();
}

OUTPUT

ALGORITHM
1 . start
2. read n, num and initialise rem=0, rev=0
3 . n=num
4. rem=num%10
rev = rem +rev*10
num = num/10 repeat step 4 until num>0
5 . print the reverse of number
6 . stop
FLOWCHART
Q19:WRITE A PROGRAM TO PRINT ARMSTRONG
NUMBERS FROM 1 TO 1000.

#include <stdio.h>
#include <math.h>
void main()
{
printf("All Armstrong numbers between 1 and 1000
are:\n");
for(int n=1;n<=1000;n++)
{
int num = n,rem,sum=0;
if(n<=9)
{
printf("%d\n",n);
}
else
{
int digit = (int) log10(num) + 1; //To count
number of digits
//Calculating sum of power of digits of a number
while(num > 0)
{
rem = num % 10; //To find last digit of the
number
sum = sum + pow(rem,digit);
num = num / 10;
}

if (sum == n)
{
printf("%d ", n);.
}
}
}
}
OUTPUT

ALGORITHM
➢ start
➢ print all Armstrong numbers between 1 and 1000
➢ read n=1
➢ if n<=9
print n
else
intialise digit =intlog10(num+1)
➢ rem=num%10
sum=sum+pow(rem,digit)
num=num/10
➢ repeat previois step until num>0
➢ repeat previous step until n<1000
➢ stop
FLOWCHART
Q20:WRITE A PROGRAM TO CONVERT DECIMAL
NUMBER TO BINARY NUMBER AND VICE VERSA.

#include<stdio.h>
void main()
{
int num,n,rem,dec,bin,base=1,ch;
printf("Enter 1 to perfrom binary conversion \n
2 for decimal conversion:");
scanf("%d",&ch);
if(ch==1)
{
printf("Enter the decimal number:");
scanf("%d",&num);
n=num;
while(num>0)
{
rem=num%2;
bin=bin+rem*base;
num=num/2;
base=base*10;
}
printf("Binary number of %d is %d",n,bin);
}
else if(ch==2)
{
printf("Enter the binary number:");
scanf("%d",&num);
n=num;
while(num>0)
{
rem=num%10;
dec=dec+rem*base;
num=num/10;
base=base*2;
}
printf("Decimal number of %d is %d",n,dec);
}
}

OUTPUT
ALGORITHM
➢ start
➢ read num,n,rem,dec,bin,ch and
initialise base =1
➢ print enter 1 to perform binary
conversion and 2 for decimal conversion
➢ if ch=1
enter the decimal number
n=num
➢ rem=num%2
bin=bin+rem*base
num=num/2
base=base*10
➢ repeat previous step until num>0
print binary remainder
➢ else if ch==2
enter the binary number
n=num
➢ rem=num%10
dec=dec+rem*base
num=num/10
base=base*2
➢ repeat previous step until num>0
print the decimal number
➢ stop
FLOWCHART
start

read num,n,rem,dec,bin,ch and initialise base =1

Enter the decimal


Ch==1
number ,n=num

Enter the binary


Ch==2
number num>0

n=num rem=num%2

dec=dec+rem*base
num=num/2
num>0 base=base*10

Print binary
number
rem=num%10

dec=dec+rem*bas
e
num=num/10

base=base*2

Print decimal number Stop


Q21:WRITE A PROGRAM THAT SIMPLY TAKES
ELEMENT OF THE ARRAY FROM THE USER AND FINDS
THE SUM OF THESE ELEMENTS.

#include<stdio.h>
#include<conio.h>
void main()
{
int arr[50],i,size,sum=0;
clrscr();
printf("Enter the size of array:");
scanf("%d",&size);
for(i=0;i<size;i++)
{
printf("Enter the element at arr[%d]:",i);
scanf("%d",&arr[i]);
}
for(i=0;i<size;i++)
{
sum=sum+arr[i];
printf("The element at arr[%d] is
%d\n",i,arr[i]);
}
printf("The sum of elements is %d",sum);
getch();
}
OUTPUT

ALGORITHM
➢ start
➢ read arr[50],i,size and initialise sum=0
➢ initialise i=0
➢ enter the element at arr[i],print the
element ,sum=sum+arr[i]
➢ increment I by 1
➢ repeat step 4 and 5 until i<size
➢ print sum
➢ stop
FLOWCHART
Start

i=0

i<size

Enter the element at


arr[i]

i=0

Print the sum of


elements
i<size

stop Sum=sum+arr[i]

Print the
element at
arr[i]
Q22:WRITE A PROGRAM THAT TAKES INPUT OF
TWO ARRAYS AND SAVES SUM OF
CORRESPONDING ELEMENTS OF THESE ARRAYS
IN A THIRD ARRAY AND PRINTS THEM.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],b[50],c[50],i,size,sum=0;
clrscr();
printf("Enter the size of array:");
scanf("%d",&size);
printf("The elements of first array are:");
for(i=0;i<size;i++)
{
printf("Enter the element at arr[%d]:",i);
scanf("%d",&a[i]);
}
printf("The elements of second array are:\n");
for(i=0;i<size;i++)
{
printf("Enter the element at arr[%d]:",i);
scanf("%d",&b[i]);
}
for(i=0;i<size;i++)
{
c[i]=a[i]+b[i];
}
printf("The sum of elements of array1 and array2
is");
for(i=0;i<size;i++)
{
printf("%d\n",c[i]);
}
getch();
}
OUTPUT

ALGORITHM
➢start
➢read a[50],b[50],c[50],i,size and initialise
sum=0
➢intialise i=0
➢enter the element at arr[i] for array 1 and
array 2 ,c[i]=a[i]+b[i] and print the elemet of
array 1 and array 2
➢increment I by 1
➢repeat step 4 and 5 until i<size
➢stop
FLOWCHART
start

read a[50],b[50],c[50],i,size and initialise


sum=0 intialise i=0

Enter the element


i<size of first array

i=0

i<size

Print the i=0


elements of
second array

i<size

cC[i]=a[i]+b[i]

Print the sum of


elements of array

stop
Q23:WRITE A PROGRAM TO FIND MAXIMUM
AND MINIMUM ELEMENT OF ARRAY.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],size,i,max=0,min=0;
printf("Enter the size of array");
scanf("%d",&size);
for(i=0;i<size;i++)
{
printf("Enter the element at a[%d]",i);
scanf("%d",&a[i]);
}
max=a[0];
min=a[0];
for(i=0;i<size;i++)
{
if(max<a[i])
{
max=a[i];
}
if(min>a[i])
{
min=a[i];
}
}
printf("The maximum and minimum values of
given array is %d and %d",max,min);
getch();
}

OUTPUT

ALGORITHM
➢START
➢Read a[50],size,i and initialise max=0,min=0
➢Initialsie i=0
➢Enter the element at a[i],increment I by 1
➢Repeat step 4 until i<size
➢Max=a[0] and min=a[0]
➢If (max<a[i])
Max=a[i]
If (min<a[i])
Min=a[i]
➢Repeat step 7 until i<size
➢Print the maximum and minimum values of
given array
➢Stop
FLOWCHART

START

Read a[50],size,i and initialise


max=0,min=0

Enter the
i<size element at a[i]

max=a[0]
min=a[0]

i=0

i<size

max<a[i] max=a[0]

min>a[i] min=a[0]

Print maximum and


minimum values

stop
24. WAP TO SEARCH AN ELEMENT IN AN ARRAY
USING LINEAR SEARH.
#include<stdio.h>
int search(int array[],int n,int x)
{
for(int i=0;i<n;i++)
{
if(array[i]==x)
{
return i;
}
}
return -1;
}
void main()
{
int array[20];
int n;
int x;
printf("Enter the number of elements:");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
printf("Enter Element at array[%d]:",i);
scanf("%d",&array[i]);
}
printf("Enter the element to be searched:");
scanf("%d",&x);
int result=search(array,n,x);
if(result==-1)
{
printf("Element not found");
}
else
{
printf("%d is at index %d",x,result);
}
}
OUTPUT

ALGORITHM
• Start
• Declare array[] and variables i=0 and result ,x
• Input the elements of array and enter the number
to be searched
• If(array[i]==x)
Return i
Else
Return -1
• If result==-1
Print element not found
Else
Print element at index i
• Stop
FLOWCHART
START

READ array[],n,x,result

Int i=0 i=0

i<n Enter the element

Enter the element to be searched

result=search(array,n,x)

result==-1 Print element not


found

Print the index of


element result

stop
search(array,n,x)

int i=0 i=i+1

i<n

array[i]==x return i

return -1

stop
25: WRITE A PROGRAM TO SORT THE ELEMENTS OF THE ARRAY
USING THE BUBBLE SORTING TECHNIQUE.

#include<stdio.h>
int bubblesort(int array[],int n)
{
for(int i=0;i<=n;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(array[j]<array[j+1])
{
int temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
}
int main()
{
int array[20],n,num;
printf("Enter the number of elements:");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
printf("Enter the element at array[%d]",i);
scanf("%d",&array[i]);
}
num=bubblesort(array,n);
printf("The sorted array is:\n");
for(int i=0;i<n;i++)
{
printf("%d\t",array[i]);
}
return 0;
}

OUTPUT
ALGORITHM
• Start
• bubblesort(array)
• enter the number of elements
• now starting with the first element index compare the
current element with the next element of the array
• if the current element is greater than the next element
of the array ,swap them
• if the current element is less than the next element
move to the next element
• repeat the previous steps until the sorted array is
obtained
• print the sorted array
• stop
FLOWCHART

start

int array[20],n,num,i

i=i+1

i<n

Enter the element


at array[i]

num=bubblesort(array,n)

Print the sorted


array

stop
bubblesort(array,n)

int i=0

i=i+1

i<n

int j=0

j=j+1

j<n

array[j]
<array[j
+1]

stop int temp=array[i]

array[j]=array[j+1]

array[j+1]=temp
26:WRITE A PROGRAM TO ADD AND MULTIPLY TWO MATRICES OF
ORDER nXn.

#include<stdio.h>
void main()
{
int a[5][5],b[5][5],c[5][5],d[5][5],i,j,k,rc1,sum=0;
printf("Enter the rows and columns of matrix 1 and matrix 2:");
scanf("%d",&rc1);
printf("Enter the elements of matrix 1:\n");
for(i=0;i<rc1;i++)
{
for(j=0;j<rc1;j++)
{
printf("Enter the element at a[%d][%d]:",i,j);
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements of matrix 2:\n");
for(i=0;i<rc1;i++)
{
for(j=0;j<rc1;j++)
{
printf("Enter the element at b[%d][%d]:",i,j);
scanf("%d",&b[i][j]);
}
}
for(i=0;i<rc1;i++)
{
for(j=0;j<rc1;j++)
{
sum=0;
for(k=0;k<rc1;k++)
{
sum=sum+a[i][k]*b[k][j];
}
c[i][j]=sum;
}
}
for(i=0;i<rc1;i++)
{
for(j=0;j<rc1;j++)
{
d[i][j]=a[i][j]+b[i][j];
}
}
printf("The addition of two matrix is:\n");
for(i=0;i<rc1;i++)
{
printf("\n");
for(j=0;j<rc1;j++)
{
printf("%d\t",d[i][j]);
}
}
printf("\n");
printf("The multiplication of two matrix is:\n");
for(i=0;i<rc1;i++)
{
printf("\n");
for(j=0;j<rc1;j++)
{
printf("%d\t",c[i][j]);
}
}
}
OUTPUT

ALGORITHM
• start
• read a[5][5],b[5][5],c[5][5],d[5][5],i,j,k,rc1 and initialise sum=0
• enter the elements of matrix 1 and matrix 2 as number of times
the the number of rows and columns of matrix is given
• i=0 and j=0,sum=0 and k=0
sum=sum+a[i][k]*b[k][j]
until k<rc1 and
c[i][j]=sum
• repeat step 4 until i<rc1 And j<rc1
• i=0 and j=0
d[i][j]=a[i][j]+b[i][j] until j<rc1 and i<rc1
• print the matrix obtained after addition and multiplication.
• Stop
Start
• FLOWCHART

read a[5][5],b[5][5],c[5][5],d[5][5],i,j,k,rc1
and initialise sum=0

enter the elements of matrix 1 and matrix


2 as number of times the the number of
rows and columns of matrix is given

int i=0,j=0

i<rc1

j<rc1

d[i][j]=a[i][j]+b[i][j]

i<rc1

j<rc1

sum=0

K<rc1

Print the matrix


obtained after Sum=sum+a[i][k]*b[k][j
addition and ] stop
multiplication
27:WRITE A PROGRAM THAT FINDS THE SUM OF DIAGONAL
ELEMENTS OF A mXn matrix.

#include<stdio.h>
void main()
{
int a[5][5],i,j,rc,trace=0;
printf("Enter the rows and columns of matrix:");
scanf("%d",&rc);
printf("Enter the elements of matrix:\n");
for(i=0;i<rc;i++)
{
for(j=0;j<rc;j++)
{
printf("Enter the element at a[%d][%d]:",i,j);
scanf("%d",&a[i][j]);
}
}
for(i=0;i<rc;i++)
{
for(j=0;j<rc;j++)
{
if(i==j)
{
trace=trace+a[i][i];
}
}
}
printf("The trace of matrix is: %d",trace);
}

OUTPUT
ALGORITHM
• Start
• Read a[5][5],i,j,rc,trace=0
• Enter the rows and columns of matrix
• Enter the elemenst of matrix until i<rc and j<rc
• If (i==j)
trace=trace+a[i][i]
and the loop continues until i<rc and j<rc
• Print the trace of the matrix
• Stop
FLOWCHART
Start

read a[5][5],i=0,j=0,k,rc and initialise


trace=0

enter the rows and columns of matrix

i<rc

j<rc

Enter the elements of


matrix

i<rc

j<rc

i==j

trace=trace+a[i][i]

Print trace

stop
28:WRITE A PROGRAM TO IMPLEMENT strlen(),strcpy() and
strcat().

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char string[50],dest[50],c1[50];
clrscr();
printf("Enter the string:");
gets(string);
printf("The length of %s is %d\n",string,strlen(string));
char dest[50];
strcpy(dest,string);
printf("The value at source is %s\n",string);
printf("The value at destination is %s\n",dest);
printf("Enter the character to be concatenated:");
scanf("%s",&c1);
strcat(string,c1);
printf("The concatenated string is %s\n",string);
getch();
}

OUTPUT

ALGORITHM
• Start
• Read string[50],dest[50],c1[50]
• Print the length of string is strlen(string)
• strcpy(dest,string)
• print the value at source is string
• print the value at destination is dest
• strcat(string,c1)
• print the concatenated string is string
• stop
FLOWCHART

START

Read string[50],dest[50],c1[50]

Print the length of string is strlen(string)

strcpy(dest,string)

print the value at source is string

print the value at destination is dest

strcat(string,c1)

Print the concatenated string is string

stop
29:WRITE A PROGRAM TO SWAP TWO ELEMENTS
USING THE CONCEPT OF POINTERS.

#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,*ptr1,*ptr2,*ptr3;
clrscr();
printf("Enter the value of x:");
scanf("%d",&x);
printf("Enter the value of y:");
scanf("%d",&y);
ptr1=&x;
ptr2=&y;
printf("The value of x and y before swapping are %d and
%d\n",*ptr1,*ptr2);
ptr3=ptr1;
ptr1=ptr2;
ptr2=ptr3;
printf("The value of x and y after swapping are %d and
%d",*ptr1,*ptr2);
getch();
}

OUTPUT

ALGORITHM
➢ start
➢ read x,y,*ptr1,*ptr2,*ptr3;
➢ Enter the value of x and Enter the value of y
➢ ptr1=&x;
ptr2=&y;
➢ print The value of x and y before swapping
➢ ptr3=ptr1;
ptr1=ptr2;
ptr2=ptr3;
➢ printf The value of x and y after swapping
➢ stop
FLOWCHART

START

read x,y,*ptr1,*ptr2,*ptr3

ptr1=&x
ptr2=&y

print The value of x and y before swapping

ptr3=ptr1
ptr1=ptr2
ptr2=ptr3

print The value of x and y after swapping

stop
30: Define a structure data type TRAIN_INFO. The type
contain Train No.: integer type Train name: string Departure
Time: aggregate type TIME Arrival Time : aggregate type
TIME Start station: string End station : string The
structure type Time contains two integer members: hour
and minute. Maintain a train timetable and implement the
following operations: (i)List all the trains (sorted according
to train number) that depart from a particular section.
(ii)List all the trains that depart from a particular station at a
particular time. (iii)List all he trains that depart from a
particular station within the next one hour of a given time.
(iv)List all the trains between a pair of start station and
end station.

#include <stdio.h>
struct TRAIN{
int train_no;
char train_name[10];
int arrhour;
int arrmin;
int departhour;
int departmin;
char start_station[20];
char end_station[20];
} tr[10];
int main()
{
int i,n,j,arr[10],h,m,station[20],bstation[20];
printf("Enter the number of records to enter:");
scanf("%d",&n);
arr[n];
printf("Enter information of train:\n");
for (i=0;i<n;++i)
{
printf("\nFor train number %d,\n",i+1);
printf("Enter train number: ");
scanf("%d", &tr[i].train_no);
arr[i]=tr[i].train_no;
printf("Enter train name: ");
scanf("%s", &tr[i].train_name);
printf("Enter start station: ");
scanf("%s", &tr[i].start_station);
printf("Enter end station: ");
scanf("%s", &tr[i].end_station);
printf("Enter arrival hour: ");
scanf("%d", &tr[i].arrhour);
printf("Enter arrival minute: ");
scanf("%s", &tr[i].arrmin);
printf("Enter departure hour: ");
scanf("%d", &tr[i].departhour);
printf("Enter departure minute: ");
scanf("%d", &tr[i].departmin);
}
printf("Enter the hour:");
scanf("%d",&h);
printf("enter the minute:");
scanf("%d",&m);
for(i=0;i<n;i++)
{
if(h==tr[i].departhour && m==tr[i].departmin)
{
printf("Train having departure time %d:%d are
%s\n",h,m,tr[i].train_name);
}
else
{
printf("No trains are departed at this time\n");
break;
}
}
for(int i=0;i<=n;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(arr[j]<arr[j+1])
{
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printf("The sorted train number is:\n");
for(int i=0;i<n;i++)
{
printf("%d\t",tr[i].train_no);
}
printf("\nEnter the end station:");
scanf("%s",&station);
for(i=0;i<n;i++)
{
if(station==tr[i].end_station)
{
printf("%d train number is found ",tr[i].train_no);
}
else
{
printf("No train is found\n");
break;
}
}
printf("\nEnter the start station:");
scanf("%s",&bstation);
for(i=0;i<n;i++)
{
if(station==tr[i].end_station &&
bstation==tr[i].start_station)
{
printf("train between %s and %s is
%d",tr[i].start_station,tr[i].end_station,tr[i].train_no);
}
else
{
printf("No train is found between these stations");
break;
}
}
return 0;
}
OUTPUT

ALGORITHM
• start
• define structure train along with its members
• read i,n,j,arr[10],h,m,station[20],bstation[20]
• enter the information of train
• enter the various details regarding the train
until i<n
• if n==tr[i].departhour and m==tr[i].departmin
print the train number
else
print no trains are departed at this time
• sort the train information with respect to train
no and print the sorted details ie the train no
• if(station==tr[i].end_station)
print train number is found
else
print train no is found
• if(station==tr[i].end_station and
bstation==tr[i].start_station)
print train is found between thse station
else
print no train is found between these stations
repeat these steps until i<n
• stop
FLOWCHART

START

Define structure train along with its members

Read I,n,j,h,m,station[20],bstation[20]

i<n

Enter information of train

if n==tr[i].departhour
Print train number
and tr[i].departmin

No trains are departed at this


time
Print sorted
information of train
ifstation==tr[i].en
d_station

Print train number Print no train is found

if
Print train is found
station==tr[i].end_stati
between these station
on==tr[i].start_station
st
Print train is not found between these station op
31.WAP to compare the contents of two files and
determine whether they are same or not.

#include <stdio.h>
#include <string.h>
int main (void)
{
FILE *fptr1,*fptr2;
char path [256];
char string1 [20];
char string2 [20];
fptr1 = fopen("file1.txt", "r");
if (fptr1 == NULL)
{
printf ("Error! Unable to open the file1.");
return 1;
}
fptr2 =fopen("file2.txt", "r");
if (fptr2 == NULL)
{
printf ("Error! Unable to open the file2.");
fclose (fptr1);
return 1;
}
while ((!feof (fptr1)) && (! feof (fptr2)))
{
fscanf (fptr1, "%s", string1);
fscanf (fptr2, "%s", string2);
if (strcmp (string1, string2) != 0)
{
printf ("The content of files are not the same.");
}
else
{
printf(“The content of files are not the same.”);
}
}
fclose (fptr1);
fclose (fptr2);
return 0;
}
OUTPUT

ALGORITHM
• start
• Input file path of two files to compare from user,
store it in string1 and string2.
• Open both files in r (read) mode and store their
references in fptr1 and fptr2.
• If the file cannot be opened return sorry unable
to return the file
• Else
Read a character from both files and compare.
If content of both files are same return that the
contents are same
Else
Return the contents of file are not same
• Stop
FLOWCHART

START

Input file path of two files


to compare from user, store
it in string1 and string2.

Open both files in r (read) mode and store their


references in fptr1 and fptr2.

Print unable to open


If fptr1==NULL
the file

If fptr2==NULL

If string1==string2
Print unable to
open the file

Print content of both


files are same

Print content of the files


are not same

stop
32. WAP to check whether a given word exists in a file
or not. If yes then find the number of times it occurs.

#include<stdio.h>
#include<stdlib.h>
#inlcude<string.h>
void main()
{
FILE*fp;
char path[255],targetword[100];
printf(“Enter the path of the file:”);
gets(path);
fp=fopen(path,”r”);
if(fp==NULL)
{
printf(“file not found!”);
}
printf(“Enter the word to be searched:”):
scanf(“%s”,&targetword):
char current_word[100];
int ch,i=0;
int wordcounter=0;
while((ch=fgetc(fp))!=EOF)
{
if(ch==’’||ch==’\0’)
{
current_word[i]=’\0’;
i=0;
if(!(strcmp(targetword,current_word)))
{
wordcounter++;
}
}
else
{
currentword[i]=ch;
i++;
}
}
if(wordcounter==0)
{
printf(“word not found”);
}
else
{
Printf(“Matches found”,wordcounter);
}
fclose(fp);
return 0;
}

OUTPUT
Enter the word to be searched:is
Word not found

ALGORITHM
• Start
• Read path[255],targetword[100],fp;
• Open the file
• if(fp==NULL)
print file not found
exit(1)
• read current_word[100],ch,i=0,wordcounter=0
• if(ch==’’ or ch==’\n’)
current_word[i]==’\n’
if(strcmp(targetword,current_word)))
wordcounter++
else
current_word[i]=ch
i++
until end of file not found
• if(wordcounter==0)
print word not found
else
print matches found and wordcounter
• fclose(fp)
• stop
FLOWCHART
START

Read
path[255],targetword[100],fp,current_word[100],ch,i=0,wordcounter=0

Open the file

If fp==NULL Print file not found

If ch==’’ or ch==’\n’

current_word[i]=’\0’

if
strcmp(targetword, wordcounter++
current_word)

current_word[i]=ch

i++

Print word not


If wordcounter==0
found

Print matches found and wordcounter stop

You might also like