PPSP Lab
PPSP Lab
sNo=1&qId=6014e0b598ee190668074059&bd=AY3RFZHVEQg%3D%3D&lid=6014e…
S.No: 1 Exp. Name: Program to find number of days in a given month of a given year. Date:
Aim:
Page No:
Write a C Program to find the number of days in a given month of a given year. Print the output as shown in
the test cases.
Source Code:
ID: 1075
numberOfDays.c
#include <stdio.h>
void main()
{
int m,y,nd;
printf("Enter month and year: ");
scanf("%d%d",&m,&y);
if(m==2)
{
if(y%4==0&&y%100!=0||y%400==0)
{
nd=29;
}
else
{
nd=28;
}
}
else
{
if(m==4||m==6||m==9||m==11)
{
nd=30;
}
else
{
nd=31;
}
}
printf("%d\n",nd);
}
G Pulla Reddy Engineering College (Autonomous)
Test Case - 1
User Output
Enter month and year: 2 2004
29
Test Case - 2
User Output
https://gprec.codetantra.com/secure/labs-q.jsp?sNo=1&qId=6014e0b598ee190668074059&bd=AY3RFZHVEQg%3D%3D&lid=6014e94fb65a870… 1/2
2/8/2021 https://gprec.codetantra.com/secure/labs-q.jsp?sNo=1&qId=6014e0b598ee190668074059&bd=AY3RFZHVEQg%3D%3D&lid=6014e…
Test Case - 2
Enter month and year: 2 2021
28
Page No:
ID: 1075
G Pulla Reddy Engineering College (Autonomous)
https://gprec.codetantra.com/secure/labs-q.jsp?sNo=1&qId=6014e0b598ee190668074059&bd=AY3RFZHVEQg%3D%3D&lid=6014e94fb65a870… 2/2
2/8/2021 https://gprec.codetantra.com/secure/labs-q.jsp?sNo=2&qId=60178f56d9e4fa0619226728&bd=AY3RFZHVEQg%3D%3D&lid=6014e9…
Aim:
Page No:
Program to read angles or sides of a triangle (based on options: 1) angles, 2) sides) and print if it is equilateral
or isosceles or isosceles perpendicular or just perpendicular or scalene triangle.
Source Code:
ID: 1075
triangles.c
#include <stdio.h>
#include <math.h>
void main()
{
int ch,a1,a2,a3,s1,s2,s3;
printf("Find the type of triangle based on angles or sides.\n");
printf("1. Angles\n2. Sides\n");
printf("Enter your choice: ");
scanf("%d",&ch);
if(ch==1)
{
printf("Enter the first angle: ");
scanf("%d",&a1);
printf("Enter the second angle: ");
scanf("%d",&a2);
printf("Enter the third angle: ");
scanf("%d",&a3);
if(a1+a2+a3==180)
{
if(a1==a2&&a2==a3)
{
printf("The triangle is: Equilateral triangle\n");
}
else if((a1==a2||a2==a3||a1==a3)&&(a1==90||a2==90||a3==90))
{
printf("The triangle is: Isosceles Perpendicular triangle\n");
}
else if(a1==a2||a2==a3||a1==a3)
{
printf("The triangle is: Isosceles triangle\n");
}
else if(a1==90||a2==90||a3==90)
G Pulla Reddy Engineering College (Autonomous)
{
printf("The triangle is: Perpendicular triangle\n");
}
else
{
printf("the triangle is: Scalene triangle\n");
}
}
else
{
printf("It is not a triangle.\n");
}
}
else if(ch==2)
{
https://gprec.codetantra.com/secure/labs-q.jsp?sNo=2&qId=60178f56d9e4fa0619226728&bd=AY3RFZHVEQg%3D%3D&lid=6014e94fb65a8706… 1/3
2/8/2021 https://gprec.codetantra.com/secure/labs-q.jsp?sNo=2&qId=60178f56d9e4fa0619226728&bd=AY3RFZHVEQg%3D%3D&lid=6014e9…
printf("Enter the first side: ");
scanf("%d",&s1);
printf("Enter the second side: ");
scanf("%d",&s2);
printf("Enter the third side: ");
Page No:
scanf("%d",&s3);
if((s1+s2>s3)&&(s2+s3>s1)&&(s1+s3>s2))
{
if(s1==s2&&s2==s3)
ID: 1075
{
printf("The triangle is: Equilateral triangle\n");
}
else if((s1==s2||s2==s3||s1==s3)&&(s1*s1==s2*s2+s3*s3||s2*s2==s1*s1+s3*s3||s3*
s3==s1*s1+s2*s2))
{
printf("The triangle is: Isosceles perpendicular triangle\n");
}
else if(s1==s2||s2==s3||s1==s3)
{
printf("The triangle is: Isosceles triangle\n");
}
else if(s1*s1==s2*s2+s3*s3||s2*s2==s1*s1+s3*s3||s3*s3==s1*s1+s2*s2)
{
printf("The triangle is: Perpendicular triangle\n");
}
else
{
printf("The triangle is: Scalene triangle\n");
}
}
else
{
printf("It is not a triangle.\n");
}
}
else
{
printf("Invalid choice!!!\n");
}
}
G Pulla Reddy Engineering College (Autonomous)
Test Case - 1
User Output
Find the type of triangle based on angles or sides. 1
1. Angles 1
2. Sides 1
Enter your choice: 1
Enter the first angle: 60
Enter the second angle: 60
Enter the third angle: 60
The triangle is: Equilateral triangle
https://gprec.codetantra.com/secure/labs-q.jsp?sNo=2&qId=60178f56d9e4fa0619226728&bd=AY3RFZHVEQg%3D%3D&lid=6014e94fb65a8706… 2/3
2/8/2021 https://gprec.codetantra.com/secure/labs-q.jsp?sNo=2&qId=60178f56d9e4fa0619226728&bd=AY3RFZHVEQg%3D%3D&lid=6014e9…
Test Case - 2
User Output
Find the type of triangle based on angles or sides. 2
Page No:
1. Angles 2
2. Sides 2
Enter your choice: 2
Enter the first side: 60
ID: 1075
Enter the second side: 50
Enter the third side: 60
The triangle is: Isosceles triangle
Test Case - 3
User Output
Find the type of triangle based on angles or sides. 3
1. Angles 3
2. Sides 3
Enter your choice: 3
Invalid choice!!!
Test Case - 4
User Output
Find the type of triangle based on angles or sides. 1
1. Angles 1
2. Sides 1
Enter your choice: 1
Enter the first angle: 45
Enter the second angle: 90
Enter the third angle: 45
The triangle is: Isosceles Perpendicular triangle
Test Case - 5
User Output
Find the type of triangle based on angles or sides. 2
1. Angles 2
2. Sides 2
Enter your choice: 2
G Pulla Reddy Engineering College (Autonomous)
https://gprec.codetantra.com/secure/labs-q.jsp?sNo=2&qId=60178f56d9e4fa0619226728&bd=AY3RFZHVEQg%3D%3D&lid=6014e94fb65a8706… 3/3
2/8/2021 https://gprec.codetantra.com/secure/labs-q.jsp?sNo=3&qId=6014e5dc98ee190668074e61&bd=AY3RFZHVEQg%3D%3D&lid=6014e…
Exp. Name: Program to calculate the area and perimeter of different shapes using
S.No: 3 Date:
switch statement
Aim:
Page No:
Write a C program to calculate the area and perimeter of different shapes using switch statement.
1. Triangle
2. Rectangle
ID: 1075
3. Square
4. Circle.
areaAndPerimeter.c
#include <stdio.h>
void main()
{
int op;
float b,h,s1,s2,s3,l,r;
float a,p;
printf("Enter your option\n");
printf("1. triangle\n2. square\n3. circle\n4. rectangle\n5. exit\n");
scanf("%d",&op);
while(op<5)
{
switch(op)
{
case 1:printf("Enter base and height of a triangle: ");
scanf("%f%f",&b,&h);
a=0.5*b*h;
printf("Area of triangle: %f\n",a);
printf("Enter sides of triangle: ");
scanf("%f%f%f",&s1,&s2,&s3);
p=s1+s2+s3;
printf("Perimeter of triangle: %f\n",p);
break;
case 2:printf("Enter the length of a square: ");
scanf("%f",&l);
a=l*l;
p=4*l;
printf("Area of square: %f\n",a);
G Pulla Reddy Engineering College (Autonomous)
https://gprec.codetantra.com/secure/labs-q.jsp?sNo=3&qId=6014e5dc98ee190668074e61&bd=AY3RFZHVEQg%3D%3D&lid=6014e94fb65a870… 1/3
2/8/2021 https://gprec.codetantra.com/secure/labs-q.jsp?sNo=3&qId=6014e5dc98ee190668074e61&bd=AY3RFZHVEQg%3D%3D&lid=6014e…
printf("Perimeter of rectangle: %f\n",p);
}
printf("Enter your option\n");
printf("1. triangle\n2. square\n3. circle\n4. rectangle\n5. exit\n");
scanf("%d",&op);
Page No:
}
}
ID: 1075
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
Enter your option 1
1. triangle 1
2. square 1
3. circle 1
4. rectangle 1
5. exit 1
Enter base and height of a triangle: 3 5
Area of triangle: 7.500000 3 5 5
Enter sides of triangle: 3 5 5
Perimeter of triangle: 13.000000 2
Enter your option 2
1. triangle 2
2. square 2
3. circle 2
4. rectangle 2
5. exit 2
Enter the length of a square: 5
Area of square: 25.000000 3
Perimeter of square: 20.000000 3
Enter your option 3
1. triangle 3
2. square 3
3. circle 3
4. rectangle 3
5. exit 3
Enter the radius of a circle: 2.2
G Pulla Reddy Engineering College (Autonomous)
https://gprec.codetantra.com/secure/labs-q.jsp?sNo=3&qId=6014e5dc98ee190668074e61&bd=AY3RFZHVEQg%3D%3D&lid=6014e94fb65a870… 2/3
2/8/2021 https://gprec.codetantra.com/secure/labs-q.jsp?sNo=3&qId=6014e5dc98ee190668074e61&bd=AY3RFZHVEQg%3D%3D&lid=6014e…
Test Case - 1
3. circle 5
4. rectangle 5
5. exit 5
Page No:
ID: 1075
G Pulla Reddy Engineering College (Autonomous)
https://gprec.codetantra.com/secure/labs-q.jsp?sNo=3&qId=6014e5dc98ee190668074e61&bd=AY3RFZHVEQg%3D%3D&lid=6014e94fb65a870… 3/3
2/8/2021 https://gprec.codetantra.com/secure/labs-q.jsp?sNo=4&qId=6018fcca60dd3a0668c1e652&bd=AY3RFZHVEQg%3D%3D&lid=6014e9…
S.No: 4 Exp. Name: Program to Calculate Income Tax based on the given monthly salary Date:
Aim:
Page No:
Program to read monthly salary of an employee and calculate Income tax to be paid based on the following
criteria:
< 100000 per year- no tax
100001 to 200000 per year- 5%
ID: 1075
200001 to 300000 per year- 10%
300001 to 500000 per year- 20%
> 500000 per year- 30%
Source Code:
incomeCalc.c
#include <stdio.h>
#include <stdlib.h>
void main()
{
double msal,ysal,it;
printf("Enter your monthly salary: ");
scanf("%lf",&msal);
ysal=12*msal;
if(ysal<0)
{
printf("Invalid Salary!!!\n");
exit(0);
}
else if(ysal<=100000)
{
printf("You are exempted from Income Tax.\n");
}
else if(ysal<=200000)
{
it=(ysal-100000)*0.05;
}
else if(ysal<=300000)
{
it=5000+(ysal-200000)*0.1;
}
else if(ysal<=500000)
{
G Pulla Reddy Engineering College (Autonomous)
it=15000+(ysal-300000)*0.2;
}
else
{
it=55000+(ysal-500000)*0.3;
}
printf("You have to pay %lf/- as income tax\n",it);
}
Test Case - 1
https://gprec.codetantra.com/secure/labs-q.jsp?sNo=4&qId=6018fcca60dd3a0668c1e652&bd=AY3RFZHVEQg%3D%3D&lid=6014e94fb65a8706… 1/2
2/8/2021 https://gprec.codetantra.com/secure/labs-q.jsp?sNo=4&qId=6018fcca60dd3a0668c1e652&bd=AY3RFZHVEQg%3D%3D&lid=6014e9…
Test Case - 1
User Output
Enter your monthly salary: 35000
Page No:
You have to pay 39000.000000/- as income tax
Test Case - 2
ID: 1075
User Output
Enter your monthly salary: 5000
You are exempted from Income Tax.
You have to pay 0.000000/- as income tax
Test Case - 3
User Output
Enter your monthly salary: 10000
You have to pay 1000.000000/- as income tax
https://gprec.codetantra.com/secure/labs-q.jsp?sNo=4&qId=6018fcca60dd3a0668c1e652&bd=AY3RFZHVEQg%3D%3D&lid=6014e94fb65a8706… 2/2
2/8/2021 https://gprec.codetantra.com/secure/labs-q.jsp?sNo=5&qId=601797b5d9e4fa06192288ba&bd=AY3RFZHVEQg%3D%3D&lid=6014e…
Exp. Name: Program to generate a series of ‘N’ numbers based on the pattern of the
S.No: 5 Date:
numbers as : 9 13 22 36 55 79
Aim:
Page No:
Program to generate a series of ‘N’ numbers based on the pattern of the numbers as : 9 13 22 36 55 79
Source Code:
ID: 1075
series1.c
#include <stdio.h>
void main()
{
int n,term=9,gap=4,i;
printf("Enter the number of terms you want: ");
scanf("%d",&n);
printf("The series is:");
for(i=1;i<=n;i++)
{
printf(" %d",term);
term=term+gap;
gap=gap+5;
}
printf("\n");
}
Test Case - 1
User Output
Enter the number of terms you want: 6
The series is: 9 13 22 36 55 79
Test Case - 2
User Output
Enter the number of terms you want: 10
The series is: 9 13 22 36 55 79 108 142 181 225
G Pulla Reddy Engineering College (Autonomous)
Test Case - 3
User Output
Enter the number of terms you want: 20
The series is: 9 13 22 36 55 79 108 142 181 225 274 328 387 451 520 594 673 757 846 940
https://gprec.codetantra.com/secure/labs-q.jsp?sNo=5&qId=601797b5d9e4fa06192288ba&bd=AY3RFZHVEQg%3D%3D&lid=6014e94fb65a8706… 1/1
2/8/2021 https://gprec.codetantra.com/secure/labs-q.jsp?sNo=6&qId=60179b59d9e4fa061922973b&bd=AY3RFZHVEQg%3D%3D&lid=6014e…
Exp. Name: Program to generate a series of ‘N’ numbers based on the pattern of the
S.No: 6 Date:
numbers as : 1 4 8 11 22 25 50
Aim:
Page No:
Program to generate a series of ‘N’ numbers based on the pattern of the numbers as : 1 4 8 11 22 25 50
Source Code:
ID: 1075
series2.c
#include <stdio.h>
void main()
{
int term=1,gap=3,i,n;
printf("Enter the number of terms you want: ");
scanf("%d",&n);
printf("The series is:");
for(i=1;i<=n;i++)
{
printf(" %d",term);
if(i%2==1)
{
term=term+3;
}
else
{
term=term*2;
}
}
printf("\n");
}
Test Case - 1
User Output
Enter the number of terms you want: 7
The series is: 1 4 8 11 22 25 50
G Pulla Reddy Engineering College (Autonomous)
Test Case - 2
User Output
Enter the number of terms you want: 10
The series is: 1 4 8 11 22 25 50 53 106 109
Test Case - 3
User Output
Enter the number of terms you want: 20
The series is: 1 4 8 11 22 25 50 53 106 109 218 221 442 445 890 893 1786 1789 3578 3581
https://gprec.codetantra.com/secure/labs-q.jsp?sNo=6&qId=60179b59d9e4fa061922973b&bd=AY3RFZHVEQg%3D%3D&lid=6014e94fb65a8706… 1/1
2/8/2021 https://gprec.codetantra.com/secure/labs-q.jsp?sNo=7&qId=6017a493d9e4fa061922c01f&bd=AY3RFZHVEQg%3D%3D&lid=6014e9…
Exp. Name: Program to keep reading positive integers as input until you press -1 and
S.No: 7 Date:
print the average of only the prime numbers you have read.
Aim:
Page No:
Program to keep reading positive integers as input until you press -1 and print the average of only the prime
numbers you have read.
Source Code:
ID: 1075
avgPrime.c
#include <stdio.h>
void main()
{
int n,fc,pc=0;
float ps=0,pavg;
printf("Enter the digits(-1 to quit): ");
do
{
scanf("%d",&n);
fc=0;
for(int i=1;i<=n;i++)
{
if(n%i==0)
fc++;
}
if(fc==2)
{
pc++;
ps=ps+n;
}
}
while(n!=-1);
pavg=(ps)/(pc);
//printf("prime sum %f prime count %d",ps,pc);
printf("The average of the positive prime integers is: %f\n",pavg);
}
Test Case - 1
User Output
Enter the digits(-1 to quit): 3 4 5 9 6 -1
The average of the positive prime integers is: 4.000000
Test Case - 2
User Output
Enter the digits(-1 to quit): 25 52 3 31 13 8 9 1 -1
The average of the positive prime integers is: 15.666667
https://gprec.codetantra.com/secure/labs-q.jsp?sNo=7&qId=6017a493d9e4fa061922c01f&bd=AY3RFZHVEQg%3D%3D&lid=6014e94fb65a8706… 1/1
2/8/2021 https://gprec.codetantra.com/secure/labs-q.jsp?sNo=8&qId=585e40080cf2f069ef156c6c&bd=AY3RFZHVEQg%3D%3D&lid=6014e94…
Exp. Name: Write a C program to check whether the given number is Strong number
S.No: 8 Date:
or not
Aim:
Page No:
Write a program to check whether the given number is strong number or not.
[Hint: A number is called strong number if sum of the factorials of its digits is equal to number itself. For
ID: 1075
example: 145 since 1! + 4! + 5! = 1 + 24 + 120 = 145.]
At the time of execution, the program should print the message on the console as:
Enter a value :
Note: Do use the printf() function with a newline character ( \n ) at the end.
Source Code:
Program416.c
#include <stdio.h>
int fact(int);
void main()
{
int n,x,r,sum=0;
printf("Enter a value : ");
scanf("%d",&n);
x=n;
while(n!=0)
{
r=n%10;
sum=sum+fact(r);
n=n/10;
}
if(x==sum)
{
G Pulla Reddy Engineering College (Autonomous)
int fact(int n)
{
if(n==0)
return 1;
else
https://gprec.codetantra.com/secure/labs-q.jsp?sNo=8&qId=585e40080cf2f069ef156c6c&bd=AY3RFZHVEQg%3D%3D&lid=6014e94fb65a87061… 1/2
2/8/2021 https://gprec.codetantra.com/secure/labs-q.jsp?sNo=8&qId=585e40080cf2f069ef156c6c&bd=AY3RFZHVEQg%3D%3D&lid=6014e94…
return(n*fact(n-1));
}
Page No:
Execution Results - All test cases have succeeded!
Test Case - 1
ID: 1075
User Output
Enter a value : 145
The given number 145 is a strong number
Test Case - 2
User Output
Enter a value : 2
The given number 2 is a strong number
Test Case - 3
User Output
Enter a value : 56
The given number 56 is not a strong number
https://gprec.codetantra.com/secure/labs-q.jsp?sNo=8&qId=585e40080cf2f069ef156c6c&bd=AY3RFZHVEQg%3D%3D&lid=6014e94fb65a87061… 2/2
2/8/2021 https://gprec.codetantra.com/secure/labs-q.jsp?sNo=9&qId=601526b973079306665dea58&bd=AY3RFZHVEQg%3D%3D&lid=6014e…
Aim:
Page No:
Write a C program to find the number of times digit 3 occurs in each and every number from 0 to N. Given a
number N as input, count the number of 3s occurring in all the numbers from 0 to N
Source Code:
ID: 1075
digitCount.c
#include <stdio.h>
int count_3s(int);
void main()
{
int n,i,count=0;
printf("Enter the range: ");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
count=count+count_3s(i);
}
printf("Total occurrences of 3 from 0 to %d is %d\n",n,count);
}
int count_3s(int n)
{
int c=0;
while(n!=0)
{
if(n%10==3)
c++;
n=n/10;
}
return c;
}
Test Case - 1
User Output
G Pulla Reddy Engineering College (Autonomous)
Test Case - 2
User Output
Enter the range: 500
Total occurrences of 3 from 0 to 500 is 200
https://gprec.codetantra.com/secure/labs-q.jsp?sNo=9&qId=601526b973079306665dea58&bd=AY3RFZHVEQg%3D%3D&lid=6014e94fb65a870… 1/1
2/8/2021 https://gprec.codetantra.com/secure/labs-q.jsp?sNo=10&qId=601e4617cbed4c06664de8ab&bd=AY3RFZHVEQg%3D%3D&lid=6014…
S.No: 10 Exp. Name: Program to find the LCM of the given numbers Date:
Aim:
Page No:
Write a Program to find the LCM of the given numbers
Source Code:
lcm.c
ID: 1075
#include <stdio.h>
int gcd(int,int);
void main()
{
int n1,n2,lcm;
printf("Enter two integer values : ");
scanf("%d%d",&n1,&n2);
lcm=n1*n2/gcd(n1,n2);
printf("The lcm of two numbers %d and %d = %d\n",n1,n2,lcm);
}
Test Case - 1
User Output
Enter two integer values : 3 2
The lcm of two numbers 3 and 2 = 6
Test Case - 2
User Output
G Pulla Reddy Engineering College (Autonomous)
https://gprec.codetantra.com/secure/labs-q.jsp?sNo=10&qId=601e4617cbed4c06664de8ab&bd=AY3RFZHVEQg%3D%3D&lid=6014e94fb65a87… 1/1
2/8/2021 https://gprec.codetantra.com/secure/labs-q.jsp?sNo=11&qId=60152ddb73079306665e02ed&bd=AY3RFZHVEQg%3D%3D&lid=6014…
S.No: 11 Exp. Name: Program to find the sum of last two numbes in the series. Date:
Aim:
Page No:
Write a C program to print the sum of the last two numbers in the generated Fibonacci series of N terms using
recursion.
Source Code:
ID: 1075
sumOfLastNumbers.c
#include <stdio.h>
int fib(int);
void main()
{
int n,sum;
printf("Enter n: ");
scanf("%d",&n);
printf("The fibonacci series of %d terms are :",n);
for(int i=1;i<=n+1;i++)
{
if(i<=n)
printf(" %d",fib(i));
else
sum=fib(i);
}
printf("\nsum of last two numbers = %d\n",sum);
}
int fib(int n)
{
if(n==1)
return 0;
else if(n==2)
return 1;
else
return(fib(n-1)+fib(n-2));
}
Test Case - 1
User Output
Enter n: 8
The fibonacci series of 8 terms are : 0 1 1 2 3 5 8 13
sum of last two numbers = 21
Test Case - 2
User Output
Enter n: 12
The fibonacci series of 12 terms are : 0 1 1 2 3 5 8 13 21 34 55 89
sum of last two numbers = 144
https://gprec.codetantra.com/secure/labs-q.jsp?sNo=11&qId=60152ddb73079306665e02ed&bd=AY3RFZHVEQg%3D%3D&lid=6014e94fb65a87… 1/1
2/8/2021 https://gprec.codetantra.com/secure/labs-q.jsp?sNo=12&qId=5d5fd7f5e9d704759df90f4f&bd=AY3RFZHVEQg%3D%3D&lid=6014e9…
S.No: 12 Exp. Name: Write a C program to print all the Unique elements of the given array Date:
Aim:
Page No:
Write a program in C to print all unique elements of the given array.
If any element appears more than once in the array, print it only once. The elements should be printed in the
order of appearance in the array.
ID: 1075
Sample Input and Output:
uniqueElements.c
#include <stdio.h>
void main()
{
int a[100],n,i,j,k;
printf("Enter the size of array : ");
scanf("%d",&n);
printf("Enter %d elements : ",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;)
{
if(a[i]==a[j])
{
for(k=j;k<n;k++)
{
a[k]=a[k+1];
}
n--;
}
else
{
G Pulla Reddy Engineering College (Autonomous)
j++;
}
}
}
printf("Unique elements : ");
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
}
https://gprec.codetantra.com/secure/labs-q.jsp?sNo=12&qId=5d5fd7f5e9d704759df90f4f&bd=AY3RFZHVEQg%3D%3D&lid=6014e94fb65a87061… 1/2
2/8/2021 https://gprec.codetantra.com/secure/labs-q.jsp?sNo=12&qId=5d5fd7f5e9d704759df90f4f&bd=AY3RFZHVEQg%3D%3D&lid=6014e9…
Test Case - 1
User Output
Enter the size of array : 5
Page No:
Enter 5 elements : 101 111 121 121 111
Unique elements : 101 111 121
Test Case - 2
ID: 1075
User Output
Enter the size of array : 7
Enter 7 elements : 1 4 2 2 1 4 3
Unique elements : 1 4 2 3
Test Case - 3
User Output
Enter the size of array : 5
Enter 5 elements : 12 17 17 12 12
Unique elements : 12 17
Test Case - 4
User Output
Enter the size of array : 6
Enter 6 elements : -2 -5 -2 -3 -1 -5
Unique elements : -2 -5 -3 -1
Test Case - 5
User Output
Enter the size of array : 4
Enter 4 elements : -9 -9 -9 -9
Unique elements : -9
Test Case - 6
User Output
Enter the size of array : 2
G Pulla Reddy Engineering College (Autonomous)
Enter 2 elements : 25 26
Unique elements : 25 26
https://gprec.codetantra.com/secure/labs-q.jsp?sNo=12&qId=5d5fd7f5e9d704759df90f4f&bd=AY3RFZHVEQg%3D%3D&lid=6014e94fb65a87061… 2/2
2/8/2021 https://gprec.codetantra.com/secure/labs-q.jsp?sNo=13&qId=6015345773079306665e11ee&bd=AY3RFZHVEQg%3D%3D&lid=6014…
Exp. Name: Program to remove all characters in a string except alphabets and blank
S.No: 13 Date:
spaces
Aim:
Page No:
Write a C program to remove all characters in a string except alphabets and blank spaces.
Example:
Input: Pro23?g,ra4>mi:?ng i@#n& >$C
ID: 1075
Output: Programming in C
Source Code:
removeCharacter.c
#include <stdio.h>
void main()
{
char s1[100],s2[100],i,j=0;
printf("Enter a string: ");
gets(s1);
for(i=0;s1[i];i++)
{
if((s1[i]>=65&&s1[i]<=90)||(s1[i]>=97&&s1[i]<=122)||s1[i]==32)
{
s2[j++]=s1[i];
}
}
printf("Resultant String: %s\n",s2);
}
Test Case - 1
User Output
Enter a string: Pro23?g,ra4>mi:?ng i@#n& >$C
Resultant String: Programming in C
Test Case - 2
G Pulla Reddy Engineering College (Autonomous)
User Output
Enter a string: C#$ode *T58ant^%ra
Resultant String: Code Tantra
https://gprec.codetantra.com/secure/labs-q.jsp?sNo=13&qId=6015345773079306665e11ee&bd=AY3RFZHVEQg%3D%3D&lid=6014e94fb65a87… 1/1
2/8/2021 https://gprec.codetantra.com/secure/labs-q.jsp?sNo=14&qId=6018b838e8f350066cc684b2&bd=AY3RFZHVEQg%3D%3D&lid=6014e…
S.No: 14 Exp. Name: Program to find the type of given matrix. Date:
Aim:
Page No:
Program to find if the given matrix is lower triangular or upper triangular or zero matrix or just a matrix using
switch statement. A square matrix is taken as input and it is checked if it is lower triangular or upper triangular
or zero matrix or just a matrix. A square matrix is said to be a lower triangular matrix if all the entries above the
diagonal are zero. A square matrix is said to be a upper triangular matrix if all the entries below the diagonal
ID: 1075
are zero. A square matrix is said to be a zero matrix if all the entries are zeros. If not any of these three, display
it as just a square matrix.
Source Code:
matrixType.c
#include <stdio.h>
void main()
{
int a[20][20],n,i,j,ltm=0,utm=0,zm=0,op;
printf("Enter the size of square matrix: ");
scanf("%d",&n);
printf("Enter the elements of the matrix:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i<j&&a[i][j]!=0)
ltm=1;
if(i>j&&a[i][j]!=0)
utm=1;
if(a[i][j]!=0)
zm=1;
}
}
if(zm==0)
op=1;
G Pulla Reddy Engineering College (Autonomous)
else if(ltm==0&&utm==0)
op=2;
else if(ltm==0)
op=3;
else if(utm==0)
op=4;
else
op=5;
switch(op)
{
case 1:printf("The matrix is zero matrix.\n");break;
case 2:printf("The matrix is both lower and upper triangular.\n");break;
case 3:printf("The matrix is lower triangular.\n");break;
case 4:printf("The matrix is upper triangular.\n");break;
case 5:printf("The matrix is normal matrix.\n");break;
https://gprec.codetantra.com/secure/labs-q.jsp?sNo=14&qId=6018b838e8f350066cc684b2&bd=AY3RFZHVEQg%3D%3D&lid=6014e94fb65a870… 1/2
2/8/2021 https://gprec.codetantra.com/secure/labs-q.jsp?sNo=14&qId=6018b838e8f350066cc684b2&bd=AY3RFZHVEQg%3D%3D&lid=6014e…
}
Page No:
Execution Results - All test cases have succeeded!
Test Case - 1
ID: 1075
User Output
Enter the size of square matrix: 3
Enter the elements of the matrix: 1 0 0
2 6 0 2 6 0
4 2 3 4 2 3
The matrix is lower triangular.
Test Case - 2
User Output
Enter the size of square matrix: 3
Enter the elements of the matrix: 1 2 3
4 5 6 4 5 6
7 8 9 7 8 9
The matrix is normal matrix.
Test Case - 3
User Output
Enter the size of square matrix: 3
Enter the elements of the matrix: 1 2 3
0 4 5 0 4 5
0 0 7 0 0 7
The matrix is upper triangular.
Test Case - 4
User Output
Enter the size of square matrix: 4
G Pulla Reddy Engineering College (Autonomous)
Test Case - 5
User Output
Enter the size of square matrix: 2
Enter the elements of the matrix: 0 0
0 0 0 0
The matrix is zero matrix.
https://gprec.codetantra.com/secure/labs-q.jsp?sNo=14&qId=6018b838e8f350066cc684b2&bd=AY3RFZHVEQg%3D%3D&lid=6014e94fb65a870… 2/2
2/8/2021 https://gprec.codetantra.com/secure/labs-q.jsp?sNo=15&qId=6017d4659a1abc0665f46c93&bd=AY3RFZHVEQg%3D%3D&lid=6014e…
Exp. Name: Program to find the number of prime numbers and number of composite
S.No: 15 Date:
numbers in an array to which memory is allocated dynamically using pointers.
Aim:
Page No:
Program to find the number of prime numbers and number of composite numbers in an array to which memory
is allocated dynamically using pointers.
Source Code:
ID: 1075
compoPrime.c
#include <stdio.h>
int checkPrime(int);
void main()
{
int *p,n,i,pc=0,cc=0;
printf("Enter the number of elements: ");
scanf("%d",&n);
p=(int *)malloc(n*sizeof(int));
printf("Enter elements of array: ");
for(i=0;i<n;i++)
scanf("%d",p+i);
for(i=0;i<n;i++)
{
if(checkPrime(*(p+i))==2)
pc++;
else
cc++;
}
printf("Number of primes: %d\n",pc);
printf("Number of composites %d\n",cc);
}
int checkPrime(int n)
{
int i,fc=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
fc++;
}
return fc;
G Pulla Reddy Engineering College (Autonomous)
Test Case - 1
User Output
Enter the number of elements: 8
Enter elements of array: 2
3 3
4 4
5 5
https://gprec.codetantra.com/secure/labs-q.jsp?sNo=15&qId=6017d4659a1abc0665f46c93&bd=AY3RFZHVEQg%3D%3D&lid=6014e94fb65a870… 1/2
2/8/2021 https://gprec.codetantra.com/secure/labs-q.jsp?sNo=15&qId=6017d4659a1abc0665f46c93&bd=AY3RFZHVEQg%3D%3D&lid=6014e…
Test Case - 1
6 6
7 7
8 8
Page No:
9 9
Number of primes: 4
Number of composites 4
ID: 1075
Test Case - 2
User Output
Enter the number of elements: 10
Enter elements of array: 10
20 20
30 30
40 40
55 55
11 11
19 19
25 25
30 30
60 60
Number of primes: 2
Number of composites 8
https://gprec.codetantra.com/secure/labs-q.jsp?sNo=15&qId=6017d4659a1abc0665f46c93&bd=AY3RFZHVEQg%3D%3D&lid=6014e94fb65a870… 2/2
2/8/2021 https://gprec.codetantra.com/secure/labs-q.jsp?sNo=16&qId=6018c5aee8f350066cc6ba7b&bd=AY3RFZHVEQg%3D%3D&lid=6014e…
Exp. Name: Program to read today’s date and display tomorrow’s date using
S.No: 16 Date:
structures considering all cases.
Aim:
Page No:
Program to read today’s date and display tomorrow’s date using structures considering all cases.
Source Code:
ID: 1075
nextDate.c
#include <stdio.h>
struct date
{
int day;
int month;
int year;
};
void main()
{
struct date d;
int lastday[ ]={31,28,31,30,31,30,31,31,30,31,30,31};
printf("Enter today's date: ");
scanf("%d/%d/%d",&d.day,&d.month,&d.year);
if(d.day<lastday[d.month-1])
{
d.day++;
}
else if(d.month==12)
{
d.day=1;
d.month=1;
d.year++;
}
else if(d.day==28&&(d.year%4==0&&d.year%100!=0||d.year%400==0))
{
d.day++;
}
else
{
d.day=1;
d.month++;
}
G Pulla Reddy Engineering College (Autonomous)
Test Case - 1
User Output
Enter today's date: 02/02/2020
Next day is: 3/2/2020
Test Case - 2
https://gprec.codetantra.com/secure/labs-q.jsp?sNo=16&qId=6018c5aee8f350066cc6ba7b&bd=AY3RFZHVEQg%3D%3D&lid=6014e94fb65a870… 1/2
2/8/2021 https://gprec.codetantra.com/secure/labs-q.jsp?sNo=16&qId=6018c5aee8f350066cc6ba7b&bd=AY3RFZHVEQg%3D%3D&lid=6014e…
Test Case - 2
User Output
Enter today's date: 28/02/2016
Page No:
Next day is: 29/2/2016
ID: 1075
G Pulla Reddy Engineering College (Autonomous)
https://gprec.codetantra.com/secure/labs-q.jsp?sNo=16&qId=6018c5aee8f350066cc6ba7b&bd=AY3RFZHVEQg%3D%3D&lid=6014e94fb65a870… 2/2
2/8/2021 https://gprec.codetantra.com/secure/labs-q.jsp?sNo=17&qId=601e680a54a955066aa760bb&bd=AY3RFZHVEQg%3D%3D&lid=6014…
Aim:
Page No:
Program to perform addition, subtraction, multiplication and division of two complex numbers using structures
to functions
Source Code:
ID: 1075
complexNumbersOperations.c
#include <stdio.h>
int ch;
void menu()
{
printf("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n");
printf("Press 5 to exit\n");
printf("Enter your choice: ");
scanf("%d",&ch);
}
struct complex
{
float real;
float imag;
};
void main()
G Pulla Reddy Engineering College (Autonomous)
{
struct complex c1,c2;
printf("Enter real and imaginary parts of first complex number: ");
scanf("%f%f",&c1.real,&c1.imag);
printf("Enter real and imaginary parts of second complex number: ");
scanf("%f%f",&c2.real,&c2.imag);
printf("The first complex number is: ");
complex_display(c1.real,c1.imag);
printf("The second complex number is: ");
complex_display(c2.real,c2.imag);
menu();
while(ch<5)
{
switch(ch)
{
https://gprec.codetantra.com/secure/labs-q.jsp?sNo=17&qId=601e680a54a955066aa760bb&bd=AY3RFZHVEQg%3D%3D&lid=6014e94fb65a87… 1/3
2/8/2021 https://gprec.codetantra.com/secure/labs-q.jsp?sNo=17&qId=601e680a54a955066aa760bb&bd=AY3RFZHVEQg%3D%3D&lid=6014…
case 1:add(c1,c2);break;
case 2:subtract(c1,c2);break;
case 3:multiply(c1,c2);break;
case 4:divide(c1,c2);break;
}
Page No:
menu();
}
}
ID: 1075
void add(struct complex c1,struct complex c2)
{
printf("Addition result: ");
complex_display(c1.real+c2.real,c1.imag+c2.imag);
}
Test Case - 1
User Output
Enter real and imaginary parts of first complex number: 3 6
Enter real and imaginary parts of second complex number: 4 5
The first complex number is: 3.000000+6.000000i 1
The second complex number is: 4.000000+5.000000i 1
1. Addition 1
2. Subtraction 1
3. Multiplication 1
4. Division 1
Press 5 to exit 1
Enter your choice: 1
Addition result: 7.000000+11.000000i 2
https://gprec.codetantra.com/secure/labs-q.jsp?sNo=17&qId=601e680a54a955066aa760bb&bd=AY3RFZHVEQg%3D%3D&lid=6014e94fb65a87… 2/3
2/8/2021 https://gprec.codetantra.com/secure/labs-q.jsp?sNo=17&qId=601e680a54a955066aa760bb&bd=AY3RFZHVEQg%3D%3D&lid=6014…
Test Case - 1
1. Addition 2
2. Subtraction 2
3. Multiplication 2
Page No:
4. Division 2
Press 5 to exit 2
Enter your choice: 2
Subtraction result: -1.000000+1.000000i 3
ID: 1075
1. Addition 3
2. Subtraction 3
3. Multiplication 3
4. Division 3
Press 5 to exit 3
Enter your choice: 3
Multiplication result: -18.000000+39.000000i 4
1. Addition 4
2. Subtraction 4
3. Multiplication 4
4. Division 4
Press 5 to exit 4
Enter your choice: 4
Division result: 1.024390+0.219512i 5
1. Addition 5
2. Subtraction 5
3. Multiplication 5
4. Division 5
Press 5 to exit 5
Enter your choice: 5
https://gprec.codetantra.com/secure/labs-q.jsp?sNo=17&qId=601e680a54a955066aa760bb&bd=AY3RFZHVEQg%3D%3D&lid=6014e94fb65a87… 3/3
2/8/2021 https://gprec.codetantra.com/secure/labs-q.jsp?sNo=18&qId=6018f0f6e8f350066cc7a137&bd=AY3RFZHVEQg%3D%3D&lid=6014e9…
Exp. Name: Program to read details (rollno, name, dob, cgpa) of N students
S.No: 18 dynamically, store the data using structure (use nested structure for dob) and Date:
display all the details.
Page No:
Aim:
Program to read details (rollno, name, dob, cgpa) of N students dynamically, store the data using structure (use
nested structure for dob) and display all the details.
ID: 1075
Source Code:
studentDetails.c
#include <stdio.h>
#include <stdlib.h>
struct date
{
int day;
int month;
int year;
};
struct student
{
int rno;
char name[50];
struct date dob;
float cgpa;
};
void main()
{
int n,i;
struct student *p;
printf("Enter number of students: ");
scanf("%d",&n);
p=(struct student *)malloc(n*sizeof(struct student));
for(i=0;i<n;i++)
{
printf("Enter student %d data: \n",i+1);
printf("Enter roll number: ");
scanf("%d",&(p+i)->rno);
printf("Enter name: ");
scanf("%s",(p+i)->name);
G Pulla Reddy Engineering College (Autonomous)
https://gprec.codetantra.com/secure/labs-q.jsp?sNo=18&qId=6018f0f6e8f350066cc7a137&bd=AY3RFZHVEQg%3D%3D&lid=6014e94fb65a8706… 1/2
2/8/2021 https://gprec.codetantra.com/secure/labs-q.jsp?sNo=18&qId=6018f0f6e8f350066cc7a137&bd=AY3RFZHVEQg%3D%3D&lid=6014e9…
}
}
Page No:
Execution Results - All test cases have succeeded!
Test Case - 1
ID: 1075
User Output
Enter number of students: 2
Enter student 1 data: 12
Enter roll number: 12
Enter name: Ram
Enter date of birth: 11/11/1997
Enter cgpa: 9
Enter student 2 data: 13
Enter roll number: 13
Enter name: Raj
Enter date of birth: 10/9/1998
Enter cgpa: 8
Student 1 data:
Roll number: 12
Name: Ram
Date of birth: 11/11/1997
Cgpa: 9.00
Student 2 data:
Roll number: 13
Name: Raj
Date of birth: 10/9/1998
Cgpa: 8.00
https://gprec.codetantra.com/secure/labs-q.jsp?sNo=18&qId=6018f0f6e8f350066cc7a137&bd=AY3RFZHVEQg%3D%3D&lid=6014e94fb65a8706… 2/2
2/8/2021 https://gprec.codetantra.com/secure/labs-q.jsp?sNo=19&qId=6018e9c8e8f350066cc788f0&bd=AY3RFZHVEQg%3D%3D&lid=6014e…
Exp. Name: Program to read a text file, convert all the lowercase characters into
S.No: 19 Date:
uppercase and re-write the uppercase characters into another file.
Aim:
Page No:
Program to read a text file, convert all the lowercase characters into uppercase and re-write the uppercase
characters into another file. Also, count the number of lines in the file and display it. Follow the instructions
given below to write a program to copy the contents of one file to another file:
ID: 1075
Open an existing file " text1.txt " in read mode
Open an existing file " text2.txt " in read mode
Open a new file " output1.txt " in write mode
Replace all lowercase characters into upper casecharaters and copy the text in to the new file
output1.txt
Then open the new file output1.txt in read mode and display the content on the screen
Close the file
Source Code:
replace.c
#include <stdio.h>
#include <stdlib.h>
void main()
{
FILE *fp1,*fp2;
int ch,lc=1;
char c;
printf("Choose the file\n");
printf("1. text1.txt\n");
printf("2. text2.txt\n");
printf("Enter your choice: ");
scanf("%d",&ch);
if(ch!=1&&ch!=2)
{
printf("Invalid choice!!\n");
exit(0);
}
else if(ch==1)
{
fp1=fopen("text1.txt","r");
}
else
{
G Pulla Reddy Engineering College (Autonomous)
fp1=fopen("text2.txt","r");
}
fp2=fopen("output1.txt","w+");
while((ch=getc(fp1))!=EOF)
{
if(ch>=97&&ch<=122)
ch=ch-32;
putc(ch,fp2);
}
printf("Contents of new file: ");
rewind(fp2);
while((ch=getc(fp2))!=EOF)
{
putchar(ch);
https://gprec.codetantra.com/secure/labs-q.jsp?sNo=19&qId=6018e9c8e8f350066cc788f0&bd=AY3RFZHVEQg%3D%3D&lid=6014e94fb65a870… 1/2
2/8/2021 https://gprec.codetantra.com/secure/labs-q.jsp?sNo=19&qId=6018e9c8e8f350066cc788f0&bd=AY3RFZHVEQg%3D%3D&lid=6014e…
if(ch=='\n')
lc++;
}
printf("\nNumber of lines in the file are: %d",lc);
printf("\n");
Page No:
fclose(fp1);
fclose(fp2);
}
ID: 1075
text1.txt
text2.txt
Test Case - 1
User Output
Choose the file 1
1. text1.txt 1
2. text2.txt 1
Enter your choice: 1
Contents of new file: NATURE'S IS SO BEAUTIFUL WHEN THE SUN RISES ABOVE THE HORIZON, SO
COLORFUL AND SO CHEERING OUR MINDS TO MAKE US SO ENERGETIC.
THE MARVELOUS AND FANTASTIC RAINBOW WILL MAKE EVERY PERSON'S MIND
SO PLEASANT AND HEART SO LIGHT.
G Pulla Reddy Engineering College (Autonomous)
Test Case - 2
User Output
Choose the file 3
1. text1.txt 3
2. text2.txt 3
Enter your choice: 3
Invalid choice!!
https://gprec.codetantra.com/secure/labs-q.jsp?sNo=19&qId=6018e9c8e8f350066cc788f0&bd=AY3RFZHVEQg%3D%3D&lid=6014e94fb65a870… 2/2
2/8/2021 https://gprec.codetantra.com/secure/labs-q.jsp?sNo=20&qId=6018e73be8f350066cc77f66&bd=AY3RFZHVEQg%3D%3D&lid=6014e…
Exp. Name: Program to write a text that has commas into a file and read that text,
S.No: 20 Date:
replace commas with semi colons and write it into another file.
Aim:
Page No:
Program to write a text that has commas into a file and read that text, replace commas with semi colons and
write it into another file. Follow the instructions given below to write a program to copy the contents of one file to
another file:
ID: 1075
Open an existing file " text1.txt " in read mode
Open an existing file " text2.txt " in read mode
Open a new file " output1.txt " in write mode
Replace the commos present in the text file depending on the choice of the file with a semicolon and
copy the text in the new file output1.txt
Then open the new file output1.txt in read mode and display the content
Close the file
Source Code:
replace.c
#include <stdio.h>
#include <stdlib.h>
void main()
{
FILE *fp1,*fp2;
int ch;
char c;
printf("Choose the file\n");
printf("1. text1.txt\n");
printf("2. text2.txt\n");
printf("Enter your choice: ");
scanf("%d",&ch);
if(ch!=1&&ch!=2)
{
printf("Invalid choice!!\n");
exit(0);
}
else if(ch==1)
{
fp1=fopen("text1.txt","r");
}
else
{
G Pulla Reddy Engineering College (Autonomous)
fp1=fopen("text2.txt","r");
}
fp2=fopen("output1.txt","w+");
while(!feof(fp1))
{
ch=fgetc(fp1);
if(ch==',')
{
ch=';';
}
if(ch==EOF)
break;
putc(ch,fp2);
}
https://gprec.codetantra.com/secure/labs-q.jsp?sNo=20&qId=6018e73be8f350066cc77f66&bd=AY3RFZHVEQg%3D%3D&lid=6014e94fb65a870… 1/2
2/8/2021 https://gprec.codetantra.com/secure/labs-q.jsp?sNo=20&qId=6018e73be8f350066cc77f66&bd=AY3RFZHVEQg%3D%3D&lid=6014e…
printf("Contents of new file: ");
rewind(fp2);
while(!feof(fp2))
{
ch=fgetc(fp2);
Page No:
if(ch==EOF)
break;
putchar(ch);
}
ID: 1075
if(ch==EOF)
printf("\n");
fclose(fp1);
fclose(fp2);
}
text1.txt
The three items, a book, a pen, and paper, were on the table.
text2.txt
Test Case - 1
User Output
Choose the file 1
1. text1.txt 1
2. text2.txt 1
Enter your choice: 1
Contents of new file: The three items; a book; a pen; and paper; were on the table.
Test Case - 2
G Pulla Reddy Engineering College (Autonomous)
User Output
Choose the file 4
1. text1.txt 4
2. text2.txt 4
Enter your choice: 4
Invalid choice!!
https://gprec.codetantra.com/secure/labs-q.jsp?sNo=20&qId=6018e73be8f350066cc77f66&bd=AY3RFZHVEQg%3D%3D&lid=6014e94fb65a870… 2/2