PPS LAB PROGRAMS
PPS LAB PROGRAMS
PPS LAB PROGRAMS
Practice sessions
a. Write a simple program that prints the results of all the operators available in C (including pre/
post increment , bitwise and/or/not , etc.). Read required operand values from standard input.
Arithmetic Operators
#include <stdio.h>
int main()
{
int a,b,c;
printf(“enter a and b values”);
scanf(“%d %d”,&a,&b);
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c=a/b;
printf("a/b = %d \n",c); c=a
%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
}
Output:
enter a and b values 9 4
a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1
Assignment Operators
#include <stdio.h>
int main()
{
int a = 5, c;
c = a;
printf("c = %d \n", c);
c += a; // c = c+a
printf("c = %d \n", c);
c -= a; // c = c-a
printf("c = %d \n", c);
c *= a; // c = c*a
printf("c = %d \n", c);
c /= a; // c = c/a
printf("c = %d \n", c);
c %= a; // c = c%a
printf("c = %d \n", c);
return 0;
}
Output
c=5
c = 10
c=5
c = 25
c=5
c=0
Relational Operators
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10;
printf("%d == %d = %d \n", a, b, a == b); // true
printf("%d == %d = %d \n", a, c, a == c); //
false printf("%d > %d = %d \n", a, b, a > b);
//false
printf("%d > %d = %d \n", a, c, a > c); //false
printf("%d < %d = %d \n", a, b, a < b); //false
printf("%d < %d = %d \n", a, c, a < c); //true
printf("%d != %d = %d \n", a, b, a != b); //false
printf("%d != %d = %d \n", a, c, a != c); //true
printf("%d >= %d = %d \n", a, b, a >= b); //true
printf("%d >= %d = %d \n", a, c, a >= c); //false
printf("%d <= %d = %d \n", a, b, a <= b); //true
printf("%d <= %d = %d \n", a, c, a <= c); //true
return 0;
}
Output
5 == 5 = 1
5 == 10 = 0
5>5=0
5 > 10 = 0
5<5=0
5 < 10 = 1
5 != 5 = 0
5 != 10 = 1
5 >= 5 = 1
5 >= 10 = 0
5 <= 5 = 1
5 <= 10 = 1
Logical Operators
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;// 1 (true) 0(false)
result = (a == b) && (c > b);
printf("(a == b) && (c > b) equals to %d \n", result);
result = (a == b) && (c < b);
printf("(a == b) && (c < b) equals to %d \n", result);
result = (a == b) || (c < b);
printf("(a == b) || (c < b) equals to %d \n", result);
result = (a != b) || (c < b);
printf("(a != b) || (c < b) equals to %d \n", result);
result = !(a != b);
printf("!(a == b) equals to %d \n", result);
result = !(a == b);
printf("!(a == b) equals to %d \n", result);
return 0;
}
Output
(a == b) && (c > b) equals to 1
(a == b) && (c < b) equals to 0
(a == b) || (c < b) equals to 1
(a != b) || (c < b) equals to 0
!(a != b) equals to 1
!(a == b) equals to 0
Bitwise operator
#include <stdio.h>
int main(){
int a = 12, b = 25, num=212, i;
printf("AND = %d", a&b);
printf("OR = %d", a|b);
printf("XOR = %d", a^b);
printf("complement = %d\n",~35);
printf("complement = %d\n",~-12);
for (i=0; i<=2; ++i)
printf("Right shift by %d: %d\n", i, num>>i);
printf("\n");
for (i=0; i<=2; ++i)
printf("Left shift by %d: %d\n", i, num<<i);
return 0;
}
Output
AND = 8
OR = 29
XOR = 21
complement = -36
complement = 11
Right Shift by 0: 212
Right Shift by 1: 106
Right Shift by 2: 53
sizeof Operator
#include <stdio.h>
int main()
{
int a, e[10];
float b;
double c;
char d;
printf("Size of int=%lu bytes\n",sizeof(a));
printf("Size of float=%lu bytes\n",sizeof(b));
printf("Size of double=%lu bytes\n",sizeof(c));
printf("Size of char=%lu byte\n",sizeof(d));
printf("Size of integer type array having 10 elements = %lu bytes\n", sizeof(e));
return 0;
}
Output
Size of int = 4 bytes
Size of float = 4 bytes
Size of double = 8
bytes Size of char = 1
byte
Size of integer type array having 10 elements = 40 bytes
int main(){
int year;
printf("Enter a year to check if it is a leap year or not \n");
scanf("%d", &year);
}
Output
Enter a year to check if it is a leap year or not
2014
not a leap year
Output:
x = 107, z = 108.000000
#include<stdio.h>
int main()
{
double x;
printf(“enter x value:”);
scanf(“%lf”,&x);
// Explicit conversion from double to int
int sum = (int)x + 1;
printf("sum = %d", sum);
return 0;
}
Output:
enter x value: 1.2
sum = 2
2. Simple numeric problems
a. Write a program for find the max and min from the three numbers.
#include<stdio.h>
void main(){
int a,b,c;
printf("Enter 3 numbers");
scanf("%d%d%d",&a,&b,&c);
else
else
Output
Enter 3 numbers 1
Maximum number is c =3
Minimum number is a =1
b. Write the program for the simple, compound interest.
#include<stdio.h>
#include<math.h>
int main()
float p,q,r,SI,CI;
int n;
scanf("%f",&p);
scanf("%f",&r);
scanf("%d",&n);
SI = ((p*r*n)/100);
q = 1+(r/100);
CI=p*pow(q,n)-p;
return 0;
Output
#include <stdio.h>
int main(void)
int num;
scanf("%d",&num);
else if ( num >=60 && num<=70){ // Note the space between else & if
return 0;
Output
5x1=5
5 x 2 = 10
5 x 3 = 15
#include <stdio.h>
int main(){
int n, i, row;
scanf("%d",&n);
printf("Enter no of rows:
"); scanf("%d",&row);
return 0;
Output
Enter an integer: 9
Enter no of rows:5
9*1=9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
e. Write a program that shows the binary equivalent of a given positive number between 0 to 255.
#include<stdio.h>
int main()
int bn[50];
scanf("%d",&n);
for(i=0;n>0;i++) //
bn[i]=n%2;
n=n/2;
printf("binary number
is"); for(i=i-1;i>=0;i--)
printf("%d",bn[i]);
Output
enter n 12
1100
3. Expression Evaluation
a. A building has 10 floors with a floor height of 3 meters each. A ball is dropped from the top of
the building. Find the time taken by the ball to reach each floor. (Use the formula s = ut+(1/2)at^2
where u and a are the initial velocity in m/sec (= 0) and acceleration in m/sec^2 (= 9.8 m/s^2)).
#include<stdio.h>
#include<math.h>
main()
int s=30,u=0,r,t;
float a=9.8;
r=2*s/a;
t=sqrt(r);
Output
time taken is 2
b. Write a C program, which takes two integer operands and one operator from the user, performs
the operation and then prints the result. (Consider the operators +,-,*, /, % and use Switch
Statement)
#include <stdio.h>
void main()
int a, b, c;
char ch;
scanf("%c", &ch);
case '+': c = a + b;
break;
case '-': c = a - b;
break;
case '*': c = a * b;
break;
case '/': c = a / b;
break;
case '%': c = a % b;
break;
break;
Output:
#include <stdio.h>
int main()
int n, i, flag = 0;
scanf("%d",&n);
if(n%i==0)
flag++;
if (flag==2)
else
return 0;
Output:
3 is a prime number.
d. Write a C program to find the sum of individual digits of a positive integer and test given
number is palindrome.
#include<stdio.h>
#include<math.h>
void main ()
scanf("%d", &number);
original_number=number;
while (number != 0)
reverse_integer=reverse_integer*10 + digit;
if (original_number == reverse_integer)
else
Output:
#include <stdio.h>
int main()
int i, n, t1 = 0, t2 = 1, nextTerm;
scanf("%d", &n);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
return 0;
Output:
#include<stdio.h>
void main()
int n, i, j, count;
scanf("%d", &n);
count = 0;
if(i % j == 0)
count++;
if(count == 2)
printf("%d\t", i);
} }
Output:
2 3 5 7 11 13 17 19
2 3 5 7
g. Write a C program to find the roots of a Quadratic equation.
#include<stdio.h>
#include<math.h>
int main(){
float a,b,c;
float d,root1,root2;
scanf("%f%f%f",&a,&b,&c);
d = b * b - 4 * a * c;
printf("%.3f%+.3fi",-b/(2*a),sqrt(-d)/(2*a));
printf(", %.3f%+.3fi",-b/(2*a),-sqrt(-d)/(2*a));
return 0;
else if(d==0){
return 0;
else{
return 0;
Output:
#include<stdio.h>
#include<math.h>
main()
float x,s=0,i;
scanf(“%f”,&x); for(i=1;i<=3;i+
+)
s+=pow(-x,i)/(2*i);
s=1-s; printf(“result=
%f”,s);
Output:
result=-0.3
i. Write a C program to read in two numbers, x and n, and then compute the sum of this geometric
progression: 1+x+x^2+x^3+………….+x^n. For example: if n is 3 and x is 5, then the program
computes 1+5+25+125.
#include <stdio.h>
#include <math.h>
void main()
int n, x, i, sum = 0;
scanf("%d", &n);
scanf("%d", &x);
printf("illegal value");
else
printf("sum=%d", sum);
Output:
sum=7
4. Arrays and Pointers and Functions:
a. Write a C program to find the minimum, maximum and average in an array of integers.
#include <stdio.h>
int main()
int arr[MAX_SIZE];
scanf("%d", &size);
scanf("%d", &arr[i]);
max = arr[0];
min = arr[0];
{
max = arr[i];
min = arr[i];
sum+=arr[i];
avg=sum/size;
return 0;
Output
21349
Maximum element = 9
Minimum element = 1
Average = 3
b. Write a functions to compute mean, variance, Standard Deviation, sorting of n elements in single
dimension array.
#include <stdio.h>
#include <math.h>
#define MAXSIZE 10
void main()
float x[MAXSIZE];
int i, n;
scanf("%d", &n);
scanf("%f", &x[i]);
cal(x,n);
std_deviation = sqrt(variance);
std_deviation);
Output:
1234
#include <stdio.h>
int main()
{ scanf("%d", &a[i][j]);
{
for (j = 0; j < columns; j++)
{ scanf("%d", &b[i][j]);
/* matrix addtion */
matrixAddition(a, b, sum);
printf("%d", sum[i][j]);
printf("\t");
printf("\n");
return 0;
int i, j;
for (i = 0; i < rows; i++)
return;
}Output:
11
11
11
11
22
22
ii. Multiplication of Two Matrices
#include<stdio.h>
int main()
scanf("%d", &first[c][d]);
if (n != p)
else{
scanf("%d", &second[c][d]);
mulmatrix(first,second,m,n,p,q);
}
int c,d,k,sum=0,multiply[10][10];
multiply[c][d] = sum;
sum = 0;
Output:
11
11
11
11
22
22
iii. Transpose of a matrix with memory dynamically allocated for the new matrix as row and
column counts may not be same.
#include <stdio.h>
int main()
++i)
scanf("%d", &a[i][j]);
{
printf("%d ", a[i][j]);
if (j == c-1) printf("\n\
n");
transpose[j][i] = a[i][j];
printf("\nTranspose of Matrix:\n");
printf("%d ",transpose[i][j]);
if(j==r-1)
printf("\n\n");
return 0;
Entered Matrix:
12
34
Transpose of Matrix:
13
24
#include <stdio.h>
int recfactorial(int
x);
void main()
int n, a, b;
scanf("%d", &n);
a = recfactorial(n);
b = nonrecfactorial(n);
int recfactorial(int x)
{ int f;
if(x == 0){
return(1);
else{
f = x * recfactorial(x - 1);
return(f);
int nonrecfactorial(int x)
{ int i, f = 1;
f = f * i;
return(f);
Output
ii. To find the GCD (greatest common divisor) of two given integers.
#include <stdio.h>
void main()
int a, b, c, d;
printf("Enter two numbers a and b");
c = recgcd(a, b);
d = nonrecgcd(a, b);
{ if(y == 0){
return(x);
else{
return(recgcd(y, x % y));
{ int z;
while(x % y != 0){
z = x % y;
x = y;
y = z;
return(y);
Output
#include <stdio.h>
int main()
int result = 1;
scanf("%d", &base);
scanf("%d", &exponent);
result=nonrec(base,exponent);
printf("Answer = %lld\n",
result);
{ if (a != 0)
else
return 1;
{
int result=1;
result = result * b;
Output
Enter an exponent: 5
Answer = 3125
e. Write a program for reading elements using pointer into array and display the values using
array.
#include <stdio.h>
int main(){
pointer int i;
for(i=0;i<10;i++){
printf("%d\t",arr[i]);
}
return 0;
Output:
1234551233
1234551233
f. Write a program for display values reverse order from array using pointer.
#include<stdio.h>
#define MAX 30
void main() {
int size, i, arr[MAX];
int *ptr;
ptr = &arr[0];
printf("\nEnter the size of array : ");
scanf("%d", &size);
printf("\nEnter %d integers into array: ", size);
for (i = 0; i < size; i++) {
scanf("%d", ptr); ptr+
+;
}
ptr = &arr[size - 1];
Element1 is: 2
Element0 is: 1
#include<stdio.h>
#include<conio.h>
void main() {
int
numArray[10];
int i, sum = 0;
int *ptr;
printf("\nEnter 10 elements :
scanf("%d", &numArray[i]);
sum = sum +
*ptr; ptr++;
Output:
Enter 10 elements : 11 12 13 14 15 16 17 18 19 20
#include <stdio.h>
#include <stdlib.h>
int main()
FILE *fptr;
char filename[100], c;
scanf("%s", filename);
if (fptr == NULL){
exit(0);
c = fgetc(fptr);
fclose(fptr);
return 0;
}
Output:
/*Contents of a.txt*/
b. Write a C program which copies one file to another, replacing all lowercase characters with their
uppercase equivalents.
#include<stdio.h>
#include<stdlib.h>
int main()
char ch;
if (fp1 == NULL)
exit(1);
if (fp2 == NULL)
fclose(fp1);
exit(1);
while((ch=fgetc(fp1))!=EOF)
{
ch = toupper(ch);
fputc(ch,fp2);
return 0;
Output
c. Write a C program to count the number of times a character occurs in a text file. The file name
and the character are supplied as command line arguments.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int count=0;
char c;
FILE *fp1;
if (fp1 == NULL)
exit(0);
scanf("%c",&c);
count++;
printf("%d",count);
fclose(fp1);
return 0;
e. Write a C program to merge two files into a third file (i.e., the contents of the first file followed by
those of the second are put in the third file).
#include <stdio.h>
#include <stdlib.h>
int main()
{
// Open two files to be merged
FILE *fp1 = fopen("file1.txt", "r");
FILE *fp2 = fopen("file2.txt", "r");
// Open file to store the result
FILE *fp3 = fopen("file3.txt", "w");
char c;
fclose(fp1);
fclose(fp2);
fclose(fp3);
return 0;
}
Output:
a. Write a C program to convert a Roman numeral ranging from I to L to its decimal equivalent.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
char rom[30];
scanf("%s", &rom);
len =strlen(rom);
+)
switch (rom[i])
break;
break;
break;
break;
case 'C': a[i] = 100;
break;
break;
break;
break;
k = a[len - 1];
k = k - a[i – 1];
k = k + a[i – 1];
Output
decimal equivalent is 17
#include <stdio.h>
int main () {
int num;
scanf("%d", &num);
/* M - 1000 */
printf("M");
/*
* D is 500. CM is 900
*/
printf("CM");
num = num -
900;
} else {
printf("D");
}
} else if (num >= 100) {
/* C is 100. CD is 400
*/
printf("CD");
} else {
printf("C");
/* L is 50. XC is 90
* XC = C - X = 100 - 10 => 90
*/
printf("XC");
} else {
printf("L");
/* XL is 40. IX is 9. X is 10
* XL = L - X = 50 - 10 = 40
* IX = X - I = 10 - 1 = 9
*/
printf("XL");
} else if (num == 9)
{ printf("IX");
num = num - 9;
} else {
printf("X");
/* V is 5 and IV is 4
* IV = V - I = 5 - 1 => 4
*/
if (num >= 5) {
printf("V");
num = num - 5;
} else {
printf("IV");
num = num - 4;
} else {
printf("I");
num = num - 1;
}
}
printf("\n");
Output
#include<stdio.h>
#include<string.h>
void main()
gets(str1);
l1 = strlen(str1);
gets(str2);
l2 = strlen(str2);
scanf("%d", &n);
}
for(i = 0; i < l2; i++)
str1[n + i] = str2[i];
str2[l2 + 1] = '\0';
Output:
sachin
tendulkar
#include<stdio.h>
#include<string.h>
void main()
char str[20];
int i, n, l, pos;
gets(str);
scanf("%d", &pos);
printf("Enter the number of characters to be deleted\n");
scanf("%d", &n);
l = strlen(str);
str[i - n] = str[i];
str[i - n] = '\0';
Output:
sachin
f. Write a C program to determine if the given string is a palindrome or not (Spelled same in both
directions with or without a meaning like madam, civic, noon, abcba, etc.)
#include <stdio.h>
#include <string.h>
void main()
char str[20];
int i, l, f = 0;
l = strlen(str);
f = f + 1;
if(f == l)
else
Output:
madam
g. Write a C program that displays the position of a character ch in the string S or – 1 if S doesn‘t
contain ch.
#include<stdio.h>
#include<string.h>
void main()
gets(s);
gets(t);
if(found)
else
printf("-1");
Output:
hyderabad
de
#include <stdio.h>
#include <string.h>
void main()
char str[100];
int i = 0, l = 0, f = 1;
gets(str);
l = l + 1;
f = f + 1;
Output:
Enter any string
a. Write a menu driven C program that allows a user to enter n numbers and then choose between
finding the smallest, largest, sum, or average. The menu and all the choices are to be functions. Use
a switch statement to determine what action to take. Display an error message if an invalid choice is
entered.
#include <stdio.h>
#include <stdlib.h>
int option;
int menu();
int a[20],n,i,small1=0,sum1=0,large1=0,avg1=0;
int main(void)
printf("enter no of elements");
scanf("%d",&n);
printf("enter elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
menu();
scanf("%d",&option);
switch(option)
{
case 1: small(n,a);
break;
case 2: large(n,a);
break;
case 3: sum(n,a);
break;
case 4: avg(n,a);
break;
default:
printf("invalid");
return 0;
int menu()
printf ("\n***********************");
t* 1. SMALLEST *");
printf("\n\t**********************");
{
small1=a[0];
for(i=1;i<n;i++)
if(small1>a[i])
small1=a[i];
printf("%d",small1);
large1=a[0];
for(i=1;i<n;i++)
if(large1<a[i])
large1=a[i];
printf("%d",large1);
for(i=0;i<n;i++)
{
sum1=sum1+a[i];
printf("%d",sum1);
for(i=0;i<n;i++)
sum1=sum1+a[i];
avg1=sum1/n; printf("\n
%d",avg1);
Output
enter no of elements
enter elements 2 3 4 5 6 4
***********************
* MENU *
* *
* 1. SMALLEST *
* 2. LARGEST *
* 3. SUM *
* 4. AVERAGE *
**********************
a) 1
12
123
#include <stdio.h>
int main()
int i, j;
for(i=1; i<=3;++i)
for(j=1; j<=i;++j)
printf("%d ",j);
printf("\n");
return 0;
Output
12
123
b) *
**
***
#include <stdio.h>
int main()
int i, j;
for(i=1; i<=3;++i)
for(j=1; j<=i;++j)
printf("*");
printf("\n");
return 0;
Output
**
***
c) Floyd's Triangle
23
456
#include <stdio.h>
int main()
{
int i, j, number= 1;
++number;
printf("\n");
return 0;
Output
23
456
d)
22
333
4444
#include <stdio.h>
int main()
int i, j,num=1;
for(j=1;j<=i;++j)
printf("%d", num);
++num;
printf("\n");
return 0;
Output
22
333
4444
e)
**
***
**
#include<stdio.h>
int main()
int i, j, N, columns;
columns=1; printf("\
n"); for(i=1;i<N*2;i+
+){
printf("*");
+;
else{
columns--;
printf("\n");
return 0;
Output
**
***
****
***
**
*
a. Write a C program that uses non recursive function to search for a Key value in a given list of
integers using linear search method.
#include<stdio.h>
void main()
scanf("%d", &n);
scanf("%d", &a[i]);
scanf("%d", &key);
if(a[i] == key)
flag = 1;
break;
if(flag == 1)
printf("The key elements is found at location %d", i + 1);
else
Output
23456
b. Write a C program that uses non recursive function to search for a Key value in a given sorted
list of integers using binary search method.
#include<stdio.h>
void main()
printf("enter n value");
scanf("%d",&n);
scanf("%d", &a[i]);
scanf("%d", &key);
low = 0;
high = n - 1;
if(key == a[mid])
break;
else
if(key >
a[mid]) low =
mid + 1; else
high = mid - 1;
if(key == a[mid])
else
Output
enter n
value 5
23456
4
The key element is found at location 3
c. Write a C program that implements the Bubble sort method to sort a given list of integers in
ascending order.
#include<stdio.h>
void main()
scanf("%d", &n);
scanf("%d", &a[i]);
temp = a[j];
a[j + 1] =
temp;
}
printf("The sorted array is\n");
n", a[i]);
Output
23756
d. Write a C program that sorts the given array of integers using selection sort in descending order
#include<stdio.h>
void main()
scanf("%d", &n);
scanf("%d", &a[i]);
}
for(i = 0; i < n - 1; i++)
min = i;
min = j;
temp = a[i];
a[i] = a[min];
a[min] = temp;
n", a[i]);
Output
23756
2
e. Write a C program that sorts the given array of integers using insertion sort in ascending order
#include <stdio.h>
int main()
int n, array[1000], i, j, t;
scanf("%d", &n);
scanf("%d", &array[i]);
j = i;
while ( j > 0)
{ t = array[j];
array[j] = array[j-1];
array[j-1] = t;
j--;
return 0;
Output
Enter 5 integers
2 3 7 10 6
10
#include<stdio.h>
#include<string.h>
int main(){
int i,j,count;
char str[10][10],temp[10];
printf("No of strings to be
entered:"); scanf("%d",&count);
for(i=0;i<count;i++)
scanf("%s",&str[i]);
for(i=0;i<count;i++)
for(j=i+1;j<count;j++)
{ if(strcmp(str[i],str[j])>0)
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
for(i=0;i<count;i++)
puts(str[i]);
return 0;
Output
Sri
Devi
College
Engineering
Technology
College
Devi
Engineering
Sri
Technology