PPS Lab Manual for C Programming
PPS Lab Manual for C Programming
(COLLEGE OF ENGINEERING)
LAB MANUAL
[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]
SYLLABUS:
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.
Simple numeric problems:
a. Write a program for fiend the max and min from the three numbers.
b. Write the program for the simple, compound interest.
c. Write program that declares Class awarded for a given percentage of marks, where mark
<40%= Failed, 40% to <60% = Second class, 60% to <70%=First class, >=70% =
Distinction. Read percentage from standard input.
d. Write a program that prints a multiplication table for a given number and the number of
rows in the table. For example, for a number 5 and rows = 3, the output should be:
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.
1 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. 9
2 Write a simple program that converts one given data type to another using auto
conversion and casting. Take the values form standard input 10
3 Write a program for find the max and min from the three numbers. 11
4 Write the program for the simple, compound interest 12
5 Write program that declares Class awarded for a given percentage of marks, where
mark <40%= Failed, 40% to <60% = Second class, 60% to <70%=First class, >=
70% = Distinction. Read percentage from standard input. 12
6 Write a program that prints a multiplication table for a given number and the
number of rows in the table. 13
7 Write a program that shows the binary equivalent of a given positive number
between 0 to 255 14
8 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. 14
9 Write a C program, which takes two integer operands and one operator from the
user, performs the operation and then prints the result. 15
10 Write a program that finds if a given number is a prime number 16
11 Write a C program to find the sum of individual digits of a positive integer and test
given number is palindrome 17
12 Write a C program to generate the first n terms of the Fibonacci sequence. 18
13 Write a C program to generate all the prime numbers between 1 and n, where n is a
value supplied by the user 18
14 Write a C program to find the roots of a Quadratic equation 19
15 Write a C program to calculate the following, where x is a fractional value. 1-x/2
+x^2/4-x^3/6 19
16 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. 20
17 Write a C program to find the minimum, maximum and average in an array of
integers. 20
18 Write a functions to compute mean, variance, Standard Deviation, sorting of n
elements in single dimension array. 22
19 Write a C program that uses functions to perform the following:
i) Addition of Two Matrices
ii) Multiplication of Two Matrices
iii)Transpose of a matrix 24
Write C programs that use both recursive and non-recursive functions
20 i. To find the factorial of a given integer.
ii. To find the GCD (greatest common divisor) of two given integers.
iii. To find x^n 28
1) 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.
Output:
Enter a,b values 5 3
Sum is 8
Subtraction is 2
Multiplication is 15
Division is 1
Mod is 2
2) Write a simple program that converts one given data type to another using auto
conversion and casting. Take the values form standard input
Source Code:
#include<stdio.h>
void main()
{
char c;
int a;
float b;
printf("Enter a character");
scanf("%c",&c);
printf("Enter an integer value");
scanf("%d",&a);
printf("Enter a float value");
Output:
Enter a characterB
Enter an integer value3
Enter a float value5
#include<stdio.h>
void main()
{
int a,b,c;
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>c)
printf("Maximum number is b = %d",b);
else
printf("Maximum number is c = %d",c);
if(a<b && a<c)
printf("Minimum number is a = %d",a);
else if(b<c)
printf("Minimum number is b = %d",b);
else
printf("Minimum number is c = %d",c);
getch();
}
Output:
Enter the value of Principal p=1000
Enter the value of rate r=12
Enter the value of period in year n=2
Simple interest SI=240.000000
Compound Interest CI= 254.400009
5) Write program that declares Class awarded for a given percentage of marks, where
mark <40%= Failed, 40% to <60% = Second class, 60% to <70%=First class, >=
70% = Distinction. Read percentage from standard input.
Source code:
#include<stdio.h
> void main()
{
int per;
printf("Enter
percentage:");
scanf("%d",&per);
if(per<40)
Output:
Enter percentage:63
First CLass
6) Write a program that prints a multiplication table for a given number and the
number of rows in the table. For example, for a number 5 and rows = 3, the output
should be:
5x1=5
5 x 2 = 10
5 x 3 = 15
Source code:
#include<stdio.h
> void main()
{
int n,x,i;
printf("Enter table number
x:"); scanf("%d",&x);
printf("Enter number of rows
n:"); scanf("%d",&n);
for(i=1;i<=n;i++)
printf("%d*%d=%d\n",x,i,x*i);
getch();
}
Output:
Enter table number x:5
Enter number of rows n:6
5*1=5
5*2=10
5*3=15
5*4=20
5*5=25
5*6=30
#include <stdio.h>
int binary_conversion(int);
int main()
{
int num, bin;
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:
Enter a decimal number:15
The binary equivalent of 15 is 1111
8) 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)).
Source code:
#include<stdio.h>
#include<math.h>
int time(float t);
int s=3;
float
a=9.8;
main()
{
float t;
t=sqrt((2*s)/a);
printf("time to reach for every floor
%f\n",t); time(t);
getch();
int time(float t)
{
float t1;
s=s+3;
if(s<=30)
{
t1=sqrt((2*s)/a);
printf("time to reach for every floor %f\n",t1-t);
time(t1);
}
}
Output
time to reach for every floor 0.782461
time to reach for every floor 0.324106
time to reach for every floor 0.248695
time to reach for every floor 0.209660
time to reach for every floor 0.184714
time to reach for every floor 0.166994
time to reach for every floor 0.153567
time to reach for every floor 0.142937
time to reach for every floor 0.134249
time to reach for every floor 0.126976
Output:
Enter number n 37
37 is a prime number
11) Write a C program to find the sum of individual digits of a positive integer and
test given number is palindrome
Source Code:
#include
<stdio.h> void
main()
{
int num, temp, k, reverse = 0,s=0;
Output:
Enter an integer 424
Given number is = 424
Its reverse is = 424
Sum of individual digits is 10
Number is a palindrome
12) 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.
Source Code:
#include<stdio.h>
void main()
{
int n1=0,n2=1,n,i,f;
printf("enter fibonacci sries length n: ");
scanf("%d",&n);
printf("\n Fibonacci Series is: ");
printf("%d %d",n1,n2);
for(i=1;i<=(n-2);i++)
{
f=n1+n2;
printf(" %d",f);
n1=n2;
n2=f;
}
getch();
}
Output:
enter fibonacci sries length n: 6
Fibonacci Series is:0 1 1 2 3 5
13) Write a C program to generate all the prime numbers between 1 and n, where n
is a value supplied by the user.
Source Code:
#include<stdio.h>
void main()
{
int i,j,f,n;
printf("Enter the number n: ");
scanf("%d",&n);
printf("Prime Numbers from 1 to %d are: ",n);
for(i=1;i<=n;i++)
{
f=0;
for(j=1;j<=i;j++)
if(i%j==0)
f++;
if(f==2)
Output:
Enter x,n values:2 3
-0.333333
16) 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.
Source Code:
/*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 i,x,n;
int sum=0;
printf("Enter x,n values:");
scanf("%d%d",&x,&n);
for(i=0;i<=n;i++)
{
sum=sum+pow(x,i);
}
printf("%d",sum);
}
Output:
Enter x,n values:6 2
43
Arrays and Pointers and Functions:
17) Write a C program to find the minimum, maximum and average in an array of
integers.
Source Code:
#include <stdio.h>
void main()
{
int arr[100];
int i, max, min, size,sum;
{
sum = sum + arr[i];
}
}
printf("The numbers arranged in sorting order are:
\n"); for (i = 0; i < size; ++i)
I) Addition of Two
Matrices Source Code
#include<stdio.h>
int mtxadd(int a[5][5],int b[5][5],int m,int
n); int i,j;
void main()
{
int a[5][5],b[5][5],p,q,m,n;
printf("Enter 1st Matrix order");
scanf("%d%d",&m,&n);
printf("Enter 2nd Matrix order");
scanf("%d%d",&p,&q);
if(m==p&&n==q)
{
printf("Enter 1st Matrix Elements: ");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nEnter 2nd Matrix Elements: ");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
printf(“Matrix Addition is:\n”);
mtxadd(a,b,m,n);
}
24
Output:
Enter 1st Matrix order2 4
Enter 2nd Matrix order4 3
Enter 1st Matrix Elements:
1357
2468
Output:
Enter the number of rows and columns of matrix 3 2
Enter elements of the matrix
45
67
89
Transpose of the matrix:
4 6 8
5 7 9
20) Write C programs that use both recursive and non-recursive functions
i) To find the factorial of a given integer.
PPS LAB MANUAL
ii. To find the GCD (greatest common divisor) of two given integers.
iii. To find x^n
Source Code:
i)/*C program recursive and non-recursive functions to find the factorial of a
given integer.*/
#include<stdio.h>
void main()
{
int n, a, b;
printf("Enter any number\n");
scanf("%d", &n);
a = recfact(n);
printf("The factorial of a given number using recursion is %d \n",
a); b = nonrecfact(n);
printf("The factorial of a given number using nonrecursionis %d ", b);
getch();
}
int recfact(int x)
{
int f;
if(x == 0)
{
return(1);
}
else
{
f = x * recfact(x - 1);
return(f);
}
}
int nonrecfact(int x)
{
int i, f = 1;
for(i = 1;i <= x; i++)
{
f = f * i;
}
return(f);
}
Output: Enter any number 5
The factorial of a given number using recursion is 120
The factorial of a given number using nonrecursionis 120
iv) X^n
Source code:
#include <stdio.h>
int power(int n1, int n2);
int nonrec(int b,int p);
int main()
{
int base, q, p, r;
}
int nonrec(int b,int p)
{
int i,m=1;
if(p!=0)
{
for(i=1;i<=p;i++)
m=m*b;
return m;
}
else
return 1;
}
int power(int base, int p)
{
if (p != 0)
return (base*power(base, p-1));
else
return 1;
}
Output
Enter base number: 5
Enter power number(positive integer): 3
Non Recursive 5^3 = 125
Recursive 5^3 = 125
21) Write a program for reading elements using pointer into array and display the
values using array
Source Code:
#include
<stdio.h> void
main()
{
int arr[5], i;
printf("Enter elements: ");
}
Output:
Enter elements: 6
12
44
421
PPS LAB MANUAL
25
You entered:
6
12
44
421
25
22) Write a program for display values reverse order from array using pointer
Source Code:
#include
<stdio.h> void
main()
{
int arr[5], i;
printf("Enter elements:
"); for(i = 0; i < 5; ++i)
scanf("%d", &arr[i]);
printf("You entered:
\n"); for(i = 4; i>=0; --i)
printf("%d\n", *(arr+i));
}
Output:
Enter elements: 5
26
12
15
23
You entered:
23
15
12
26
5
Output:
Enter elements: 45
12
22
33
14
You entered:
126
FILES:
25) Write a C program which copies one file to another, replacing all lowercase
characters with their uppercase equivalents
Source Code:
#include<stdio.h
> void main()
{
FILE *fp1,*fp2; char ch;
fp1=fopen("fact.c","r");
fp2=fopen("fac.c","w");
Output:
VOID MAIN()
{
INT A;
}
26) 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.
Source Code
#include<stdio.h>
void main(int argc,char *argv[])
{
FILE *fp;
char ch, c=*argv[1];
int i=0;
fp=fopen(argv[2],"r");
while((ch=getc(fp))!=EOF)
{
if(ch==c)
i++;
printf("%c",ch);
}
printf("\n %c repeats %d times",c,i);
}
int n1;
};
void main(int argc,char *argv[])
{
int i,j,k,n,f=0;
struct twonum num;
FILE *fp;
fp= fopen(argv[1],"wb");
if (fp == NULL)
{
printf("Error in opening file");
}
else
for(i = 2; i <argc; ++i)
{
num.n1=atoi(argv[i]);
fwrite(&num, sizeof(num), 1, fp);
}
rewind(fp);
printf("Enter index for changing the value: ");
scanf("%d",&k);
printf("\nEnter new value for change: ");
scanf("%d",&n);
fseek(fp,sizeof(num)*(k-1),SEEK_SET);
num.n1=n;
fwrite(&num, sizeof(num), 1, fp);
fclose(fp);
fp=fopen(argv[1],"rb");
printf(“\nUpdated binary file content is:”);
for(i = 2; i <argc; ++i)
{
fread(&num, sizeof(num), 1, fp);
printf("%d\t",num.n1);
}
fclose(fp);
getch();
}
36
Output:
#include<stdio.h>
void main()
{
int a;
}
#include<stdio.h>
void main()
{
float c;
}
Output:
Enter the roman number XIV
Decimal equivalent is 14
char romanval[1000];
int i = 0;
int main()
{
int j;
long number;
{
postdigit('C', number / 100);
number = number - (number / 100) * 100;
}
else
{
predigit('L', 'D');
number = number - (500 - 100);
}
}
else if (number >= 50 )
{
if (number < (50 + 4 * 10))
{
postdigit('L', number / 50);
number = number - (number / 50) * 50;
}
else
{
predigit('X','C');
number = number - (100-10);
}
}
else if (number >= 10)
{
if (number < (10 + 3 * 10))
{
postdigit('X', number / 10);
number = number - (number / 10) * 10;
}
else
{
predigit('X','L');
number = number - (50 - 10);
}
}
else if (number >= 5)
{
if (number < (5 + 4 * 1))
{
postdigit('V', number / 5);
number = number - (number / 5) * 5;
}
else
{
predigit('I', 'X');
Source code:
/*To insert a sub-string in to a given main string from a given position. */
#include<stdio.h>
#include<conio.h>
#include<string.h>
void insert(char str1[],char str2[],int l1,int l2,int n);
void main()
{
char str1[20], str2[20];
int l1, l2, n, i;
puts("Enter the string 1\n");
gets(str1);
l1 = strlen(str1);
puts("Enter the string 2\n");
gets(str2);
l2 = strlen(str2);
printf("Enter the position where the string is to be inserted\n");
scanf("%d", &n);
insert(str1,str2,l1,l2,n);
getch();
}
void insert(char str1[],char str2[],int l1,int l2,int n)
{
int i;
for(i = n; i < l1; i++)
{
str1[i + l2] = str1[i];
}
for(i = 0; i < l2; i++)
{
str1[n + i] = str2[i];
}
str2[l2 + 1] = '\0';
printf("After inserting the string is %s", str1);
}
Output:
Enter the string 1
sachin
Enter the string 2
tendulkar
Enter the position where the string is to be inserted 4
After inserting the string is sachtendulkarin
PPS LAB MANUAL
Source code:
/*To delete n Characters from a given position in a given string.*/
#include<stdio.h>
#include<string.h>
void delete(char str[],int n,int l,int pos);
void main()
{
char str[20];
int i, n, l, pos;
puts("Enter the string\n");
gets(str);
printf("Enter the position where the characters are to be deleted\n");
scanf("%d", &pos);
printf("Enter the number of characters to be deleted\n");
scanf("%d", &n);
l = strlen(str);
delete(str,n,l,pos);
}
void delete(char str[],int n,int l,int pos)
{
int i;
for(i = pos + n; i < l; i++)
{
str[i - n] = str[i];
}
str[i - n] = '\0';
printf("The string is %s", str);
}
Output
Enter the string
saradi
Enter the position where characters are to be deleted 2
Enter the number of characters to be deleted 2
The string is sadi
43
printf("%s is in Palindrome\n",s1);
else
printf("%s is not in Palindrome\n",s1);
}
Output:
enter String s1: DAD
DAD is in Palindrome
33) Write a C program that displays the position of a character ch in the string S or –
1 if S doesn‘t contain ch
Source Code:
#include<stdio.h>
#include<string.h>
main()
{
char s[10],t[10];
char *found;
printf("Enter a String:");
gets(s);
printf("Enter t String:");
gets(t);
found=strstr(s,t);
if(found)
printf("String is found at : %d position",found-s);
else
printf("-1");
getch();
}
Output:
Enter a String:country
44
Enter t String:try
String is found at : 4 position
PPS LAB MANUAL
34) Write a C program to count the lines, words and characters in a given
text. Source Code:
#include<stdio.h
> void main()
{
char line[80],ch;
int i,j,end=0,c=0,w=0,l=0;
printf("enter a line of text, press enter at the end\n");
while(end==0)
{
j=0;
while((ch=getchar())!='\n')
line[j++]=ch;
line[j]='\0';
if(line[0]=='\0')
break;
else
{
w++;
for(i=0;line[i]!='\0';i++)
{
if(line[i]==' '||line[i]=='\t')
w++;
else
c++;
}
l=l+1;
}
}
printf("\n");
printf("number of lines=%d\n",l);
printf("number of words=%d\n",w);
printf("number of characters=%d\n",c);
}
Output:
enter a line of text, press enter at the end
hi hello
where are you
i am going to college
Miscellaneous:
35) 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.
Source Code:
#include<stdio.h
>
int small(int a[], int n);
int large(int a[], int n);
int sum(int a[], int n);
int avg(int a[], int n);
void main()
{
int i,ch,n,a[20]; printf("Enter n
value:"); scanf("%d",&n);
printf("Enter array
Elements"); for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n MENU \n1.Finding Smallest Element\n 2.Finding Largest Element\n");
printf("\n3. Finding Sum of all Elements\n 4.Finding Average of all Elements\n");
printf("Enter Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: small(a,n);
break;
case 2: large(a,n);
break;
case 3: sum(a,n);
break;
case 4: avg(a,n);
break;
default:printf("Invalid choice");
}
getch();
}
int small(int a[], int n)
{
int i,s;
s=a[0];
i) Source Code:
#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("%d ",j);
}
printf("\n");
}
return 0;
}
Output:
Enter number of rows:
51
12
123
1234
12345
int i, j, rows;
Output:
Enter number of rows: 4
*
**
***
****
Output:
Enter number of rows: 4
1
23
456
7 8 9 10
v) Source Code:
#include<stdio.h>
void main()
{
int n, c, k;
printf("Enter number of rows\n");
scanf("%d",&n);
for ( c = 1 ; c <= n ; c++)
{
for ( k = 1 ; k <= c ; k++ )
printf("*");
printf("\n");
}
for ( c = n - 2 ; c >= 0 ; c-- )
{
for ( k = c ; k >= 0 ; k-- )
printf("*");
printf("\n");
}
getch();
}
Output:
Enter number of rows 3
*
**
***
**
*
PPS LAB MANUAL
Sorting and Searching:
37) 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
Source Code:
#include<stdio.h
>
void search(int a[],int key,int n);
void main()
{
int i, a[20], n, key;
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);
search(a,key,n);
}
void search(int a[],int key,int n)
{
int flag = 0,i;
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");
Output:
Enter array size:6
Enter the array elements in ascending order23
25
29
54
65
74
Enter the key element
PPS LAB MANUAL
65
The key element is found at location 5
39) Write a C program that implements the Bubble sort method to sort a given list
of integers in ascending order
Source Code:
#include<stdio.h>
void main()
{
int n, a[20], temp, i, j;
printf("Enter the size of the array\n");
scanf("%d", &n);
printf("Enter the array elements\n");
for(i = 0; i < n; i++)
scanf("%d", &a[i]);
for(i = 0; i < n - 1; i++)
{
for(j = 0; j < n - 1; j++)
{
if(a[j] > a[j + 1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
printf("The sorted array is\n");
for(i = 0; i < n; i++)
printf("%d\n", a[i]);
getch();
}
Output:
Enter the size of the array
6
Enter the array elements
23
14
25
68
2
45
The sorted array is
2
14
23
25
45
68
#include<stdio.h>
void main()
{
int a[20],n, s, i, j, temp;
printf("enter number of elements:");
scanf("%d",&n);
printf("\n Enter %d elements:",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
s=i;
for(j=i+1;j<n;j++)
{
if(a[j]>a[s])
s=j;
}
temp=a[s];
a[s]=a[i];
a[i]=temp;
}
printf(“\n Array elements after selection sort:”);
for(i=0;i<n;i++)
printf("%d ",a[i]);
getch();
}
Output:
enter number of elements:5
Enter 5 elements: 45 32 89 12 8
Array elements after selection sort:
89 45 32 12 8
41) Write a C program that sorts the given array of integers using insertion sort in
ascending order
Source Code:
#include<stdio.h>
int main()
{
Output:
Enter Number of Strings:5
Enter 5 Strings:modi
india
kranthi
akash
nithin
Strings after sorting
akash
India
kranthi
modi
nithin