C Lab Manual
C Lab Manual
com 1
EX 1
PROGRAM USING I/O STATEMENTS AND EXPRESSIONS
DATE:
AIM:
ALGORITHM:
Step 1: Start
Step 2: Declare and initialize the variables for name, address, date of birth, mobile
number and age
Step 3: Display name, address, date of birth, mobile number and age
Step 4: End
PROGRAM:
#include<stdio.h>
void main()
{
char name[20]= "SAI RAM";
char address[80]= "west tambharam,chennai";
int date=20;
int month=10;
int year=1990;
int mobile=987456321;
int age=25;
printf("\n=====================");
printf("\n NAME: %s",name);
printf("\n ADDRESS:%s", address);
printf("\n DOB:%d:%d:%d", date , month, year);
printf("\n MOBILE NUMBER:%d", mobile);
printf("\n AGE:%d", age);
printf("\n=====================");
}
www.Vidyarthiplus.com
www.Vidyarthiplus.com 2
OUTPUT:
RESULT
Thus the C Program to display the personal details has been executed and the output was
verified.
www.Vidyarthiplus.com
www.Vidyarthiplus.com 3
AIM:
ALGORITHM:
Step 1: Start
Step 2: Declare the variables for name, address, date, month, year, mobile number,
age.
Step 3: Read values of name, address , date, month, year, mobile number, age
from the user.
Step 4: Display name, address, date, month, year, mobile number, age.
Step 5: End
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char name[20];
char address[80];
int date;
int month;
int year;
long int mobile;
char gender[20];
int age;
printf("\n ENTER YOUR NAME:=");
gets(name);
printf("\nENTER YOUR ADDRESS=");
gets(address);
printf("\nENTER YOUR date/month/year=");
scanf("%d/%d/%d",&date,&month,&year);
printf("\n ENTER YOUR AGE=");
scanf("%d",&age);
printf("\n ENTER YOUR GENDER(MALE/FEMALE)=");
scanf("%s",gender);
printf("\nENTER YOUR MOBILE NUMBER=");
scanf("%ld" ,&mobile);
www.Vidyarthiplus.com
www.Vidyarthiplus.com 4
printf("\n=====================");
printf("\n NAME: %s",name);
printf("\n ADDRESS:%s", address);
printf("\n DOB:%d:%d:%d", date , month, year);
printf("\n AGE:%d", age);
printf("\n GENDER:%s", gender);
printf("\n MOBILE NUMBER:%d", mobile);
printf("\n=====================");
return 0;
}
OUTPUT:
=====================
NAME: karthikeyan
ADDRESS:west tambharam,chennai.
DOB:3:12:1992
AGE:28
GENDER:MALE
MOBILE NUMBER:987654321
========================
RESULT:
Thus the C Program to read and display the user details has been executed and the
output was verified.
www.Vidyarthiplus.com
www.Vidyarthiplus.com 5
EX 2A
PROGRAM TO DISPLAY BIGGEST OF TWO NUMBERS
DATE:
AIM:
ALGORITHM:
Step 1:Start
Step 8:end
PROGRAM:
#include <stdio.h>
void main()
{
int A,B,C;
printf("Enter 3 integer number \n");
scanf("%d",&A);
scanf("%d",&B);
scanf("%d",&C);
if(A>B){
if(A>C){
printf(" %d is the Greatest Number \n",A);
}
else{
printf("%d is the greatest Number \n",C);
}
}
else{
if(B>C){
www.Vidyarthiplus.com
www.Vidyarthiplus.com 6
OUTPUT:
RESULT:
Thus the C Program to display the personal details has been executed and the output was
verified.
www.Vidyarthiplus.com
www.Vidyarthiplus.com 7
AIM:
ii] Program to check whether the entered character is vowel or not(Use switch case)
ALGORITHM:
Step 1: Start
Step 2: Declare and initialize the variables
Step 3: Get the input from the user and compare with each cases
Step 4: if match found, print vowel otherwise print consonant
Step 5: End
PROGRAM:
#include<stdio.h>
#include<conio.h>
int main()
{
char ch;
printf("Enter a character: ");
scanf("%c",&ch);
//condition to check character is alphabet or not
if((ch>='A' && ch<='Z') || (ch>='a' && ch<='z'))
{
switch(ch)
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf("%c is a VOWEL.\n",ch);
www.Vidyarthiplus.com
www.Vidyarthiplus.com 8
break;
default:
printf("%c is a CONSONANT.\n",ch);
}
}
else
{
printf("%c is not an alphabet.\n",ch);
}
return 0;
}
OUTPUT:
Enter a character
E is a vowel
Enter a character
X is a consonant
Enter a character
+ is not an alphabet
RESULT:
Thus the C Program check whether the entered character is vowel or not (Use switch
case) has been executed and the output was verified.
www.Vidyarthiplus.com
www.Vidyarthiplus.com 9
AIM:
To write a C Program to find whether the given year is leap year or not.
ALGORITHM:
Step 1: Start
Step 3: Check if year is divisible by 400 then DISPLAY "is a leap year"
Step 4: Check if year is not divisible by 100 AND divisible by 4 then DISPLAY "is a
leap year"
Step 6: Stop
PROGRAM:
#include <stdio.h>
#include <conio.h>
void main()
{
int year;
printf("Enter a year :\n");
scanf("%d", &year);
if ((year % 400) == 0)
printf("%d is a leap year \n",year);
else
if ((year % 100) != 0 && (year % 4) == 0)
printf("%d is a leap year \n",year);
else
printf("%d is not a leap year \n",year);
}
www.Vidyarthiplus.com
www.Vidyarthiplus.com 10
OUTPUT:
Enter a year:
2000
2000 is a leap year
Enter a year:
1900
1900 is not a leap year
RESULT:
Thus the C Program to find whether the given year is leap year or not has been
executed successfully and the output was verified.
www.Vidyarthiplus.com
www.Vidyarthiplus.com 11
EX 4 PROGRAM TO PERFORM THE CALCULATOR OPERATIONS,
NAMELY, ADDITION, SUBTRACTION, MULTIPLICATION, DIVISION
DATE: AND SQUARE OF ANUMBER
AIM:
ALGORITHM:
PROGRAM:
#include<stdio.h>
// functions declaration
// main function
int main()
www.Vidyarthiplus.com
www.Vidyarthiplus.com 12
printf(“%d^%d=%d\n”,num1,num1,square( num1));
return 0;
int result;
result = n1 + n2;
return result;
int result;
result = n1 - n2;
return result;
www.Vidyarthiplus.com
www.Vidyarthiplus.com 13
int result;
result = n1 * n2;
return result;
int result;
result = n1 / n2;
return result;
int result;
result = n1*n1;
return result;
www.Vidyarthiplus.com
www.Vidyarthiplus.com 14
OUTPUT:
20 + 5 = 25
20 – 5 = 15
20 * 5 = 100
20 / 5 = 4
20^20 = 400
RESULT
www.Vidyarthiplus.com
www.Vidyarthiplus.com 15
EX 5:
AIM:
ALGORITHM:
Step 1: Start
Step 5: Repeat Until num>=0 5.1 sum=sum + cube of last digit i.e
[(num%10)*(num%10)*(num%10)] 5.2 num=num/10
Step 7: Stop
PROGRAM:
#include<stdio.h>
int main()
{
int num,copy_of_num,sum=0,rem;
printf("\nEnter a number:");
scanf("%d",&num);
while (num != 0)
{
rem = num % 10;
sum = sum + (rem*rem*rem);
num = num / 10;
}
if(copy_of_num == sum)
printf("\n%d is an Armstrong Number",copy_of_num);
else
printf("\n%d is not an Armstrong Number",copy_of_num);
return(0);
}
www.Vidyarthiplus.com
www.Vidyarthiplus.com 16
OUTPUT:
RESULT:
Thus the C Program to check whether a given number is Armstrong or not has been
executed and the output was verified.
www.Vidyarthiplus.com
www.Vidyarthiplus.com 17
AIM:
ALGORITHM:
Step 4: If the number is even check the condition as n%2 ==0 else it is even.
PROGRAM:
#include <stdio.h>
int main()
int number;
scanf("%d", &number);
else
return 0;
www.Vidyarthiplus.com
www.Vidyarthiplus.com 18
OUTPUT:
Enter an integer: -7
-7 is odd.
Enter an integer : 8
8 is even
RESULT
Thus the C Program to find whether the given number is odd or even number has been
successfully executed and verified
www.Vidyarthiplus.com
www.Vidyarthiplus.com 19
EX 7
PROGRAM TO FIND FACTORIAL OF A GIVEN NUMBER
DATE:
AIM:
ALGORITHM:
PROGRAM:
int main()
int n, i; longfactorial = 1;
printf("Enter an integer: ");
scanf("%d",&n);
// show error if the user enters a negative integer
if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else
factorial *= i;
// factorial = factorial*i;
}
printf("Factorial of %d = %lu", n, factorial);
}
return 0;
}
OUTPUT:
Enter an integer: 10
Factoriaof 10 = 3628800
RESULT
Thus the C Program to find the factorial of a given number has been successfully
executed and verified.
www.Vidyarthiplus.com
www.Vidyarthiplus.com 21
EX 8
PROGRAM TO FIND OUT THE AVERAGE OF 4 INTEGERS
DATE:
AIM:
To find average of 4 integers
ALGORITHM:
Step 1. Start
Step 6. Stop
PROGRAM:
#include<stdio.h>
void main()
inti,n,sum=0,nu[100];
float avg;
clrscr();
for(i=0;i<3;i++)
scanf("%d",&nu[i]);
sum = sum + nu[i];
avg = (float)sum/n;
www.Vidyarthiplus.com
www.Vidyarthiplus.com 22
printf("\nAverage is : %.2f\n",n,avg);
getch();
}
OUTPUT:
32
45
54
22
Average is 38.25
RESULT
Thus the C Program to find the average of 4 numbers has been executed and verified.
www.Vidyarthiplus.com
www.Vidyarthiplus.com 23
EX 9
PROGRAM TO DISPLAY ARRAY ELEMENTS USING 2D ARRAYS
DATE:
AIM:
ALGORITHM:
PROGRAM:
#include<stdio.h>
int main(){
/* 2D array declaration*/
int disp[2][3];
int i, j;
for(j=0;j<3;j++) {
scanf("%d", &disp[i][j]);
for(j=0;j<3;j++) {
if(j==2){
printf("\n");
return 0;
OUTPUT:
123
456
RESULT:
Thus the C Program to display the array elements of the 2D array has been executed
and the result was verified
www.Vidyarthiplus.com
www.Vidyarthiplus.com 25
EX 10
PROGRAM TO PERFORM SWAPPING USING FUNCTIONS
DATE:
AIM:
ALGORITHM:
PROGRAM:
#include<stdio.h>
void main()
void swap(int,int);
inta,b,r;
clrscr();
printf("enter value for a&b: ");
scanf("%d%d",&a,&b);
swap(a,b);
getch();
void swap(inta,int b)
int temp;
temp=a;
a=b;
www.Vidyarthiplus.com
www.Vidyarthiplus.com 26
b=temp;
printf("after swapping the value for a & b is : %d %d",a,b);
OUTPUT:
RESULT:
Thus the C Program to swap two numbers using functions has been executed and
verified
www.Vidyarthiplus.com
www.Vidyarthiplus.com 27
EX 11
PROGRAM TO DISPLAY ALL PRIME NUMBERS BETWEEN
DATE: TWO INTERVALS USING FUNCTIONS
AIM:
ALGORITHM:
Step 3: Find and Display the prime numbers ie., the numbers that are divisible by 1
and itself between the intervals
PROGRAM:
#include <stdio.h>
/* Function declarations */
int main()
printPrimes(lowerLimit, upperLimit);
return 0;
www.Vidyarthiplus.com
www.Vidyarthiplus.com 28
/* Print all prime numbers between lower limit and upper limit*/
if(isPrime(lowerLimit))
lowerLimit++;
int i;
www.Vidyarthiplus.com
www.Vidyarthiplus.com 29
if(num % i == 0)
return 0;
return 1;
OUTPUT:
1 100
All prime number between 1 100 are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,
47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97
RESULT:
Thus the C Program to find the prime numbers between two intervals has been
executed and verified.
www.Vidyarthiplus.com
www.Vidyarthiplus.com 30
EX 12
PROGRAM TO REVERSE A SENTENCE USING RECURSION
DATE:
AIM:
ALGORITHM:
Step 1: Start
Step 2: Declare the function reverse
Step 3: Call the reverse function
Step 4: Get the sentence from the user and reverse it recursively
Step 5: stop the execution.
PROGRAM:
#include <stdio.h>
void reverseSentence();
int main() {
printf("Enter a sentence: ");
reverseSentence();
return 0;
}
void reverseSentence() {
char c;
scanf("%c", &c);
if (c != '\n') {
reverseSentence();
printf("%c", c);
}
}
OUTPUT:
Enter a sentence: margorp emosewa
awesome PROGRAM:
RESULT
Thus the C Program to reverse a sentence has been executed and verified
www.Vidyarthiplus.com
www.Vidyarthiplus.com 31
EX 13
PROGRAM TO GET THE LARGEST ELEMENT OF AN ARRAY
DATE: USINGFUNCTION
AIM:
ALGORITHM:
PROGRAM:
#include <stdio.h>
#include <conio.h>
max(int [],int);
void main()
int a[]={10,5,45,12,19};
int n=5,m;
clrscr();
m=max(a,n);
getch();
max(int x[],int k)
www.Vidyarthiplus.com
www.Vidyarthiplus.com 32
int t,i;
t=x[0];
for(i=1;i<k;i++)
if(x[i]>t)
t=x[i];
return(t);
OUTPUT:
Maximum number is 45
RESULT:
Thus the C Program to display the largest number in an array using function has been
executed and verified.
www.Vidyarthiplus.com
www.Vidyarthiplus.com 33
EX 14
PROGRAM TO CONCATENATE TWO STRINGS
DATE:
AIM:
ALGORITHM:
Step1: Start
Step 2: Get the two Strings to be concatenated.
Step 3: Declare a new String to store the concatenated String.
Step 4: Insert the first string in the new string.
Step 5: Insert the second string in the new string.
Step 6: Print the concatenated string.
Step 7: Stop
PROGRAM:
#include <stdio.h>
#include <string.h>
int main()
{
char destination[] = "Hello ";
char source[] = "World!";
printf("Concatenated String: %s\n", strcat(destination,source));
return 0;
}
OUTPUT:
RESULT:
Thus the C Program to concatenate two strings has been executed and the result was
verified.
www.Vidyarthiplus.com
www.Vidyarthiplus.com 34
EX 15
PROGRAM TO FIND THE LENGTH OF STRING
DATE:
AIM:
ALGORITHM:
PROGRAM:
#include <stdio.h>
#include <string.h>
int main()
{
char a[100];
int length;
printf("\n Enter a string to calculate its length=");
gets(a);
length = strlen(a);
printf("\nLength of the string = %d\n", length);
return 0;
}
#include <stdio.h>
#include<string.h>
int main() www.Vidyarthiplus.com
www.Vidyarthiplus.com 35
{
char i=0;a[100];
int length;
printf("\nEnter a string to calculate its length=");
scanf(“%s”,str);
while(string1[i] !='\0') {
i++;
}
length=i;
printf (“\n Length of the string = %d\n",length);
return 0;
}
OUTPUT:
RESULT:
Thus the C Program to find the length of the string has been executed and verified.
www.Vidyarthiplus.com
www.Vidyarthiplus.com 36
EX 16
PROGRAM TO FIND THE FREQUENCY OF A CHARACTER
IN A STRING
DATE:
AIM:
ALGORITHM:
PROGRAM:
#include <stdio.h>
int main() {
char str[1000], ch;
int count = 0;
OUTPUT:
Frequency of e = 4
RESULT:
Thus the C Program to find the frequency of a character in a string has been executed
and verified.
www.Vidyarthiplus.com
www.Vidyarthiplus.com 38
EX 17
PROGRAM TO STORE STUDENT INFORMATION IN STRUCTURE
DATE: AND DISPLAY IT
AIM:
ALGORITHM:
Step 1: START
Step 2: Read student details like name, mark1,2,3
Step 3: Calculate total, and average
Step 4: Display the grade
Step 5: STOP
PROGRAM:
#include<stdio.h>
struct student
{
int roll_no, mark1, mark2, mark3, total;
float average;
char name[10],grade;
};
void struct_funct_student(struct student stu);
int main()
{
struct student stud;
printf("\nRoll No.=");
scanf("%d",&stud.roll_no);
printf("Name=");
scanf("%s",stud.name);
printf("Mark1=");
scanf("%d",&stud.mark1);
printf("Mark2=");
scanf("%d",&stud.mark2);
printf("Mark3=");
www.Vidyarthiplus.com
www.Vidyarthiplus.com 39
scanf("%d",&stud.mark3);
struct_funct_student(stud);
return 0;
}
void struct_funct_student( struct student stu)
{
stu.total = stu.mark1 + stu.mark2 + stu.mark3;
stu.average = stu.total / 3;
if(stu.average >= 90)
stu.grade='S';
else if(stu.average >= 80)
stu.grade='A';
else if(stu.average >= 70)
stu.grade='B';
else if(stu.average >= 60)
stu.grade='C';
else if(stu.average >= 50)
stu.grade='D';
else
stu.grade='F';
printf("\n ROLL NO. \t NAME \t TOTAL \t AVG \t
GRADE \n");
printf("%d \t %s \t %d \t %f \t %c",
stu.roll_no,stu.name,stu.total,stu.average,stu.grade);
}
www.Vidyarthiplus.com
www.Vidyarthiplus.com 40
OUTPUT:
Roll No.= 1
Name= a
Mark1= 95
Mark2= 94
Mark3= 96
1 a 285 95.000000 S
RESULT:
Thus the C Program to store and display student details using structures has been
executed and the result was verified.
www.Vidyarthiplus.com
www.Vidyarthiplus.com 41
EX 18
PROGRAM TO READ THE STUDENT DATA AND CALCULATE
DATE: THETOTAL MARKS
AIM:
ALGORITHM:
Step 4 : Calculate the student who got the highest total marks
PROGRAM:
#include<stdio.h>
struct student
int sub1;
int sub2;
int sub3;
int sub4;
int sub5;
};
void main()
int i,total=0;
www.Vidyarthiplus.com
www.Vidyarthiplus.com 42
clrscr();
for(i=0;i<=9;i++)
scanf("%d%d%d",& s[i].sub1,&s[i].sub2,&s[i].sub3,&s[i].sub4,&s[i].sub5);
total=s[i].sub1+s[i].sub2+s[i].sub3+s[i].sub4+s[i].sub5;
getch();
OUTPUT:
80 70 90 80 98
RESULT:
Thus the C Program to print the student details has been executed and the result was
verified.
www.Vidyarthiplus.com
www.Vidyarthiplus.com 43
EX :19
AIM :
To insert, update, delete and append telephone details of an individual or a company into a
telephone directory using random access file.
ALGORITHM :
Step 1: Create a random access file
Step 2: call the respective procedure to insert, update, delete or append based on user choice
Step 3: Access the random access file to make the necessary changes and save
PROGRAM
#include "stdio.h"
#include "string.h"
#include<stdlib.h>
#include<fcntl.h>
struct dir
{
char name[20];
char number[10];
};
int record = 0;
int main(void) {
int choice = 0;
FILE *fp = fopen( "telephone.dat", "rb+" );
if (fp == NULL ) perror ("Error opening file");
while (choice != 6)
{
printf("\n1 insert\t 2 update\n");
printf("3 delete\t 4 display\n");
printf("5 search\t 6 Exit\n Enter choice:");
scanf("%d", &choice);
switch(choice)
{
case 1: insert(fp); break;
case 2: update(fp); break;
case 3: del(fp); break;
case 4: display(fp); break;
case 5: search(fp); break;
default: ;
} www.Vidyarthiplus.com
}
www.Vidyarthiplus.com 44
fclose(fp);
return 0;
}
OUTPUT:
3 delete 4 display
5 search 6 Exit
Enter choice: 4
Telephone directory
Name Number
*******************************
bb 11111
*******************************
1 insert 2 update
3 delete 4 display
5 search 6 Exit
Enter choice: 5
Enter name: bb
bb 11111
1 insert 2 update
3 delete 4 display
5 search 6 Exit
Enter choice: 1
Enter individual/company name: aa
Enter telephone number: 222222
1 insert 2 update
3 delete 4 display
5 search 6 Exit
Enter choice: 2
Enter name: aa
Enter number: 333333
Updated successfully
1 insert 2 update
3 delete 4 display
5 search 6 Exit
Enter choice:
Telephone directory
Name Number
*******************************
bb 11111
aa 333333
*******************************
1 insert 2 update
3 delete 4 display
5 search 6 Exit www.Vidyarthiplus.com
www.Vidyarthiplus.com 47
Enter choice: 3
Enter name: aa
1 Deleted successfully
1 insert 2 update
3 delete 4 display
5 search 6 Exit
Enter choice: 4
Telephone directory
Name Number
*******************************
bb 11111
*******************************
1 insert 2 update
3 delete 4 display
5 search 6 Exit
Enter choice: 6
RESULT:
Thus the C program To insert, update, delete and append telephone details of an
individual or a company into a telephone directory using random access file was successfully
written and executed.
www.Vidyarthiplus.com
www.Vidyarthiplus.com 48
EX :20
PROGRAM TO COUNT THE NUMBER OF ACCOUNT HOLDERS
WHOSEBALANCE IS LESS THAN THE MINIMUM BALANCE USING
DATE:
SEQUENTIAL ACCESS FILE
AIM:
To count the number of account holders whose balance is less than the minimum
balance using sequential access file.
ALGORITHM :
PROGRAM:
#include <stdio.h>
void insert();
void count();
int main(void)
{
int choice = 0;
while (choice != 3)
{
printf("\n1 insert records\n");
printf("2 Count min balance holders\n");
printf("3 Exit\n");
printf("Enter choice:");
scanf("%d", &choice);
www.Vidyarthiplus.com
www.Vidyarthiplus.com 49
switch(choice)
{
case 1: insert(); break;
case 2: count(); break;
}
}
}
void insert()
{
unsigned int account,i;
char name[30];
double balance;
FILE* cfPtr;
void count()
{
unsigned int account;
char name[30];
double balance;
float minBal = 5000.00;
int count = 0;
FILE *cfPtr;
if ((cfPtr = fopen("clients.dat", "r")) == NULL)
printf("File could not be opened");
else
www.Vidyarthiplus.com
www.Vidyarthiplus.com 50
{
printf("%-10s%-13s%s\n", "Account", "Name", "Balance");
fscanf(cfPtr, "%d%29s%lf", &account, name, &balance);
while (!feof(cfPtr))
{
if (balance < minBal)
{
printf("%-10d%-13s%7.2f\n", account, name, balance);
count++;
}
fscanf(cfPtr, "%d%29s%lf", &account, name, &balance);
}
fclose(cfPtr);
printf("The number of account holders whose balance is less than the minimum balance:
%d", count);
}
}
www.Vidyarthiplus.com
www.Vidyarthiplus.com 51
OUTPUT:
1 insert records
2 Count min balance holders
3 Exit
Enter choice:1
Enter the No. of records 2
Enter the account, name, and balance.1001 A 10000
Enter the account, name, and balance.1002 B 300
1 insert records
2 Count min balance holders
3 Exit
Enter choice:2
Account Name Balance
1002 B 300.00
The number of account holders whose balance is less than the minimum balance: 1
1 insert records
2 Count min balance holders
3 Exit
Enter choice:
RESULT :
Thus the C Program to count the number of account holders whose balance is less
than the minimum balance using sequential access file
www.Vidyarthiplus.com