POP Lab Programs
POP Lab Programs
#include <stdio.h>
int main() {
char operator;
scanf("%c", &operator);
switch(operator) {
case '+':
break;
case '-':
break;
case '*':
break;
case '/':
else {
break;
default:
return 0;
Expected Output:
Output 1:
Result: 15.00
Output 2:
5
Result: 5.00
Output 3:
Result: 50.00
Output 4:
Result: 2.00
#include <stdio.h>
int main()
return 0;
Expected output:
#include <stdio.h>
int main()
{
int number1, number2, sum;
scanf("%d",&number1);
scanf("%d",&number2);
sum = number1+number2;
return 0;
Expected output:
64 + 36 = 100
#include <stdio.h>
#include <math.h>
int main()
double a, b, c;
discriminant = b * b - 4 * a * c;
if (discriminant > 0)
else if (discriminant == 0)
root1 = -b / (2 * a);
else
// Complex roots
return 0;
Expected Output:
Output 1:
Root 1 = -1.000000
Root 2 = -3.000000
Output 2:
Output 3:
1
1
#include<stdio.h>
int main()
int rad;
scanf("%d", &rad);
ci = 2 * PI * rad;
return 0;
Expected Output:
Circumference : 31.400002
#include <stdio.h>
int main()
{
// Input
scanf("%f", &principal);
scanf("%f", &rate);
scanf("%f", &time);
// Display Result
return 0;
Expected Output:
3. An electricity board charges the following rates for the use of electricity: for the first 200
units 80 paise per unit: for the next 100 units 90 paise per unit: beyond 300 units Rs 1 per
unit. All users are charged a minimum of Rs. 100 as meter charge. If the total amount is
more than Rs 400, then an additional surcharge of 15% of total amount is charged. Write a
program to read the name of the user, number of units consumed and print out the charges
#include <stdio.h>
int main() {
// Variables
char name[50];
int units;
// Input
scanf("%s", name);
scanf("%d", &units);
// Calculate charges
} else {
totalAmount += surcharge;
// Display charges
return 0;
Expected Output:
Output 1:
Units Consumed: 0
Output 2:
Enter the name of the user: Anuradha
Enter the number of units consumed: 167
Electricity Charges for Anuradha:
Units Consumed: 167
Meter Charge: Rs. 100.00
Total Charges: Rs. 233.60
Surcharge: Rs. 0.00
Output 3:
Enter the name of the user: Rajeev
Enter the number of units consumed: 598
Electricity Charges for Rajeev:
Units Consumed: 598
Meter Charge: Rs. 100.00
Total Charges: Rs. 745.20
Surcharge: Rs. 97.20
3a. Write a C Program To Check the Given Character is Lowercase or Uppercase or Special
Character.
#include <stdio.h>
int main()
char ch;
scanf("%c", &ch);
else
return 0;
Expected Output:
Output 1:
Enter a character: a
a is a lowercase letter.
Output 2:
Enter a character: A
A is an uppercase letter.
Output 3:
#include <stdio.h>
int main()
{
int number;
scanf("%d", &number);
if (number%2 == 0)
else
return 0;
Expected Output:
Output 1:
Enter an integer: 2
2 is an even integer.
Output 2:
Enter an integer: 7
7 is an odd integer.
4. Write a C Program to display the following by reading the number of rows as input,
1
121
12321
1234321
---------------------------
nth row
#include <stdio.h>
void main()
{
int i,j,n;
printf("Input number of rows : ");
scanf("%d",&n);
for(i=0; i<=n; i++)
{
for(j=1; j<=n-i; j++) //print blank spaces
printf(" ");
printf("\n");
}
}
Expected Output:
Input number of rows : 4
121
12321
1234321
4a. Write a C program to find largest of 3 numbers using ternary operator
#include <stdio.h>
int main()
int largest;
largest = (num1 > num2) ? ((num1 > num3) ? num1 : num3) : ((num2 > num3) ? num2 : num3);
return 0;
Expected Output:
#include <stdio.h>
int main()
char ch;
switch (ch)
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
break;
default:
break;
}
return 0;
Expected Output:
Output 1:
Enter a character: s
s is not a vowel.
Output 2:
Enter a character: e
e is a vowel.
#include <stdio.h>
int main()
int n, target;
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]);
scanf("%d", &target);
int left = 0;
int right = n - 1;
if (arr[mid] == target)
index = mid;
break;
else
if (index != -1)
else
{
return 0;
Expected Output:
Output1:
Output2:
#include <stdio.h>
if (n <= 1)
return n;
else
void displayFibonacciSeries(int n)
printf("\n");
int main()
int n;
scanf("%d", &n);
printf("Fibonacci Series: ");
displayFibonacciSeries(n);
return 0;
Expected Output:
Fibonacci Series: 0 1 1 2 3 5 8 13 21 34
#include<stdio.h>
#include<stdlib.h>
int main()
int c[10][10]={0};
printf("PROGRAMTOIMPLEMENTMATRIXMULTIPLICATION \n");
printf("\nEntertheOrderofMatrix1\n");
scanf("%d%d",&m, &n);
printf("\nEntertheOrderofMatrix2\n");
scanf("%d%d",&p,&q);
if(n!=p)
printf("EntertheelementsofMatrix1\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("EntertheelementsofMatrix2\n");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d", &b[i][j]);
for(i=0;i<m;i++)
for(j=0;j<q;j++)
for(k=0;k<n;k++)
c[i][j]+=a[i][k]*b[k][j];
printf("\nMatrix1\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
printf("\n");
printf("\nMatrix2\n");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
printf("%d\t",b[i][j]);
printf("\n");
printf("\n");
printf("\nTheproductoftheMatrixis\n");
for(i=0;i<m;i++)
for(j=0;j<q;j++)
printf("%d\t",c[i][j]);
printf("\n");
printf("\n");
return 0;
Expected Output:
Output 1:
PROGRAMTOIMPLEMENTMATRIXMULTIPLICATION
EntertheOrderofMatrix1
EntertheOrderofMatrix2
EntertheelementsofMatrix1
EntertheelementsofMatrix2
10
11
12
Matrix1
1 2
3 4
5 6
Matrix2
7 8 9
10 11 12
27 30 33
61 68 75
95 106 117
Output 2:
PROGRAMTOIMPLEMENTMATRIXMULTIPLICATION
EntertheOrderofMatrix1
EntertheOrderofMatrix2
H
HE
HEL
HELL
HELLO
HELL
HEL
HE
H.
#include<stdio.h>
#include<stdlib.h>
int main()
// variables
char str[20];
// take input
scanf("%[^\n]", str);
len--;
if(i<len) place = i;
printf("%c",str[j]); // print
return 0;
}
Expected Output:
he
hel
hell
hello
hell
hel
he
#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
printf("\n");
int main() {
int numRows;
scanf("%d", &numRows);
generatePascalsTriangle(numRows);
return 0;
Expected Output:
11
121
1331
14641
7 Compute sin(x)/cos(x) using Taylor series approximation. Compare your result with the
built-in library function. Print both the results with appropriate inferences.
#include<stdio.h>
#include<math.h>
#define PI 3.142857
int main()
float x,degree,nume,deno,sum,term;
int i;
printf("Enterdegree:");
scanf("%f",°ree);
x=degree*(PI/180.0);
sum=0;
nume=x;
deno=1.0;i=1;
do
{
term=nume/deno;
sum=sum+term;
i=i+2;
nume=-nume*x*x;
deno=deno*i*(i-1);
}while(fabs(term)>=0.00001);
printf("ComputedvalueofSin(%f)=%f\n",degree,sum);
return 0;
Expected Output:
Enterdegree:60
ComputedvalueofSin(60.000000)=0.866236
#include<stdio.h>
int main()
matTrans[j][i] = mat[i][j];
printf("\n");
return 0;
Expected Output:
7
8
1 4 7
2 5 8
3 6 9
#include <stdio.h>
int main() {
scanf("%d", &num1);
scanf("%d", &num2);
return 0;
}
// Function to multiply two numbers
Expected Output:
#include<stdio.h>
#include<stdlib.h>
int main()
int num,i,j,arr[10],temp;
for(i=0;i<num;i++)
scanf("%d", &arr[i]);
for(i=0;i<num;i++)
for(j=0;j<num-i-1;j++)
{
if(arr[j]>arr[j+1])
temp=arr[j];arr[j]=arr[j+1];arr[j+1]=temp;
return 0;
Expected Output:
56
14
03
89
55
Sorted array: 3 14 55 56 89
8a.Develop a C program that reads N integer numbers and arrange them in ascending order
using selection Sort.
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]);
// Selection Sort
int minIndex = i;
minIndex = j;
arr[minIndex] = temp;
printf("\n");
return 0;
Expected Output:
12
02
23
45
78
Sorted array: 2 12 23 45 78
8b.Develop a C program that reads N integer numbers and arrange them in ascending
order using Insertion Sort.
#include <stdio.h>
void insertionSort(int arr[], int n) {
int i, key, j;
key = arr[i];
j = i - 1;
arr[j + 1] = arr[j];
j = j - 1;
arr[j + 1] = key;
int i;
printf("\n");
int main() {
int arr[100], n, i;
scanf("%d", &n);
printArray(arr, n);
insertionSort(arr, n);
printArray(arr, n);
return 0;
Expected Output:
12
65
48
23
Original array:
12 5 65 48 23
Sorted array:
5 12 23 48 65
9 Write functions to implement string operations such as compare, concatenate, and find
string length. Use the parameter passing techniques.
#include <stdio.h>
#include <string.h>
// Function prototypes
int main() {
// Input strings
gets(string1);
gets(string2);
// Compare strings
if (resultCompare == 0) {
} else {
// Concatenate strings
// String length
return 0;
strcpy(result, str1);
strcat(result, str2);
return strlen(str);
Expected Output:
Output 1:
Length of string 1: 4
Length of string 2: 3
Output 2:
Length of string 1: 4
Length of string 2: 4
#include <stdio.h>
int main() {
// Input strings
gets(string1);
gets(string2);
// Concatenate strings
return 0;
}
int i = 0, j = 0;
result[i] = str1[i];
i++;
result[i + j] = str2[j];
j++;
result[i + j] = '\0';
Expected Output:
10 Implement structures to read, write and compute average- marks of the students, list the
students scoring above and below the average marks for a class of N students.
#include <stdio.h>
#define MAX_STUDENTS 50
struct Student {
char name[50];
int marks;
};
int main() {
int n;
scanf("%d", &n);
return 1;
scanf("%s", students[i].name);
scanf("%d", &students[i].marks);
printf("\nStudent Data:\n");
printf("%-20s %-10s\n", "Name", "Marks");
int totalMarks = 0;
totalMarks += students[i].marks;
printf("%s\n", students[i].name);
printf("%s\n", students[i].name);
}
return 0;
Expected Output:
Student Data:
Name Marks
sejal 60
ankita 70
komal 55
harsha 80
punam 95
punam
sejal
ankita
komal
10a. Write a C program to read a sentence and count the number of words in the sentence.
#include <stdio.h>
#include <string.h>
void main()
char str[200];
int y = 0, x;
scanf("%[^\n]str", str);
y++;
Expected Output:
11 Develop a program using pointers to compute the sum, mean and standard deviation of
all elements stored in an array of N real numbers.
#include<stdio.h>
#include<math.h>
void main ()
int i, n;
mean = sum1 / n;
var = sum2 / n;
dev = sqrt (var);
Expected Output:
Enter no of elements:05
11
45
89
56
Sum :224.000000
Mean :44.799999
Variance :739.359985
Deviation :27.191175
#include <stdio.h>
int main() {
int a, b, temp;
ptr1 = &a;
ptr2 = &b;
temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = temp;
return 0;
Expected Output:
24
12
11b. Write a program in C using functions to swap two numbers using global variables
concept and call by reference concept.
#include <stdio.h>
int main()
int a, b;
printf("\nBefore Swapping:\n");
swap(&a, &b);
printf("\nAfter Swapping:\n");
return 0;
int temp;
temp = *x;
*x = *y;
*y = temp;
Expected Output:
12
Before Swapping:
a = 24
b = 12
After Swapping:
a = 12
b = 24
12. Write a C program to copy a text file to another, read both the input file name and
target file name.
#include <stdio.h>
#include <stdlib.h>
int main() {
char buffer[BUFFER_SIZE];
size_t bytesRead;
scanf("%s", sourceFileName);
if (sourceFile == NULL) {
exit(EXIT_FAILURE);
scanf("%s", targetFileName);
if (targetFile == NULL) {
printf("Error opening target file.\n");
fclose(sourceFile);
exit(EXIT_FAILURE);
fclose(sourceFile);
fclose(targetFile);
return 0;
Expected Output: