PPS-LAB-MANUAL BY Asst - Prof Mani Kumar (B.Tech, M.Tech (Ph.D. Central University)
PPS-LAB-MANUAL BY Asst - Prof Mani Kumar (B.Tech, M.Tech (Ph.D. Central University)
[Note:The programs may be executed using any available Open Source/ Freely available IDE
Some of the Tools available are:
CodeLite: https://codelite.org/
Code::Blocks: http://www.codeblocks.org/
DevCpp : http://www.bloodshed.net/devcpp.html
Eclipse: http://www.eclipse.org
This list is not exhaustive and is NOT in any order of preference]
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.
b. Write a simple program that converts one given data type to another using auto
conversion and casting. Take the values form standard input.
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)).
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)
c. Write a program that finds if a given number 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.
e. A Fibonacci sequence is defined as follows: the first and second terms in the sequence
are 0 and 1. Subsequent terms are found by adding the preceding two terms in the
sequence. Write a C program to generate the first n terms of the sequence.
f. Write a C program to generate all the prime numbers between 1 and n, where n is a
value supplied by the user.
g. Write a C program to find the roots of a Quadratic equation.
h. Write a C program to calculate the following, where x is a fractional value.
i. 1-x/2 +x^2/4-x^3/6
j. 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.
Files:
a. Write a C program to display the contents of a file to standard output device.
b. Write a C program which copies one file to another, replacing all lowercase
characters with their uppercase equivalents.
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.
d. Write a C program that does the following:
It should first create a binary file and store 10 integers, where the file name and 10
values are given in the command line. (hint: convert the strings using atoi function)
Now the program asks for an index and a value from the user and the value at that
index should be changed to the new value in the file. (hint: use fseek function)
The program should then read all 10 values and print them back.
e. Write a C program to merge two files into a third file (i.e., the contents of the firs t
file followed by those of the second are put in the third file).
Strings:
a. Write a C program to convert a Roman numeral ranging from I to L to its decimal
equivalent.
b. Write a C program that converts a number ranging from 1 to 50 to Roman equivalent
c. Write a C program that uses functions to perform the following operations:
d. To insert a sub-string in to a given main string from a given position.
e. ii. To delete n Characters from a given position in a given string.
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.)
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.
h. Write a C program to count the lines, words and characters in a given text.
Miscellaneous:
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
1 * 1 1 *
12 ** 23 22 **
123 *** 456 333 ***
4444 **
*
Students will get knowledge about the concepts in C programming which includes
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.
Program:
#include<stdio.h>
Void main( )
{
int a,b;
printf(“enter the values of a and b”);
scanf(“%d%d”,&a,&b);
printf(“the arithmetic operators result is %d %d %d %d”, a+b,a-b,a*b,a/b);
printf(“the relational operators result is %d %d %d %d”, a>b,a<b,a>=b,a<=b);
printf(“the logical operators result is %d %d %d %d”, a&&b,a||b,!(a==b));
printf(“the increment operator result is %d %d %d %d”,a++,++a,b++,++b);
printf(“the decrement operator result is %d %d %d %d”,a--,--a,b--,--b);
printf(“the bitwise AND operator result is %d”,a&b);
printf(“the bitwise OR operator result is %d”,a|b);
printf(“the bitwise NOT operator result is %d”,a^b);
}
Output:
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
float sum,count;
int mean;
clrscr();
printf("enter the value of sum and count");
scanf("%f%f",&sum,&count);
mean=sum/count;
printf(" the value of mean is : %d\n", mean);
}
Output:
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter 3 numbers");
scanf("%d%d%d",&a,&b,&c);
if(a>b && a>c)
printf("Maximum number is a = %d",a);
else if(b>a && b>c)
printf("Maximum number is b = %d",b);
else
printf("Maximum number is c = %d",c);
printf("\n");
if(a<b && a<c)
printf("Minimum number is a = %d",a);
else if(b<a && b<c)
printf("Minimum number is b = %d",b);
else
printf("Minimum number is c = %d",c);
getch();
}
Output:
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int phy, chem, bio, math, comp;
float per;
clrscr();
printf("Enter five subjects marks: ");
scanf("%d%d%d%d%d", &phy, &chem, &bio, &math, &comp);
per = (phy + chem + bio + math + comp) / 5.0;
printf("Percentage = %.2f\n", per);
if(per >= 70)
{
printf("Distinction");
}
else if(per <=40 )
{
printf("Failed");
}
else if(per <= 60 && per>40)
{
printf("Second Class");
}
else if(per <= 70 && per>60)
{
printf("First Class");
}
getch();
}
Output:
Program:
#include <stdio.h>
#include<conio.h>
void main()
{
int n, i, range;
clrscr();
printf("Enter an integer: ");
scanf("%d",&n);
printf("Enter the range: ");
scanf("%d", &range);
for(i=1; i<=range;i++)
{
printf("%d * %d = %d \n",n,i, n*i);
}
getch();
}
Output:
Program:
#include <stdio.h>
#include<conio.h>
int binary_conversion(int);
void main()
{
int num, bin;
clrscr();
printf("Enter a decimal number: ");
scanf("%d", &num);
bin = binary_conversion(num);
printf("The binary equivalent of %d is %d\n", num, bin);
getch();
}
int binary_conversion(int num)
{
if (num == 0)
{
return 0;
}
else
{
return (num % 2) + 10 * binary_conversion(num / 2);
}
}
Output:
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)).
Program:
#include <stdio.h>
#include <conio.h>
void main()
{
int a, b, c;
char ch;
clrscr() ;
printf("Enter your operator(+, -, /, *, %)\n");
scanf("%c", &ch);
printf("Enter the values of a and b\n");
scanf("%d%d", &a, &b);
switch(ch)
{
case '+': c = a + b;
printf("addition of two numbers is %d", c);
break;
case '-': c = a - b;
printf("substraction of two numbers is %d", c);
break;
case '*': c = a * b;
printf("multiplication of two numbers is %d", c);
break;
case '/': c = a / b;
printf("remainder of two numbers is %d", c);
break;
case '%': c = a % b;
printf("quotient of two numbers is %d", c);
break;
default: printf("Invalid operator");
break;
}
getch();
}
Output:
Program:
#include<stdio.h>
int main()
{
int n,i,flag=0;
printf("\nEnter a number:”);
scanf("%d",&n);
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{flag=1; break;
}
}
If(flag==0)
printf(“%d is a prime number”,n);
else
printf(“%d is not a prime number”,n);
return(0);
}
Output:
Enter a number: 29
29 is a prime number
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main ()
{
int number = 0, digit = 0, sum = 0;
clrscr();
printf("Enter any number\n ");
scanf("%d", &number);
while (number != 0)
{
digit = number % 10;
sum = sum + digit;
number = number / 10;
}
printf ("Sum of individual digits of a given number is %d", sum);
getch();
}
Output:
palindrome.Program:
#include <stdio.h>
void main()
{
int number, t, rev=0, rmndr;
printf("Please enter a number to check Palindrome : ");
scanf("%d",&number);
printf("\nEntered number: %d", number);
t = number;
while (number > 0)
{
rmndr = number%10;
rev = rev*10 + rmndr;
number = number/10;
}
printf("\nReversed number: %d", rev);
if(t == rev)
{
printf("\nEntered number %d is a palindrome", t);
}
else
{
printf("\nEntered number %d is not a palindrome", t);
}
}
Output:
Program:
#include <stdio.h>
int main()
{
int i, n, t1 = 0, t2 = 1, nextTerm;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (i = 1; i <= n; ++i)
{
printf("%d, ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return 0;
}
Output
Program:
#include<stdio.h>
void main()
{
int m,n,i,nof;
printf("\nEnter a number:”);
scanf("%d",&n);
printf("\nThe Prime numbers between 1 to n:”);
for(m=2;m<=n;m++)
{
nof=0;
for(i=2;i<=m/2;i++)
{
if(m%i==0)
nof++;
}
if(nof==0)
printf("%d",m);
}
OUTPUT:
Enter the value of n: 40
The Prime numbers between 1 to n: 2 3 5 7 11 13 17
19 23 29 31 37
Program:
#include<stdio.h>
#include<math.h>
void main()
{
int a,b,c,disc;
float root1,root2;
clrscr();
printf("ENTER VALUES FOR a,b,c:\n");
scanf("%d %d %d",&a,&b,&c);
disc=b*b-4*a*c;
if(disc>0)
{
}
else if(disc==0)
{
printf("THE ROOTS ARE EQUAL & THEY ARE..\n");
root1=-b/(2*a);
root2=root1;
OUTPUT:
#include <stdio.h>
#define MAX_SIZE 100 // Maximum array size
int main()
{
int arr[MAX_SIZE];
int i, max, min, size;
printf("Enter size of the array: ");
scanf("%d", &size);
printf("Enter elements in the array: ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}
max = arr[0];
min = arr[0];
for(i=1; i<size; i++)
{
if(arr[i] > max)
{
max = arr[i];
}
if(arr[i] < min)
{
min = arr[i];
}
}
printf("Maximum element = %d\n", max);
printf("Minimum element = %d", min);
return 0;
}
int main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
clrscr();
return 0;
}
Program:
#include <stdio.h>
int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
printf("Enter number of rows and columns of first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter elements of first matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter number of rows and columns of second matrix\n");
scanf("%d%d", &p, &q);
if (n != p)
printf("The matrices can't be multiplied with each other.\n");
else
{
printf("Enter elements of second matrix\n");
for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
scanf("%d", &second[c][d]);
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++) {
Output:
Program:
#include <stdio.h>
int main()
{
int a[10][10], transpose[10][10], r, c, i, j;
printf("Enter rows and columns of matrix: ");
scanf("%d %d", &r, &c);
// Storing elements of the matrix
printf("\nEnter elements of matrix:\n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("Enter element a%d%d: ",i+1, j+1);
scanf("%d", &a[i][j]);
}
// Displaying the matrix a[][] */
printf("\nEntered Matrix: \n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("%d ", a[i][j]);
if (j == c-1)
printf("\n\n");
}
// Finding the transpose of matrix a
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
transpose[j][i] = a[i][j];
}
// Displaying the transpose of matrix a
printf("\nTranspose of Matrix:\n");
for(i=0; i<c; ++i)
for(j=0; j<r; ++j)
{
printf("%d ",transpose[i][j]);
Output:
Program:
#include<stdio.h>
int power(int n1,int n2);
int main()
{
int base,exp;
printf(“enter base number:”);
scanf(“%d”,&base);
printf(“enter power number(positive integer):”);
scanf(“%d”,&exp);
printf(“%d^%d=%d”,base,exp,power(base,exp));
return 0;
}
OUTPUT:
enter base number:3
enter power number(positive integer):3
3^3=27
Program:
#include <stdio.h>
#include <conio.h>
void main()
{
int n, a, b;
clrscr();
printf("Enter any number\n");
scanf("%d", &n);
a = recfactorial(n);
printf("The factorial of a given number using recursion is %d \n", a);
b = nonrecfactorial(n);
printf("The factorial of a given number using nonrecursion is %d ", b);
getch();
}
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;
for(i = 1;i <= x; i++)
{
f = f * i;
}
Output:
Enter any number
5
The factorial of a given number using recursion is 120
The factorial of a given number using nonrecursion is 120
Program:
#include <stdio.h>
#include <conio.h>
void main()
{
int a, b, c, d;
clrscr();
printf("Enter two numbers a, b\n");
scanf("%d%d", &a, &b);
c = recgcd(a, b);
printf("The gcd of two numbers using recursion is %d\n", c);
d = nonrecgcd(a, b);
printf("The gcd of two numbers using nonrecursion is %d", d);
getch();
}
int recgcd(int x, int y)
{
if(y == 0)
{
return(x);
}
else
{
return(recgcd(y, x % y));
}
}
int nonrecgcd(int x, int y)
{
int z;
while(x % y != 0)
{
z = x % y;
x = y;
y = z;
}
return(y);
}
Program:
#include <stdio.h>
int main()
{
int data[5], i;
printf("Enter elements: ");
for(i = 0; i < 5; ++i)
scanf("%d", data + i);
printf("You entered: \n");
for(i = 0; i < 5; ++i)
printf("%d\n", *(data + i));
return 0;
}
Output:
Enter elements:
1
2
3
5
4
You entered:
1
2
3
5
4
void main() {
int size, i, arr[MAX];
int *ptr;
clrscr();
ptr = &arr[0];
Output :
Enter the size of array : 5
Enter 5 integers into array : 11 22 33 44 55
Elements of array in reverse order are :
Element 4 is : 55
Element 4 is : 44
Element 4 is : 33
Element 4 is : 22
Element 4 is : 11
Program:
#include<stdio.h>
#include<conio.h>
void main() {
int numArray[10];
int i, sum = 0;
int *ptr;
Output:
Enter 10 elements: 11 12 13 14 15 16 17 18 19 20
The sum of array elements is 155
char filename[100], c;
// Open file
fptr = fopen(filename, "r");
if (fptr == NULL)
{
printf("Cannot open file \n");
exit(0);
}
{
char string[100];
int c, count[26] = {0};
printf("Input a string\n");
gets(string);
find_frequency(string, count);
printf("Character Count\n");
for (c = 0 ; c < 26 ; c++)
printf("%c \t %d\n", c + 'a', count[c]);
return 0;
}
void find_frequency(char s[], int count[]) {
int c = 0;
while (s[c] != '\0')
{
if (s[c] >= 'a' && s[c] <= 'z' )
count[s[c]-'a']++;
c++;
}
}
OUTPUT:
Input a String:
ABCDEF
A 1
B 1
C 1
D 1
E 1
F 1
PQRqrs
ABC t ABC PQR ABC
DEF STUuv DEF STU DEF
PQR
STU
OUTPUT:
enter main string for insertion:comer
enter sub string:put
Enter position:3
Main string after insertion: computer
OUTPUT:
Enter main string for deletion: abcdef
Enter no of characters to be deleted:2
Enter position:3
main string after deletion: abcf
int main(){
char string1[20];
int i, length;
int flag = 0;
printf("Enter a string:");
scanf("%s", string1);
length = strlen(string1);
if (flag) {
printf("%s is not a palindrome", string1);
}
else {
printf("%s is a palindrome", string1);
}
return 0;
}
Program:
#include <stdio.h>
int main()
{
int i, j, rows;
printf("Enter number of rows: ");
scanf("%d",&rows);
for(i=1; i<=rows; ++i)
{
for(j=1; j<=i; ++j)
{
printf("* ");
}
printf("\n");
}
return 0;
}
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, a[20], n, key, flag = 0;
clrscr();
printf(“Enter the size of an array \n”);
scanf(“%d”, &n);
printf(“Enter the array elements”);
for(i = 0; i < n; i++)
{
scanf(“%d”, &a[i]);
}
printf(“Enter the key elements”);
scanf(“%d”, &key);
for(i = 0; i < n; i++)
{
if(a[i] == key)
{
flag = 1;
break;
}
}
if(flag == 1)
printf(“The key elements is found at location %d”, i + 1);
else
printf(“The key element is not found in the array”);
getch();
}
Input & Output:
Enter the size of an array 6
Enter the array elements 50 10 5 200 20 1
Enter the key element 1
The key Element is found at location 6
57
Enter 4 names n
apple
boy
zeebra
cat
apple apple
boy boy
zeebra cat
cat zeebra
-