Computer Programming Lab
Computer Programming Lab
Computer
Programming Lab
(PBC 102)
NAME OF EXAMINER:
SIGNATURE OF EXAMINER:
Index
Exp. Date Experiment Page Remarks
No. No.
Index
Exp. Date Experiment Page Remarks
No. No.
PROGRAM OBJECTIVE
Page 4 of 91
Start
Input C
F = 1.8*C+32
Print F
Stop
Page 5 of 91
Algorithm
to convert temperature from Celsius to Fahrenheit
STEP 1. Input C
STEP 2. F = 1.8*C+32
STEP 3. Print F
Page 6 of 91
/*Source code to convert temperature from Celsius to Fahrenheit
Name -
Section -
Roll No.-
Branch - */
#include<stdio.h>
int main()
{
float f,c;
printf("\n Enter the temp. in Celsius:");
scanf("%f",&c);
f=c*1.8+32;
printf("\n the temperature in Fahrenheit is:%.2f",f);
return 0;
}
/*OUTPUT
Page 7 of 91
PROGRAM OBJECTIVE
Page 8 of 91
Start
Input p, r, t
Si = (p * r * t)/100
A = p*pow(1+(r/100),t)
Ci = A-p
Print Si, Ci
Stop
Page 9 of 91
Algorithm
to calculate Simple Interest and Compound interest.
1. Input p,r,t
2. Si = (p*r*t)/100
3. A = p*pow(1+(r/100),t)
4. Ci = A-p
5. Print Si, Ci
Page 10 of 91
/*Source code to calculate Simple Interest and Compound interest.
Name -
Section -
Roll No.-
Branch - */
#include<stdio.h>
#include<math.h>
int main()
{
float p,r,t;
float si,A,ci;
printf("Enter the value of p,r and t");
scanf("%f%f%f",&p,&r,&t);
si=(p*r*t)/100;
A=p*pow(1+(r/100),t);
ci=A-p;
printf("\n The Simple interest is %f", si);
printf("\n The Compound interest is %f", ci);
return 0;
}
/*OUTPUT
Page 11 of 91
PROGRAM OBJECTIVE
Page 12 of 91
Start
Input bas_sal
Print total_sal
Stop
Page 13 of 91
Algorithm
Page 14 of 91
/*Source Code- to calculate total salary.
Name -
Section -
Roll No.-
Branch - */
#include<stdio.h>
int main()
{
float hra, ta, da;
float bas_sal, total_sal;
printf("Enter Basic Salary");
scanf("%f",& bas_sal);
hra= bas_sal *3/100;
ta= bas_sal *5/100;
da= bas_sal *3/100;
total_sal= bas_sal +hra+ta+da;
printf("Total Salary is %f", total_sal);
return 0;
}
/*OUTPUT
Page 15 of 91
PROGRAM OBJECTIVE
4-Write a C program to find whether a given number is even or odd (using if-else).
Page 16 of 91
Start
Input num
True False
Is
Print “Number is Print “Number is
Num % 2==
Even” 0 Odd”
Stop
Page 17 of 91
Algorithm
Page 18 of 91
/*Source Code- to find whether a given number is even or odd
Name -
Section -
Roll No.-
Branch - */
#include<stdio.h>
int main()
{
int num;
printf("\n Enter the number:");
scanf("%d",&num);
if(num %2==0)
printf("\n The number is even ");
else
printf("\n The number is odd ");
return 0;
}
/*OUTPUT
Page 19 of 91
PROGRAM OBJECTIVE
Page 20 of 91
PAGE 16 of 18
Start
Input num
No
Is Yes
num<0? Print”-ve”
No
Print”zero”
Stop
Page 21 of 91
Algorithm
To find whether number is positive, negative or zero (is else-if else structure)
STEP 1. Input num
STEP 2. Is (Num > 0) Then
Begin
Print “Positive”
End
Else if (Num<0) Then
Begin
Print “Negative”
End
Else
Begin
Print “Zero”
End
Page 22 of 91
/*Source Code- To find whether number is positive, negative or zero (if else-if else
structure)
Name -
Section -
Roll No -
Branch - */
#include<stdio.h>
int main()
{
int num;
printf("\n Enter the number:");
scanf("%d",&num);
if(num>0)
printf("\n The number is positive");
else if(num <0)
printf("\n The number is negative ");
else
printf("\n The number is zero ");
return 0;
}
/*OUTPUT
Enter the number:1
The number is positive
Enter the number:-2
The number is negative
Enter the number:0
The number is zero */
Page 23 of 91
PROGRAM OBJECTIVE
Page 24 of 91
Start
Input num1,num2,num3
Large = num1
Is num2
Yes
> Large = num2
Large?
No
Is num3
> Large? Large = num3
No
Print Large
Stop
Page 25 of 91
Algorithm
To find the largest of three numbers
1. Input num1 , num2 , num3
2. large=num1
3. Is (num2>large) then
Begin
large=num2
End
4. Is (num3>large) then
Begin
Large=num3
End
5. Print large
Page 26 of 91
/*Source Code- to find biggest of three numbers (IS STRUCTURE)
Name -
Section -
Roll No.-
Branch - */
#include <stdio.h>
int main()
{
int large, num1, num2,num3;
printf("Enter the value of num1, num2,num3\n");
scanf("%d%d%d", &num1,& num2,&num3);
large=num1;
if(num2>large)
large=num2;
if (num3>large)
large=num3;
printf("Largest no. is %d",large);
return 0;
}
/*OUTPUT
Page 27 of 91
PROGRAM OBJECTIVE
Page 28 of 91
Start
Input unit
Yes
Is
rate = 7
Unit>2000 ?
No
Is Yes
rate = 6
Unit>1000?
No
rate = 5
Bill = rate*unit
Print bill
stop
Page 29 of 91
Algorithm
1. Input unit
2. Is(unit>2000)then
Begin
Rate = 7
End
Else If(unit>1000) then
Begin
Rate = 6
End
Else
Begin
Rate = 5
End
3. Bill= Rate*unit
4. Print Bill
Page 30 of 91
/*Source Code- to calculate electricity bill
Name -
Section -
Roll No.-
Branch - */
#include<stdio.h>
int main()
{float unit, bill,rate;
printf("enter the units ");
scanf("%f",&unit);
if(unit>2000)
rate=7;
else if((unit<=2000)&&(unit>1000))
rate=6;
else
rate=5;
bill=rate*unit;
printf("\nBILL :%f",bill);
return 0;
}
/* output
enter the units :3000
BILL :21000.000000
*/
Page 31 of 91
PROGRAM OBJECTIVE
8- Write a C program to find grade of the student for four subjects marks for each
subject to be entered by the user. Calculate percentage and based upon the condition
print the grade.
Per>=85 grade=A
Per<85 and per>=70 grade=B
Per<70and per>=55 grade=C
Per<55 grade=D
Page 32 of 91
START
Input
S1,S2,S3,S4
Percent=
(S1+S2+S3+S4)*100/400
YES
Is Percent >=85 Print ”Grade= A”
?
NO
YES
Is Percent>=70 Print “Grade= B”
?
NO
YES
NO
STOP
Page 33 of 91
Algorithm
To calculate percentage and based upon the condition print the grade.
STEP 1. Input S1, S2, S3, S4
STEP 2. Percent = (S1+S2+S3+S4)*100/400
STEP 3. Is (Percent Then
Begin
Print “Grade= A”
End
Else if (Percent Then
Begin
Print “Grade= B”
End
Else if (Percent Then
Begin
Print “Grade =C”
End
Else
Begin
Print “Grade= D”
End
Page 34 of 91
/*Source code to Calculate percentage and based upon the condition print the grade
Name -
Section -
Roll No.-
Branch - */
#include<stdio.h>
int main()
{
float s1, s2,s3,s4,percent;
printf("Enter the value of s1, s2,s3,s4\n");
scanf("%f%f%f%f", &s1,& s2,&s3, &s4);
percent=(s1+s2+s3+s4)*100/400;
if (percent>=85)
printf("Grade A");
else if(percent>=70)
printf("Grade B");
else if(percent>=55)
printf("Grade C");
else
printf("Grade D");
return 0;
}
/*OUTPUT
Enter the value of s1, s2,s3,s4
97
89
88
95
Grade A
Enter the value of s1, s2,s3,s4
33
23
27
20
Grade D */
Page 35 of 91
PROGRAM OBJECTIVE
9- Write a C program to calculate the bill for a shopkeeper who sales mango @ 5 RS
per mango. If bill is greater than 500 then give discount of 10% otherwise no
discount.
Page 36 of 91
Start
Input qty
bill=5*qty
yes
noIs
bill=bill- bill*(10/100)
bill > 500?
no
Print bill
Stop
Page 37 of 91
Algorithm
Page 38 of 91
/*Source code to calculate bill for a shopkeeper
Name -
Section -
Roll No.-
Branch - */
#include <stdio.h>
int main()
{
int m_qty,bill;
printf("Enter the number of mango");
scanf("%d", &m_qty);
bill=5*m_qty;
if(bill>500)
bill=bill-bill*10/100;
printf("%d", bill);
return 0;
}
/*OUTPUT
Page 39 of 91
PROGRAM OBJECTIVE
10- Write a C program to check whether entered year is leap year or not
Page 40 of 91
START
Input year
YES
Is
year % 400 Print “ Leap Year”
== 0?
YES
Is year
Print” Not a Leap
%100N=O=0?
Year”
NO
YES
Is year % 4
Print” Leap Year”
==0?
NO
Print “not leap year”
STOP
Page 41 of 91
Algorithm
Page 42 of 91
/*Source code to check whether entered year is leap year or not
Name -
Section -
Roll No.-
Branch- */
#include <stdio.h>
int main()
{
int year;
printf("Enter a year to check if it is a leap year\n");
scanf("%d", &year);
if ( year%400 == 0)
printf("%d is a leap year.\n", year);
else if ( year%100 == 0)
printf("%d is not a leap year.\n", year);
else if ( year%4 == 0 )
printf("%d is a leap year.\n", year);
else
printf("%d is not a leap year.\n", year);
return 0;
}
Page 43 of 91
PROGRAM OBJECTIVE
11- Write a C program to input the three sides of a triangle and then calculate area
(Apply proper validation).
Page 44 of 91
Start
Input a , b 1
,c5
Is (a + b)> c && (b +
c) >a && (a + c)>b? Print “triangle is
invalid”
YES
S = (a + b + c)/2
Print Area
Stop
Page 45 of 91
Algorithm
Page 46 of 91
/*Source code to calculate area by applying validations
Name -
Section -
Roll No.-
Branch - */
#include<stdio.h>
#include<math.h>
int main()
{
int a,b,c;
float s,A;
printf("\n Enter the value of the sides of the triangle:");
scanf("%d%d%d",&a,&b,&c);
if((a+b>c) && (b+c>a) && (a+c>b))
{
s=(float)(a+b+c)/2;
A=sqrt(s*(s-a)*(s-b)*(s-c));
printf("\n The area of the triangle is:%f",A);
}
else
printf("\n The triangle is not valid");
return 0;
}
/*OUTPUT
Enter the value of the sides of the triangle:
111
The area of the triangle is:0.433013
Page 47 of 91
PROGRAM OBJECTIVE
12. Write a C program to find roots of a quadratic equation. Find roots only when
discriminant is positive.
Page 48 of 91
Start
Input a,b,c
D = b*b-4*a*c
Is no Print “No
D>0? Real roots”
yes
r1 = (-b+sqrt(D))/2*a
r2 = (-b-sqrt(D))/2*a
Print r1, r2
Stop
Page 49 of 91
ALGORITHM
To find roots of a quadratic equation.
Page 50 of 91
/*Source code to find roots of a quadratic equation. Find roots only when
discriminant is positive.
Name -
Section -
Roll No.-
Branch - */
#include<stdio.h>
#include<math.h>
int main()
{
float a,b,c,r1,r2,d;
printf("Enter the values for equation:");
scanf("%f%f%f",&a,&b,&c);
d=b*b-4*a*c;
if(d>0)
{
r1=(-b+sqrt(d))/2*a;
r2=(-b-sqrt(d))/2*a;
printf("roots are real and unequal\n");
printf("%f\n%f\n",r1,r2);
}
else
printf("roots are imaginary");
return 0;
}
/*OUTPUT
Enter the values for equation:
131
roots are real and unequal
-1.881966
-4.118034
13. Write a C program to find whether entered character is small case, capital case, a
digit or a special symbol.
Page 52 of 91
Start
Input c
Yes
Is
c>=’a’ and Print “small case”
c<=’z’?
No
Is Yes
c>=’A’ Print “capital case”
c<=’Z’?
No
Yes
Is
c>=’0’ and Print “digit”
c<=’9’?
No
Stop
Page 53 of 91
Algorithm
Step1. Input c
Step2. Is (c>=’a’ and c<=’z’) then
Begin
Print “small case”
End
Else If (c>=’A’ and c<=’Z’) then
Begin
Print “capital case”
End
Else if (c>=’0’ and c<=’9’) then
Begin
Print “digit value”
End
Else
Begin
Print “ special symbol”
End
Page 54 of 91
/*Source code to find whether entered character is small case, capital case, a digit
or a special symbol.
int main()
{
char c;
printf("Enter a character:");
scanf("%c",&c);
if((c>='a' && c<='z'))
printf("Small case letter");
else if((c>='A' && c<='Z'))
printf("Capital case letter");
else if((c>='0' && c<='9'))
printf("Digit");
else
printf("Special symbol");
return 0;
}
/*OUTPUT
Enter a character:a
Small case letter
Enter a character:S
Capital case letter
Enter a character:5
Digit
Enter a character:;
Special symbol */
Page 55 of 91
PROGRAM OBJECTIVE
Page 56 of 91
14. Write a C program to find the sum of even and odd numbers between x and y.
Page 57 of 91
Start
Input x, y
i=x
N
Is Print sume,sumo
i<=y?
Is i%2==0 N Sumo=sumo+i
Sume=sume+i
i=i+1
Stop
Page 58 of 91
Algorithm
To find the sum of even and odd numbers between x and y
Page 59 of 91
/*Source Code to find the sum of even and odd numbers between x and y.
Name -
Section -
Roll No.-
Branch - */
#include<stdio.h>
#include<conio.h>
int main()
{
int i,x,y,sume=0,sumo=0;
printf("Enter range");
scanf("%d%d",&x,&y);
i=x;
while(i<=y)
{
if(i%2==0)
sume=sume+i;
else
sumo=sumo+i;
i++;
}
printf("\nSum of even numbers is: %d",sume);
printf("\nSum of odd numbers is: %d",sumo);
getch();
return 0;
}
/*OUTPUT
Enter range
14
Sum of even numbers is: 6
Sum of odd numbers is: 4 */
Page 60 of 91
PROGRAM OBJECTIVE
Page 61 of 91
Start
Input n
Input num
Print num*num
Stop
Page 62 of 91
Algorithm
Step 1: Input n
Step 2: Repeat for i=1,2,3,…….n
Begin
Input num
Print num*num
End
Page 63 of 91
/*Source code to print square of n numbers, where value of n is inputted by the
user.
Name -
Section -
Roll No.-
Branch - */
#include<stdio.h>
int main()
{
int i,n,num;
printf("enter the number of terms");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("enter the number");
scanf("%d",&num);
printf("The square is %d \n",num*num);
}
return 0;
}
/*OUTPUT
The square is 25
*/
Page 64 of 91
PROGRAM OBJECTIVE
16. Write a C program to print Fibonacci series.
Page 65 of 91
Algorithm
To print Fibonacci series
Step 1.Input n
Step 2. c=0
Step 3. While c<n-1 repeat step 4, 5
Step 4. Is (c<=1) then
Begin
Next=c
End
Else
Begin
Next=first+ second
First=second
Second=next
End
Print next
Step 5. c=c+1
Page 66 of 91
Start
Input n
c=0
Is c<=n-1 N
Stop
?
Next=first+ second
Is N
c<=1 ?
First=second
Second=next
Y
Next=c
Print Next
c=c+1
Page 67 of 91
/*Source code to print Fibonacci series.
0 1 1 2 3 5 8 13…………………
Name -
Section -
Roll No.-
Branch - */
#include<conio.h>
#include<stdio.h>
int main()
{
int n, first = 0, second = 1, next, c;
printf("Enter the number of terms\n");
scanf("%d",&n);
printf("First %d terms of Fibonacci series are :-\n",n);
c=0;
while(c<=n-1)
{
if ( c <= 1 )
next = c;
else
{
next=first + second;
first=second;
second=next;
}
printf(" %d",next);
c++;
}
getch();
return 0;
}
/*OUTPUT
Enter the number of terms
4
First 4 terms of Fibonacci series are :-
0112 */
Page 68 of 91
PROGRAM OBJECTIVE
17. Write a C program to find the sum of digits of a number.
Page 69 of 91
Algorithm
To find the sum of digit of a number.
Step1: input n
Step2: while(n
Step3: remainder=n%10
sum= sum + remainder
n =n/10
step4: Print sum
Page 70 of 91
Start
Input n
is n 0
? No
Yes
remainder = n%10
sum=sum + remainder
n =n/10
Print sum
Stop
Page 71 of 91
/*Source code to find the sum of digits of a number.
Name -
Section -
Roll No.-
Branch - */
#include<stdio.h>
#include<conio.h>
int main()
{
int n, sum = 0, remainder;
printf("Enter an integer\n");
scanf("%d",&n);
while(n != 0)
{
remainder = n % 10;
sum = sum + remainder;
n = n / 10;
}
printf("Sum of digits of entered number = %d\n",sum);
getch();
return 0;
}
/*OUTPUT
Enter an integer
123
Sum of digits of entered number = 6 */
Page 72 of 91
PROGRAM OBJECTIVE
Page 73 of 91
Algorithm
to check Armstrong number
Page 74 of 91
Start
Input num
temp=num
Is No
num != 0?
Yes
rem=num%10
sum=sum+(rem*rem*rem)
num=num/10
Is sum==temp
?
Print “not an Armstrong no.”
stop
Page 75 of 91
/*Source code to find whether a given number is Armstrong number or not
Name -
Section -
Roll No.-
Branch - */
#include<stdio.h>
#include<conio.h>
int main()
{
int num, sum = 0, temp, rem;
printf("Enter an integer\n");
scanf("%d",&num);
temp=num;
while( num != 0 )
{
remainder=num%10;
sum=sum+rem*rem*rem;
num=num/10;
}
if(temp==sum )
printf("Entered number is an armstrong number.\n");
else
printf("Entered number is not an armstrong number.\n");
getch();
return 0;
}
/*OUTPUT
Enter an integer
153
Entered number is an armstrong number.
Enter an integer
675
Entered number is not an armstrong number. */
Page 76 of 91
PROGRAM OBJECTIVE
19. Write a c program to find whether a number is palindrome or not.
Page 77 of 91
Algorithm
To find whether a given number is palindrome or not.
Page 78 of 91
start
Input num
temp = num
no
Is num
!= 0 ?
yes
rem = num% 10
num = num/10
Is rev = no
print” not a
=temp?
palindrome”
yes
stop
Page 79 of 91
/*Source code to find whether a given number is palindrome or not.
Name -
Section -
Roll No.-
Branch - */
#include <stdio.h>
#include<conio.h>
int main()
{
int num,rem,temp,rev=0;
printf("Enter a number to check if it is a palindrome or not\n");
scanf("%d",&num);
temp=num;
while(num!=0)
{
rem=num%10;
rev=(rev*10)+rem;
num=num/10;
}
if(rev==temp)
printf("%d is a palindrome number.\n", num);
else
printf("%d is not a palindrome number.\n", num);
getch();
return 0;
}
/*OUTPUT
20- Write a C program to print the following pattern according to the number of
rows entered.
*****
****
***
**
*
Page 81 of 91
Algorithm
To print the pattern.
Step 1- Input n
Step 2- c=0
Step 3- while (c<=n-1) do step 4-9
Step 4- j=1
Step 5- while (j<=c)
begin
Print space
j = j+1
end
Step 6- k= 1
Step 7-while (k<=n-c)
begin
Print “*”
k=k+1
end
Step 8-Print newline
Step 9- c=c+1
Page 82 of 91
Start
Input n
c=0
Is c<=n- c =c+1
Stop 1?
Print newline
j=1 N
N
Is j<=c Is k<=
K=1
n- c ?
?
Y Y
Print space
Print “*”
J=j+1
K = k+1
Page 83 of 91
/*Source code to print the following pattern according to number of rows entered
*****
****
***
**
*
Name -
Section -
Roll No.-
Branch - */
#include <stdio.h>
int main()
{
int n,j,c=0,k;
printf("Enter number of rows\n");
scanf("%d",&n);
while(c<=n-1)
{
j=1;
while(j<=c)
{
printf(" ");
j++;
}
k=1;
while(k<=n-c)
{
printf("*");
k++;
}
printf("\n");
c++;
}
return 0;
}
Page 84 of 91
START
c=0
Input n
Input arr[i]
Input key
is arr[i] N
==key?
Y
Print “Element found at index:” i
c=c+1
Is N
c==0?
Y
Print “Element not found”
STOP
1: /*21. Write a c Program to search an element in a 1D array*/
2:
3: #include<stdio.h>
4: int main()
5: {
6: int arr[50],i,n,key,c=0;
7: printf("Enter size of array : ");
8: scanf("%d",&n);
9: printf("Enter elements of array : \n");
10: for(i=0;i<n;i++)
11: scanf("%d",&arr[i]);
12:
13: printf("Enter Element to Be Searched : ");
14: scanf("%d",&key);
15: for(i=0;i<n;i++)
16: {
17: if(arr[i]==key)
18: {
19: printf("Element found at %d index\n",i);
20: c++;
21: }
22: }
23: if(c==0)
24: printf("Element Not Found");
25: return 0;
26: }
START
input n
input arr[i]
sort(arr,n)
print arr[i]
STOP
sort(arr[], n)
Is
arr[j]>arr[j+1] NO
?
YES
temp=arr[j]
arr[j]=arr[j+1]
arr[j+1]=temp
1: /*22. Write a c program to perform sorting on 1-D array */
2:
3: #include<stdio.h>
4: void sort(int [],int );
5: int main()
6: {
7: int arr[50],n,i,j,temp;
8: printf("Enter no. of elements:");
9: scanf("%d",&n);
10: printf("Enter %d elements in array",n);
11: for(i=0;i<n;i++)
12: {
13: scanf("%d",&arr[i]);
14: }
15: sort(arr,n);
16: printf("Array Elements after Sorting");
17: for(i=0;i<n;i++)
18: {
19: printf("\n%d",arr[i]);
20: }
21: return 0;
22: }
23:
24: void sort(int arr[50],int n)
25: {
26: int temp,i,j;
27: for(i=0;i<n;i++)
28: {
29: for(j=0;j<n-1;j++)
30: {
31: if(arr[j]>arr[j+1])
32: {
33: temp=arr[j];
34: arr[j]=arr[j+1];
35: arr[j+1]=temp;
36: }
37: }
38: }
39: }
START
input num
p=find_prime(num)
Is NO
p==1 print “not prime”
?
YES
STOP
find_prime(num)
Is YES
num==1 return 0
?
NO
for(i=2 to num/2 Step 1)
Is YES
num%i==0 return 0
?
NO
return 1
1: /*23. Write a c program having function find_prime that returns 1
2: if its argument is a prime number and returns 0 otherwise */
3:
4: #include<stdio.h>
5: int find_prime(int);
6: int main()
7: {
8: int num,p;
9: printf("Enter any no.:");
10: scanf("%d",&num);
11: p=find_prime(num);
12: if(p==1)
13: printf("%d is a Prime Number",num);
14: else
15: printf("%d is not a Prime Number",num);
16: return 0;
17: }
18: int find_prime(int num)
19: {
20: int i;
21: if(num==1)
22: return 0;
23: else{
24: for(i=2;i<=num/2;i++)
25: {
26: if(num%i==0)
27: return 0;
28: }
29: }
30: return 1;
31: }
START
Input n
ans=sum(n)
Print ans
STOP
sum(n)
N
Is
return 0
n>0?
YES
return n%10+sum(n/10)
1: /*24. Write a C program to calculate sum of digit of a given number
2: using recursion*/
3:
4: int sum(int);
5: #include<stdio.h>
6: int main()
7: {
8: int n,ans;
9: printf("Enter no. \n");
10: scanf("%d",&n);
11: ans=sum(n);
12: printf("sum of digit of given no. is %d",ans);
13: return 0;
14: }
15: int sum(int n)
16: {
17: if(n>0)
18: return n%10+sum(n/10);
19: else
20: return 0;
21: }
Program-25
WAP for storing n integer value in an array then reverse this array without using any other array. Show
array value before and after its reversing.
#include<stdio.h>
#include<conio.h>
void main()
{
int x[100],i,n,j,t;
clrscr();
printf("enter how many terms in array=");
scanf("%d",&n);
printf("\nenter the value in array=");
for(i=0;i<n;i++)
{
scanf("%d",&x[i]);
}
printf("\n original array is=");
for(i=0;i<n;i++)
{
printf("%d\n",x[i]);
}
j=n-1;
for(i=0;i<j;i++)
{
t=x[i];
x[i]=x[j];
x[j]=t;
j--;
}
printf("\n reverse array is=\n");
for(i=0;i<n;i++)
{
printf("%d\n",x[i]);
}
getch();
}
Program-26
WAP for storing n integer value in an array then arrange them in increasing order using BUBBLE sort
technique.
#include<stdio.h>
#include<conio.h>
void main()
{
int x[100],n,i,j,t;
clrscr();
printf("how many elements in array=");
scanf("%d",&n);
printf("\nenter the value of array=");
for(i=0;i<n;i++)
{
scanf("%d",&x[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n-1-i;j++)
{
if(x[j+1]<x[j])
{
t=x[j];
x[j]=x[j+1];
x[j+1]=t;
}
}
}
printf("array after shorting are=");
for(i=0;i<n;i++)
{
printf("\n%d",x[i]);
}
getch();
}
Program-27
WAP for storing integer value in matrix of size r*c. where r is number of rows and c is number of
columns. Then obtain and display Transpose of this matrix.
#include<stdio.h>
void main()
{
int r, c, i, j, matrix[10][10], transpose[10][10];
clrscr();
printf("Enter the number of rows and columns of matrix ");
scanf("%d%d",&r,&c);
printf("Enter the elements of matrix \n");
for( i = 0 ; i < r ; i++ )
{
for( j = 0 ; j< c ; j++ )
{
scanf("%d",&matrix[i][j]);
}
}
printf("original matrix is=\n");
for( i = 0 ; i < r ; i++ )
{
for( j = 0 ; j< c ; j++ )
{
printf("%d\t",matrix[i][j]);
}
printf("\n");
}
for( i = 0 ; i < r ; i++ )
{
for( j = 0 ; j < c ; j++ )
{
transpose[j][i] = matrix[i][j];
}
}
printf("Transpose of entered matrix :-\n");
for( i = 0 ; i < c ; i++ )
{
for( j = 0 ; j < r ; j++ )
{
printf("%d\t",transpose[i][j]);
}
printf("\n");
}
getch();
}
Program-28
WAP for getting any string from user then find out whether this string is palindrome or not.
#include<stdio.h>
#include<conio.h>
void main()
{
char st[100];
int n,i,j,f=0;
clrscr();
printf("enter any string=");
gets(st);
n=strlen(st);
i=0;
j=n-1;
while(i<j)
{
if(st[i]==st[j])
{
f=1;
break;
}
i++;
j--;
}
if(f==1)
printf("\n string in palindrome");
else
printf("\n not palindrome");
getch();
}
Program-29
WAP for getting any string from user then display it in reverse.
//#include<stdio.h>
//#include<conio.h>
void main()
{
char st[100],ch;
int n,i,j;
clrscr();
printf("enter any string=");
gets(st);
n=0;
while(st[n]!='\0')
{
n++;
}
i=0;
j=n-1;
while(i<j)
{
ch=st[i];
st[i]=st[j];
st[j]=ch;
i++;
j--;
}
printf("reverse is=%s",st);
//puts(st);
getch();
}
Program-30
WAP for getting any string from user than display it in opposite case.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char st[100];
int i;
clrscr();
printf("enter any string=");
//scanf("%s",st);
gets(st);
printf("\n string in original case=");
//printf("%s",st);
puts(st);
for(i=0;st[i]!='\0';i++)
{
if(st[i]>='A'&&st[i]<='Z')
{
st[i]=st[i]+32;
}
else
{
if(st[i]>='a'&&st[i]<='z')
{
st[i]=st[i]-32;
}
}
}
printf("\n string in opposit case=");
//printf("%s",st);
puts(st);
getch();
}
Program-31
WAP for getting any string from user then find out whether this string is palindrome or not using
predefine function.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char st1[100],st2[100];
clrscr();
printf("enter any string=");
gets(st1);
strcpy(st2,st1);
strrev(st2);
if(strcmp(st1,st2)==0)
printf("\n string is palindrome");
else
printf("\n string is not palindrome");
getch();
}
Program-32
WAP for getting any string from user then displays it in reverse using predefine function.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char st[100];
clrscr();
printf("enter any string=");
gets(st);
strrev(st);
puts(st);
getch();
}
Program-33
#include<stdio.h>
#include<conio.h>
float add(float,float);
void main()
{
float a,b,c;
clrscr();
printf("Enter the value for a & b\n\n");
scanf("%f%f",&a,&b);
c=add(a,b);
printf("\nc=%f",c);
getch();
}
float add(float x,float y)
{
float z;
z=x+y;
return(z);
}
Program-34
WAP to calculate area of rectangle using user defined function which received and return value.
#include<stdio.h>
#include<conio.h>
float area(float,float);
void main()
{
float x,y,z;
clrscr();
printf("enter length and breadth of rectangle=");
scanf("%f%f",&x,&y);
z=area(x,y);
printf("\n area of ractangle is=%f",z);
getch();
}
float area(float l,float b)
{
float a;
a=l*b;
return a;
}
Program-35
#include<stdio.h>
int fibo(int n);
void main()
{
int n,i;
printf("Enter the limit:\n");
scanf("%d",&n);
i=fibo(n);
getch();
}
int fibo(int n)
{
int a=0,b=1,c,i=1;
while(i<=n)
{
c=a+b;
a=b;
b=c;
i++;
printf("%d\t",c);
}
return c;
}
Program-37
WAP for getting a number n and its power p from user then calculate value of np using recursion
function.
#include<stdio.h>
#include<conio.h>
void main()
{
int power(int,int);
int n,p,a;
clrscr();
printf("enter the number and its power=");
scanf("%d\t%d",&n,&p);
a=power(n,p);
printf("\n power is=%d",a);
getch();
}
int power(int n,int p)
{
if(p==1)
{
return n;
}
else
{
return (n*power(n,p-1));
}
}
Program-38
#include <stdio.h>
swap (int, int);
main()
{
int a, b;
printf("\nEnter value of a & b: ");
scanf("%d %d", &a, &b);
printf("\nBefore Swapping:\n");
printf("\na = %d\n\nb = %d\n", a, b);
swap(a, b);
printf("\nAfter Swapping:\n");
printf("\na = %d\n\nb = %d", a, b);
getch();
}
swap (int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
}
Program-40
#include <stdio.h>
swap (int *, int *);
main()
{
int a, b;
printf("\nEnter value of a & b: ");
scanf("%d %d", &a, &b);
printf("\nBefore Swapping:\n");
printf("\na = %d\n\nb = %d\n", a, b);
swap(&a, &b);
printf("\nAfter Swapping:\n");
printf("\na = %d\n\nb = %d\n", a, b);
swap(&a, &b);
printf("\nAfter Swapping:\n");
printf("\na = %d\n\nb = %d", a, b);
getch();
}
swap (int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
Program-41
#include<stdio.h>
#include<conio.h>
void main()
{
int a,l,b,p,*x,*y,*z,*w;
clrscr();
x=&a;
y=&l;
z=&b;
w=&p;
printf("\n enter lenght and breadth of rectangle=");
scanf("%d%d",&*y,&*z);
*x=*y**z;
w=2*(*y+*z);
printf("\n area=%d",*x);
printf("\n perimeter=%d",*w);
getch();
}
Program-42
WAP for getting two distances in km, meter and cm from user. Add them and obtain total
distance in km, meter and cm format and also show total distance.
#include<stdio.h>
#include<conio.h>
struct distance
{
int k,m,c;
};
void main()
{
struct distance d1,d2,d3;
clrscr();
printf("enter km,mtr and cm of first distance=");
scanf("%d%d%d",&d1.k,&d1.m,&d1.c);
printf("enter km,mtr and cm of second distance=");
scanf("%d%d%d",&d2.k,&d2.m,&d2.c);
d3.k=d1.k+d2.k;
d3.m=d1.m+d2.m;
d3.c=d1.c+d2.c;
if(d3.c>=100)
{
d3.c=d3.c-100;
d3.m++;
}
if(d3.m>=1000)
{
d3.m=d3.m-1000;
d3.k++;
}
printf("\n total distance is=");
printf("%dkm %dmetre %dcm",d3.k,d3.m,d3.c);
getch();
}
Program-43
WAP for getting name and 3 subject marks for any student. Calculate percentage of marks
for him and assign a grade A, B, C, D according to following criteria.
Percentage Grade
1-33 D
34-45 C
46-59 B
60-100 A
Finally display records of student with calculated percentage and grade.
#include<stdio.h>
struct student
{
char name[30];
int m1,m2,m3;
float per;
char gd;
};
void main()
{
struct student x;
printf("\n enter name and three subject marks of student=");
gets(x.name);
scanf("%d%d%d",&x.m1,&x.m2,&x.m3);
x.per=(x.m1+x.m2+x.m3)/3;
if(x.per<=33)
x.gd='D';
else if(x.per<=45)
x.gd='C';
else if(x.per<=59)
x.gd='B';
else
x.gd='A';
printf("\n records of students=");
printf("\n name=");
puts(x.name);
printf("\n marks=%d\t%d\t%d",x.m1,x.m2,x.m3);
printf("\n percentage=%f",x.per);
printf("\n grade is=%c",x.gd);
getch(); }
Program-44
WAP for getting Roll no., name and percentage of several students then get a particulate
roll no. from user and show name and percentage of student having particular roll no. In
case of absence display proper message.
#include<stdio.h>
#include<conio.h>
struct student
{
int rn;
char name[30];
int per;
};
void main()
{
struct student x[100];
int n,i,f,roll;
clrscr();
printf("enter no. of students=");
scanf("%d",&n);
printf("\n enter rn,name,per of student");
for(i=0;i<n;i++)
{
printf("\nroll number is=");
scanf("%d",&x[i].rn);
printf("name is=");
scanf("%s",&x[i].name);
printf("percentage is=");
scanf("%d",&x[i].per);
printf("-------------------");
}
f=0;
printf("\nenter any roll number for search in records=");
scanf("%d",&roll);
for(i=0;i<n;i++)
{
if(roll==x[i].rn)
{
f=1;
printf("\nname is=%s",x[i].name);
printf("\n percentage=%d",x[i].per);
break;
}
}
if(f==0)
printf("\n roll number dosnt exist in our records");
getch();
}
Program-45
WAP for getting any string from user and store all upper case letter of the string in file
“up.txt”, lower case alphabet in “lw.txt”, digit in “dg.txt” and all special character in
“sp.txt”.
#include<stdio.h>
void main()
{
char st[100];
FILE *a,*b,*c,*d;
int i,l;
clrscr();
a=fopen("up.txt","w");
b=fopen("lw.txt","w");
c=fopen("dg.txt","w");
d=fopen("sp.txt","w");
printf("enter any string\n\n");
gets(st);
l=strlen(st);
for(i=0;i<l;i++)
{
if(st[i]>='A'&&st[i]<='Z')
putc(st[i],a);
else if(st[i]>='a'&&st[i]<='z')
putc(st[i],b);
else if(st[i]>='0'&&st[i]<='9')
putc(st[i],c);
else
putc(st[i],d);
}
fclose(a);
fclose(b);
fclose(c);
fclose(d);
getch();
}
Program-46
#include <stdio.h>
void main()
{
FILE *fp1, *fp2;
char ch;
fp1 = fopen("abc.txt", "r");
fp2 = fopen("xyz.txt", "w");
while((ch = getc(fp1)) != EOF)
putc(ch, fp2);
fclose(fp1);
fclose(fp2);
getch();
}
Program-47
#include <stdio.h>
main()
{
FILE *fptr;
char ch;
printf("\nEnter any character: ");
scanf("%c", &ch);
fptr = fopen("abc.txt", "w");
putc(ch, fptr);
fclose(fptr);
fptr = fopen("abc.txt", "r");
ch = getc(fptr);
printf("\nCharacter: %c", ch);
getch();
}
Program-48
#include <stdio.h>
#define SIZE 50
main()
{
FILE *fptr;
char s1[SIZE], s2[SIZE];
printf("\nEnter any string: ");
gets(s1);
fptr = fopen("abc.txt", "w");
fputs(s1, fptr);
fclose(fptr);
fptr = fopen("abc.txt", "r");
fgets(s2, SIZE, fptr);
printf("\nYou entered:");
puts(s2);
getch();
}