C Programming Lab Manual (2018-19) V0.2
C Programming Lab Manual (2018-19) V0.2
Ramya S
Shashank S
Tojo Mathew
1
Table of Contents
Course Outcomes & CO Mapping:......................................................................................................3
Module 1. Programming Basics...........................................................................................................4
1.1 Hello World in C........................................................................................................................4
1.2 Size of all data types..................................................................................................................4
1.3 Simple and Compound Interest..................................................................................................5
1.4 Celsius to Fahrenheit Converter................................................................................................5
1.5 Area of Circle.............................................................................................................................6
1.6 Area of triangle..........................................................................................................................7
1.7 Sum of two numbers..................................................................................................................8
Module 2. Conditional Statements.......................................................................................................9
2.1 Roots of a quadratic equation....................................................................................................9
2.2 Simple Calculator....................................................................................................................10
Module 3. Looping Constructs I.........................................................................................................12
3.1 Prime........................................................................................................................................12
3.2 Prime Series.............................................................................................................................13
3.3 Star Pattern...............................................................................................................................14
3.4 Multiplication Table.................................................................................................................15
Module 4. Looping Constructs II.......................................................................................................17
4.1 GCD and LCM.........................................................................................................................17
4.2 Palindrome...............................................................................................................................18
4.3 Factorial...................................................................................................................................19
4.4 Fibonacci Numbers..................................................................................................................20
Module 5. Functions and Pointers......................................................................................................21
5.1 Swapping two numbers............................................................................................................21
5.2 Sum of array elements.............................................................................................................22
Module 6. One-D Array......................................................................................................................23
6.1 Bubble Sort..............................................................................................................................23
6.2 Binary Search...........................................................................................................................25
Module 7. Two-D Array.....................................................................................................................27
Matrix Addition..............................................................................................................................27
Module 8. Arrays & Functions...........................................................................................................30
8.1 Linear Search...........................................................................................................................31
8.2 Sorting in Descending Order...................................................................................................33
8.3 Mean, Variance & Standard Deviation....................................................................................35
8.4 Matrix Multiplication...............................................................................................................37
Module 9. Strings...............................................................................................................................40
9.1 ASTROLOGY(Without Library functions).............................................................................40
9.2 ASTROLOGY(Using library functions)..................................................................................41
Module 10. Structures........................................................................................................................43
10.1 Employee Details(Structure)..................................................................................................43
Module 11. File Handling...................................................................................................................45
11.1 File IO....................................................................................................................................45
2
Course Outcomes & CO Mapping:
On Successful completion of this course, the students will be able to
3
Module 1. Programming Basics
1.1 Hello World in C
Write a C program to print "Hello World!"
Sample Output:
Hello World!
Solution:
#include<stdio.h>
int main()
{
printf("Hello World!");
return 0;
}
Solution:
#include<stdio.h>
int main()
{
printf("Size of int is %d\n",sizeof(int));
printf("Size of float is %d\n",sizeof(float));
printf("Size of double is %d\n",sizeof(double));
printf("Size of char is %d\n",sizeof(char));
4
return 0;
}
Input format:
The first line of the input consists of float value which corresponds to the principal value.
The second line of the input consists of float value which corresponds to the rate value.
The third line of the input consists of integer value which corresponds to the period in year.
Output format:
The first line of the output consists of float value which corresponds to the simple interest.
The second line of the output consists of float value which corresponds to the compound interest.
Print the output value with two decimal places as given in the sample input and output.
Solution:
#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);
printf("Simple interest is %.2f\n",SI);
q = 1+(r/100);
CI=p*pow(q,n)-p;
printf("Compound interest is %.2f\n",CI);
return 0;
}
5
The relatioship between Celcius (C) and Fahrenheit (F) degrees for measuring temperature is linear.
Find an equation relating C and F if 0 C corresponds to 32 F and 100 C corresponds to 212 F.
Write a C program to simulate Celcius to Fahrenheit Converter.
Input Format:
Input consists of a single integer which corresponds to a measure of temperature in Celcius.
Output Format:
Refer Sample Input and Output for exact formatting specifications.
[All floating point values are displayed correct to 1 decimal place]
Solution:
#include <stdio.h>
int main()
{
float celsius, F;
scanf("%f", &celsius);
F = ((celsius * 9)/5) + 32;
printf("%.1fF",F);
return 0;
}
Input Format:
Input consists of float value which corressponds to radius of circle.
Output Format:
Output consist of area of circle in mtsq.
Sample Input:
5.5
Sample Output:
94.98
Solution:
#include<stdio.h>
int main()
6
{
float r,area;
scanf("%f",&r);
area = 3.14*r*r;
printf("%.2f",area);
return 0;
}
Input Format:
Input consists of three integer variables corresponding to three sides of the triangle.
Output Format:
The output consists of the area of the triangle.
Refer sample input and output for formatting specifications.
[All text in bold corresponds to the input and the rest corresponds to output]
Sample Input:
5
6
7
Sample Output:
14.70
Solution:
#include<stdio.h>
#include<math.h>
int main()
{
int a,b,c;
float s,area;
scanf("%d %d %d",&a,&b,&c);
s = (a+b+c)/2.0;
7
area = sqrt(s*(s-a)*(s-b)*(s-c));
printf("%.2f",area);
return 0;
Input Format:
Input consists of 2 integers.
Output Format:
The output consists of an Integer indicating the sum of inputs.
Sample Input:
4
7
Sample output:
11
Solution:
#include<stdio.h>
int main()
{
int a,b;
scanf("%d%d",&a,&b);
printf("Sum: %d",a+b);
return 0;
}
8
Module 2. Conditional Statements
2.1 Roots of a quadratic equation
Write a C program to find and output all the roots of a given quadratic equation, for non-zero
coefficients. (Using if…else statement)
Input Format:
Input consists of 3 integers corresponding to the coefficients of the Quadratic equation ax^2 + bx +
c
Output Format:
The output consists of the roots of the quadratic equation.
111
372
9
printf("Enter the coefficients of a, b and c :\n");
scanf("%f%f%f", &a, &b, &c);
d = b*b-4*a*c;
if(a == 0){
printf("invalid");
}
else if(d == 0){
x1 = x2 = -b/(2*a);
printf("%.2f", x1);
}
else if(d>0){
x1 = (-b+sqrt(d))/(2*a);
x2 = (-b-sqrt(d))/(2*a);
printf("%.2f and %.2f", x1, x2);
}
else if(d < 0){
r = -b/(2*a);
img = (sqrt(-d))/(2*a);
printf("%.2f+%.2fi and ",r,img);
printf("%.2f-%.2fi",r,img);
}
return 0;
}
Write a C program to simulate a simple calculator that performs arithmetic operations like addition,
subtraction, multiplication, and division only on integers. Error message should be reported, if any
attempt is made to divide by zero. (Using switch statement)
Input Format:
The first input consists of an integer which corresponds to the first operand, second input consists of
an operator(+,-,*,/,%) and the third input consists of an integer which corresponds to the second
operand.
Output Format:
Refer Sample Output
Sample Input 1:
10
+
5
Sample Output 1:
The sum is 15
Sample Input 2:
10
10
-
5
Sample Output 2:
The difference is 5
Sample Input 3:
10
*
5
Sample Output 3:
The product is 50
Sample Input 4:
10
/
5
Sample Output 4:
The quotient is 2
Sample Input 5:
10
/
0
Sample Output 5:
Divide by Zero error!!!
Sample Input 6:
10
%
5
Sample Output 6:
The remainder is 0
Sample Input 7:
10
=
5
Sample Output 7:
Invalid Input
Solution:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a=0,b=0;
11
char op;
scanf("%d %c %d",&a,&op,&b); // Input format: 3 + 5 (Space required)
switch(op)
{
case '+' : printf("\nThe sum is %d",a+b);
break;
case '-' : printf("\nThe difference is %d",a-b);
break;
case '*' : printf("\nThe product is %d",a*b);
break;
case '/' : if(b==0)
{
printf("\nDivide by Zero error!!!");
exit(0);
}
else
{
printf("\nThe quotient is %d",a/b);
break;
}
case '%' : printf("\nThe remainder is %d",a%b);
break;
default:printf("\nInvalid Input");
}
return 0;
}
12
33
Not prime
Solution:
#include<stdio.h>
#include<math.h>
int main()
{
int n, c = 2, flag = 0;
scanf("%d",&n);
if ( flag == 0 )
printf("Prime\n");
else
printf("Not prime\n");
return 0;
}
Sample Input:
5
Sample Output:
2 3 5 7 11
13
Solution:
int main(){
int count=0,i,flag,n,num;
scanf("%d",&n);
num=2; // First prime number
while(count<n)
{
flag = 0;
for(i=2;i<=num/2;i++)
{
if(num%i==0)
{
flag=1;
break; // exit inner loop
}
}
if(flag==0)
{
printf("%d ",num);
count++;
}
num++;
}
return 0;
}
[All text in bold corresponds to input and the rest corresponds to output]
14
Sample Input and Output 2:
3
*
**
***
Solution:
#include <stdio.h>
int main()
{
int i, j, rows;
scanf("%d",&rows);
i=1;
while(i<=rows)
{
for(j=1; j<=i; ++j)
{
printf("*");
}
printf("\n");
i++;
}
return 0;
}
Input Format:
Input consists of an integer (0<N<20).
Output Format :
The output consists of tables from 1 to N as shown in sample input-output format.
Refer sample input and output for formatting specifications.
[All text in bold corresponds to the input and the rest corresponds to output]
Sample Input :
5
Sample Output:
12345
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
15
6 12 18 24 30
7 14 21 28 35
8 16 24 32 40
9 18 27 36 45
10 20 30 40 50
Solution:
#include <stdio.h>
int main()
{
int i , j;
int n,num;
scanf("%d",&n);
for(i=1; i<=10; i++)
{
num= i;
for(j=1; j<=n; j++)
{
printf("%d ",(num*j));
}
printf("\n");
}
return 0;
}
16
Module 4. Looping Constructs II
4.1 GCD and LCM
Write a C program to find the GCD and LCM of two integers and output the results along with the given integers.
Use Euclid’s algorithm. (Using looping constructs)
Input Format:
The first and the second input are two integers.
Output Format:
Output consists of GCD and LCM of the 2 input numbers.
Refer sample input and output for formatting specifications.
[All text in bold corresponds to input and the rest corresponds to output]
Solution:
#include <stdio.h>
int main() {
int a, b, x, y, rem=0, gcd, lcm;
printf("Enter two numbers\n");
scanf("%d%d", &x, &y);
a = x;
b = y;
/*
If b > a,after first iteration a & b
exchanges values after first iteration.
*/
while (b != 0) {
rem = a % b;
a=b;
b = rem;
}
gcd = a;
lcm = (x*y)/gcd;
17
4.2 Palindrome
Write a C program to reverse a given integer number and check whether it is a palindrome or not. Output the
given number with suitable message. (Using While statement)
Input Format:
The first input consists of an integer.
Output Format:
Output consists of a statement in which we print whether the input is palindrome or not.
Refer sample input and output for formatting specifications.
[All text in bold corresponds to input and the rest corresponds to output]
Solution:
#include <stdio.h>
int main()
{
long int num, n, reverse = 0;
while (n != 0)
{
reverse = reverse * 10; // Shifting the digit left by one poistion.
reverse = reverse + n%10; // extracting & appending right most digit of n to reverse.
n = n/10; // Discardng the right most digit of n.
}
if(num == reverse)
{
printf("%ld is a palindrome.",num);
}
else
{
printf("%ld is not a palindrome.",num);
}
return 0;
18
}
4.3 Factorial
Write a program to find the factorial of a given number(Using While loop).
Input Format:
Input consists of a single integer, n.
Output Format:
The output consists of a single integer which corresponds to n!
Sample Input :
4
Sample Output :
24
120
Solution:
#include<stdio.h>
int main()
{
int n, temp;
scanf("%d",&n);
temp = 1;
while(temp<=n)
{
fact = fact*temp;
temp++;
}
printf("%ld",fact);
return 0;
}
19
4.4 Fibonacci Numbers
Write a C program to generate and print first ‘N’ Fibonacci numbers. (Using do-While statement)
Input Format:
The first input consists of an integer.
Output Format:
Output consists of series of n fibinacci numbers.
Refer sample input and output for formatting specifications.
[All text in bold corresponds to input and the rest corresponds to output]
Solution:
/* Fibonacci Series C language */
#include<stdio.h>
int main()
{
int n, prev = 0, current = 1, next, count;
count = 0;
do
{
if ( count <= 1 )
{
printf("%d ",count); // For 0 & 1
}
else
{
next = prev + current; // Next value
printf("%d ",next);
prev = current; // Update previous value
current = next; // Update current value
}
count++;
}
while(count<n);
20
return 0;
}
Mrs. Anitha , our favourite Maths teacher wanted to teach her students to swap
two elements.
Write a program to accept 2 integers and to swap them using functions and
pointers.
Function Specification:
void swap(int *a,int *b)
This functions swaps 2 integers.
Input Format:
The input consists of 2 integer.
Output Format:
Refer to the sample input and output for formatting details.
[All text in bold corresponds to input and the rest corresponds to
output.]
Function Definitions:
voidswap(int *, int *)
Solution:
21
#include<stdio.h>
int main()
{
int a,b;
printf("Before swapping\n");
printf("a = %d b = %d\n",a,b);
swap(&a,&b);
printf("After swapping\n");
printf("a = %d b = %d\n",a,b);
return 0;
}
Function Specification:
int sum(int *arr,int n)
This functionadds all the elements in an arra.. And returns the added Sum.
Input Format:
Input consists of n+1 integers. The frst integer corresponds to ‘n’,the size of
the arra.. The next ‘n’ integers correspond to the elements in the arra..
Assume that the maximum value of n is 15.
Output Format:
Refer sampleoutput for better understanding.
Sample Input 1:
5
22
2
3
6
8
1
Sample Output 1:
The sum of the elements in the arra. is 20
Solution:
#include<stdio.h>
int sum(int *arr,int n);
int main()
{
int i,n,s;
int a[20];
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
s = sum(a,n);
for(i=0;i<n;i++)
sum += arr[i];
return sum;
Input Format:
The frst input consists of an integer which corresponds to the number of elements present
in the single dimension arra..
23
The next n inputs are the elements in the arra..
Output Format:
First line output consists of arra. elements before sorting and the next line of the
outputconsists of arra. elements after sorting.
Solution:
#include<stdio.h>
int main()
{
int array[100],n,i,j,temp;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(array[j]>array[j+1])
24
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
return 0;
}
Input Format:
The frst input consists of an integer which corresponds to the number of elements present
in the single dimension arra..
The next n inputs are the elements in the arra..
The next inputconsists of an integer which corresponds to the ke. element to be searched.
Output Format:
Output should displa. whether the ke. is present in the arra. or not.
25
Enter the number of elements :
5
Enter the elements :
6
2
3
7
1
Enter the ke. to be searched :
2
The ke. is not present in the arra.
Solution:
#include<stdio.h>
int main()
{
int a[20],n,i,key,low,high,mid;
printf("Enter the number of elements\n");
scanf("%d",&n);
26
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(a[mid]==key)
{
printf("The key is present in the array\n");
printf("At index %d\n",mid);
goto label1;
}
if(a[mid]<key)
low=mid+1;
else
high=mid-1;
}
printf("The key is not present in the array\n");
label1: return 0;
}
The frst input consists of an integer which corresponds to the number of elements present
in the two dimension arra..
The next n inputs are the elements of the frst matrix and then followed b.elements of the
second matrix.
Output Format:
The output consist of the frst matrix and then the second matrix and then the resultant
matrix which corresponds to the addition of two matrix.
Print "Matrix addition is not possible" if the size(order) of two matrixis not same.
27
8
9
6
3
Enter the size of Second matrix :
2
2
Second matrix :
1
4
5
6
First input matrix :
89
63
Second input matrix :
14
56
Resultant matrix :
9 13
11 9
28
Sample Input and Output 2 :
Enter the size of First matrix :
2
2
First matrix :
8
9
6
3
Enter the size of Second matrix :
2
2
1
Second matrix :
4
5
6
First input matrix :
89
63
Second input matrix :
14
56
Resultant matrix :
9 13
11 9
Solution:
29
if(m!=p||n!=q)
{
printf("Matrix addition not possible");
return 0;
}
return 0;
}
30
8.1 Linear Search
Write C program for user-defned functions:
1. To input N integer numbers into a single dimension arra..
2. To conduct a linear search.
Using these functions, write a C program to accept the N integer numbers & given ke.
integer number and conduct a linear search. Report success or failure in the form of a
suitable message.
Function Specification:
void search(int a[ ],int b,int n)
This function will search whether the element 'b' is present in anarray "a" of size n. Andprint "The ke.
is present in the arra.."if foundelseprint"The ke. is not present in the arra..".
Input Format:
The first input consists of an integer which corresponds to the number of elements present in the single
dimension array.
The next n inputs are the elements in the array.
The next input consistsof an integer which corresponds to the ke. element to be searched.
Output Format:
The output should displa. whetherthe ke. is present in the arra. or not.
31
Additional Sample TestCases
Sample Input and Output 1 :
Enter the number of elements :
7
Enter the elements :
3
6
2
4
12
25
64
Enter the key to be searched :
25
The key is present in the array.
At index 5
Sample Input and Output 2 :
Enter the number of elements :
5
Enter the elements :
2
4
6
8
10
Enter the key to be searched :
9
The key is not present in the array.
Solution:
#include<stdio.h>
#include<stdlib.h>
int i,n;
void linear(int a[10],int key);
void input(int a[10]);
int main()
{
int a[10],key;
printf("Enter the number of elements :\n");
scanf("%d",&n);
input(a);
printf("Enter the key to be searched :\n");
scanf("%d",&key);
linear(a,key);
return 0;
}
void input(int a[10])
32
{
printf("Enter the elements :\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
}
Input Format:
The frst input consists of an integer which corresponds to the number of elements present
in the single dimension arra..
The next n inputs are the elements in the arra..
Output Format:
First line output consists of arra. elements before sorting and the next line of the output
consists of arra. elements after sorting the elements in descending order.
33
8
3
Before sorting the arra. :
96583
After sorting the arra. :
98653
Solution:
int main()
{
int n;
int a[10];
input(a,n);
return 0;
34
}
if ( pos != i )
{
temp = a[i];
a[i] = a[pos];
a[pos] = temp;
}
}
}
Input Format:
The frst input consists of an integer which corresponds to the number of
elements present in the single dimension arra..
The next n inputs are the elements in the arra..
Output Format:
First line output consists of 3 foat values which corresponds to the mean,
variance & standard deviation values.
35
Sample Input and Output:
Enter the number of elements in the arra.
5
Enter the elements in the arra.
6
8
9
1
4
The Mean = 5.60, Variance = 8.24 and Standard deviation = 2.87
Solution:
int main()
{
float m,s,v;
int n;
int a[10];
printf("Enter the number of elements in the array\n");
scanf("%d",&n);
input(n,a);
m = mean(n,a);
s =sd(n,a);
v= variance(n,a);
printf("The Mean = %.2f, Variance = %.2f and Standard deviation = %.2f",m,v,s);
return 0;
}
void input(int n, int a[])
{
int i;
printf("Enter the elements in the array\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
}
36
float std=sqrt(variance(n, a));
return std;
}
for(i=0;i<n;i++)
sum=sum + pow(a[i]-m,2);
v=sum/n;
return v;
}
m=(float)sum/n;
return m;
}
37
2
Enter the size of the Second matrix :
2
2
Enter the elements of First matrix
1
2
3
4
Enter the elements of Second matrix
5
6
7
8
The Matrix after Multiplication is
19 22
43 50
Solution:
void mul();
void input(int x[10][10],int m,int n);
void display(int x[10][10], int m,int n);
int main()
{
if(n!=p)
{
printf("Incompatible Array size\n");
return -1;
}
else
38
{
mul();
return 0;
}
void mul()
{
int i,j,k;
for (i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]= c[i][j]+ a[i][k]*b[k][j];
}
}
}
39
printf("\n");
}
Module 9. Strings
Write a C program to determine whether a given word is a palindrome or not. Do not use
an. string librar. functions.
Input Format:
Input consists of a single string. Assume that the maximum length of the string is 50.
Output Format:
Refer sample input and output for formatting specifcations.
Solution:
// Check for string palindrome without using built-in functions
#include <stdio.h>
#include <string.h>
int main(){
char str[20];
40
int flag = 0;
flag = checkpalindrom(str);
if (flag) {
printf("%s is not a palindrome", str);
}
else {
printf("%s is a palindrome", str);
}
return 0;
}
if(string[i] != string[length-i-1])
{
flag = 1;
break;
}
}
return flag;
}
Write a C program to determine whether a given word is a palindrome or not. Use String
built-in function.
Input Format:
Input consists of a single string. Assume that the maximum length of the string is 50.
41
Output Format:
Refer sample input and output for formatting specifcations.
Solution:
// Check for string palindrome using built-in functions
#include <stdio.h>
#include <string.h>
for(i = 0;i<len/2;i++)
{
temp = str[i];
str[i] = str[len-i-1];
str[len-i-1] = temp;
}
//printf("Reversed: %s",str);
int main(){
char str[20];
int flag = 0;
42
flag = checkpalindrom(str);
if (flag ==1) {
printf("%s is a palindrome\n", str);
}
else {
printf("%s is not a palindrome\n", str);
}
return 0;
}
char str[20];
int flag = 0;
if(strcmp(string,str) == 0)
flag = 1;
return flag;
}
Input Format:
The frst input is an integer which corresponds to the number of emplo.ees.
Secondinput is a charecter arra. which corresponds to the name of the emplo.ee.
Third input is a charecter arra. which corresponds to the job of the emplo.ee.
Fourth input is a foat which corresponds to the salar. of the emplo.ee.
Output Format
Output should displa. the emplo.ee details which is given in the sample input and output
format.
43
[All text in bold corresponds to input and the rest corresponds to output]
Solution:
#include<stdio.h>
#include<string.h>
int main()
{
44
struct employee
{
char name[20];
char job[20];
float salary;
};
struct employee e[10];
int i,n;
printf("Enter the number of Employees :\n");
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("Enter the details of Employee :%d\n",i+1);
45
Get the student detailsNAME, USN, BRANCH,SEMESTERfrom the user
[All text in bold corresponds to input and the rest corresponds to output]
Solution:
#include <stdio.h>
int main()
{
int num,sem;
char name[100];
char branch[100];
FILE *fptr;
fptr = fopen("student.rec","w");
if(fptr == NULL)
{
printf("Error!");
return 0;
}
printf("Enter name:\n");
scanf("%s",name);
fprintf(fptr,"Name : ");
fprintf(fptr,"%s\n",name);
printf("Enter USN:\n");
scanf("%d",&num);
fprintf(fptr,"USN : ");
fprintf(fptr,"%d\n",num);
printf("Enter branch:\n");
scanf("%s",branch);
fprintf(fptr,"Branch : ");
fprintf(fptr,"%s\n",branch);
printf("Enter sementer:\n");
scanf("%d",&sem);
46
fprintf(fptr,"Semester : ");
fprintf(fptr,"%d\n",sem);
fclose(fptr);
return 0;
}
47