CP Lab Manual-Program
CP Lab Manual-Program
DATE:
Aim:
To write a C Program to find area and circumference of the circle using I/O
statements and Expressions
Algorithm:
Step 1: Start
Step 2: Declare the variables Area, circum, pi, r
clrscr();
printf(“Enter the radius of the circle:”);
scanf(“%f”,&r);
Area=pi*r*r; circum=2*pi*r;
printf(“\nArea=%f\ncircumference=%f”,Area,circum);
getch();
}
Output:
Enter the radius of the
circle: 10 Area=314.20
Circumference=62.8
Result:
Thus a C Program to find area and circumference of the circle using I/O
statements and Expressions was executed and the output was verified.
EX.No.: 1b PROGRAM USING I/O STATEMENTS AND EXPRESSIONS
DATE :
Aim:
To write a C Program to find simple interest using I/O statements and Expressions
Algorithm:
Step 1: Start
Step 2: Declare pamt, r, n, si=0;
Step 3: Calculate the simple interest using the expression si=((pamt*r*n)/100)
Step 4: Print si
Step 5: Stop
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
float pamt,n,r,si;
clrscr();
printf(“Enter the value of pamt,n,r:”);
scanf(“\n%f\n%f\n%f”,&pamt,&r,&n);
si=((pamt*r*n)/100);
printf(“\nSimple interest=%f”,si);
getch();
}
Output:
Enter the value of
pamt,n,r: 2500
10
2
Simple interest: 500
Result:
Thus a C Program to find simple Interest using I/O statements and Expressions was
executed successfully and output was verified.
EX.No.: 2a PROGRAM USING DECISION-MAKING CONSTRUCTS
DATE:
Aim:
To write a C program to find greatest of three numbers using decision making
constructs.
Algorithm:
Step 1: Start
Step 2: Declare the variable a, b and c
Step 3: Read the variable a,b and c
Step 4: Check the condition
Step 4.1:if (a>b) and (a>c)
Display a is greater number
Step 4.2: elseif (b>c) and (b>a)
Display b is greater number
Step 4.3: else
Display c is greater number
Step 5: Stop
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(“\n Enter the first number: ”);
scanf(“%d”,&a);
printf(“\n Enter the second number:”);
scanf(“%d”,&b);
printf(“\nEnter the third number: ”);
scanf(“%d”,&c);
if (a>b && a>c)
printf(“\n %d is greater number”,a);
10 is greater number
Result:
Thus a C Program to find greatest among three numbers using decision making
constructs was executed successfully and output was verified.
EX.No.:2b PROGRAM USING DECISION-MAKING CONSTRUCTS
DATE:
Aim:
To write a C program to find odd or even number using decision making constructs.
Algorithm:
Step 1: Start
Step 2: Declare the variable a
Step 3: Read the variable a
Step 4: Check if (a%2==0)
Step 4.1: if it is true
Step 4.2: Display a is even number
Step 4.3: else
Step 4.4: Display a is odd number
Step 5: Stop
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
printf(“\n Enter the number a :”);
scanf(“%d”,&a);
if (a%2==0)
{
printf(“\n %d is even number”,a);
else
Output:
Result:
Thus the C program to find odd or even number using decision making constructs
program was successfully executed and output was verified.
EX. No.: 3 LEAP YEAR CHECKING
DATE:
Aim:
To write a C Program to find whether the given year is leap year or not.
Algorithm:
Step 1: Start
Step 6: If the condition at step 5.1 becomes true, then print the output as “It is a leap
year”.
Step 7: If the condition at step 5.2 becomes true, then print the ouput as “It is not a
leap year”.
Step 8: If the condition at step 5.3 becomes true, then print the ouput as “It is a leap
year”.
Step 9: If neither of the condition becomes true, then the year is not a leap year and
print the same.
#include<stdio.h>
#include<conio.h>
void main()
{
int year;
scanf("%d", &year);
if ((year % 400) == 0)
else if ((year % 4) == 0)
else
Output:
Result:
Thus a C Program for Leap year checking was executed successfully and the
output was verified.
EX. No.: 4 ARITHMETIC OPERATIONS
DATE:
Aim:
To write a C Program to Design a calculator to perform the operations, namely,
addition, subtraction, multiplication, division and square of a number.
Algorithm:
Step 1: Start
Step 2: Initialize n1 ,n2 , op, variables
Step 3: Display the menu with option add, sub, mul and div, square
Step 4: Use switch () statement.
Step 5: Display the respective result.
Step 6: Stop.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int
a,b,op;
printf(“\n Enter two number:”);
scanf(“%d%d”, &a,&b);
printf(“\n menu”);
printf(“\n 1.Addtion”);
printf(“\n 2.Subraction”);
printf(”\n 3.Multiplication”);
printf(“\n 4.Division”);
printf(“\n 5. Square”);
printf(“\nPlease select the operation”) ;
scanf(“%d”,&op);
switch(op)
{
case 1:
{
printf(“\n Addition is :%d”,a+b); brea
}
case 2::
{
printf(“\Subtraction is:%d”,a-b);
break;
}
case 3:
{
printf(“\n Multiplication is:%d,a*b);
break;
}
case 4:
{
printf(“\n Division is:%d”,a/b);
break;
}
case 5:
{
printf(“\nSquare of %d is:%d”, a,a*a);
printf(“\nSquare of %d is:%d”, b, b*b);
break;
}
default:
{
printf(“\n Invalid operation”);
}
getch();
}
Output:
Enter two number: 20,10
menu 1.Addtion
2.Subraction
3.Multiplication
4.Division
5. Square
Please select the operation 1
Addition is :30
Please select the operation 2
Subtraction is:10
Please select the operation 3
Multiplication is:200 Please
select the operation 4
Division is: 2
Please select the operation 5
Square of 20 is:400
Square of 10 is:100
Please select the operation 10
Invalid operation
Result:
Thus a C Program for Arithmetic operations was executed successfully and the
output was verified.
EX. No.: 5 ARMSTRONG NUMBER
DATE:
Aim:
To write a C program to check whether a given number is Armstrong or not.
Algorithm:
Step 1: Start
Step 2: Declare the variables and assign a=n
Step 3: Using while loop
Step 3.1: remainder =number%10
Step 3.2: sum=sum+(remainder*remainder*remainder)
Step 3.3: number=number/10
Step 4: Repeat step 3 until number>0
Step 5: if sum==a
Step 6: Display number is Armstrong
Step 7: else display number is not Armstrong
Step 8: Stop
Program:
#include<stdio.h>
#include<conio.h>
void main ()
{
int n,r,a,s=0;
printf (“\n Enter a number :”);
scanf(“%d”,&n);
a=n;
while (n>0)
{
r=n%10;
s=s+(r*r*r); n=n/10;
}
if (a==s)
printf(“\n %d is an Armstrong number “,a)
else
printf(“\n %d is not an Armstrong number”, a);
}
Output:
Enter a number: 153
153 is an Armstrong number
Result:
Thus the C program to check whether a given number is Armstrong or not
was executed successfully and output was verified.
EX. No.: 6 SORT THE NUMBERS BASED ON THE WEIGHT
DATE:
Aim:
To write a C Program to perform the following:
Given a set of numbers like <10, 36, 54, 89, 12, 27>, find sum of weights based on the
following conditions
5 if it is a perfect cube
4 if it is a multiple of 4 and divisible by 6
3 if it is a prime number
Sort the numbers based on the weight in the increasing order as shown below
<10,its weight>,<36,its weight><89,its weight>
Algorithm:
Step1: Start
Step2: Declare variables
Step3: Read the number of elements.
Step7: Stop
Program:
#include<stdio.h>
#include<math.h>
void main()
{
int nArray[50],wArray[50],nelem,i,j,t;
printf("\nEnter the number of elements in an array : ");
scanf("%d",&nelem); printf("\nEnter %d elements\n",nelem);
for(i=0;i<nelem;i++)
scanf("%d",&nArray[i]);
for(i=0; i<nelem; i++)
{
wArray[i] = 0;
if(percube(nArray[i]))
wArray[i] = wArray[i] + 5;
if(nArray[i]%4==0 && nArray[i]%6==0)
wArray[i] = wArray[i] + 4;
if(prime(nArray[i]))
wArray[i] = wArray[i] + 3;
}
for(i=0;i<nelem;i++)
for(j=i+1;j<nelem;j++)
if(wArray[i] > wArray[j])
{
t = wArray[i]; wArray[i] = wArray[j]; wArray[j] = t;
}
break;
}
return flag;
}
int percube(int num)
{
int i,flag=0;
for(i=2;i<=num/2;i++)
if((i*i*i)==num)
{
flag=1; break;
}
return flag;
}
Output:
Enter the number of elements in an
array: 5 Enter 5 elements:
8
11
216
24
34
<34,0>
<11,3>
<24,4>
<8,5>
<216,9>
Explanation:
Result:
Thus a C Program for Sort the numbers based on the weight was executed
successfully and the output was verified.
EX. No. : 7 AVERAGE HEIGHT OF PERSONS
DATE:
Aim:
To write a C Program to populate an array with height of persons and find how many
persons are above the average height.
Algorithm:
Step1:Start
Step2: Declare variables
Step3: Read the total number of persons and their height.
Step4: Calculate avg=sum/n and find number of persons their h>avg.
Step5: Display the output of the calculations.
Step6: Stop
Program:
#include<stdio.h>
#include <conio.h>
void main()
{
int i,n,sum=0,count=0,height[100];
float avg;
clrscr();
printf("Enter the Number of Persons : ");
scanf("%d",&n);
printf("\nEnter the Height of each person incentimeter\n");
for(i=0;i<n;i++)
{
scanf("%d",&height[i]);
Output:
Enter the Number of Persons: 5
Enter the Height of each person in
centimeter 150
155
162
158
154
Result:
Thus a C Program average height of persons was executed successfully and the
output was verified.
EX. No.: 8 BODY MASS INDEX OF THE INDIVIDUALS
DATE:
Aim:
To write a C Program to Populate a two dimensional array with height and weight of
persons and compute the Body Mass Index of the individuals.
Algorithm:
Step1: Start
Step2: Declare variables
Step3: Read the number of persons and their height and weight.
Program:
#include<stdio.h>
#include<math.h>
void main()
{
int n,i,j;
float
massheight[5][5];
float bmi[n];
printf("How many people's BMI do you want to calculate?\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<2;j++)
{
switch(j)
{
case 0:
printf("\nPlease enter the mass of the person %d in kg: ",i+1);
scanf("%f",&massheight[i][0]);
break;
case 1:
printf("\nPlease enter the height of the person %d in meter: ",i+1);
scanf("%f",&massheight[i][1]);
break;
}
}
}
for(i=0;i<n;i++)
{
bmi[i]=massheight[i][0]/pow(massheight[i][1],2.0);
printf("Person %d's BMI is %f\n",i+1,bmi[i]);
getch();
}
Output:
Result:
Thus a C Program Body Mass Index of the individuals was executed successfully
and the output was verified.
EX. No.: 9 REVERSE OF A GIVEN STRING
DATE:
Aim:
To write a C Program to perform reverse without changing the position of special
characters for the given string.
Algorithm:
Step 1: Start
Step 2: Declare variables.
Step 3: Read a String.
Step 4: Check each character of string for alphabets or a special character by
using is Alpha ().
Step 5: Change the position of a character vice versa if it is alphabet otherwise remains
same.
Step 6: Display the output of the reverse string without changing the position of special
characters.
Step 7: Stop
Program:
#include <stdio.h>
#include <string.h>
#include <conio.h>
void swap(char *a, char *b)
{
char t;
t = *a;
*a = *b;
*b = t;
}
void main()
{
char str[100];
reverse(char *);
int isAlpha(char);
void swap(char *a ,char *b);
clrscr();
r--;
else
{
swap(&str[l], &str[r]);
l++;
r--;
}
}
int isAlpha(char x)
{
return ( (x >= 'A' && x <= 'Z') || (x >= 'a' && x <= 'z') );
}
Output:
Result:
Thus a C Program for reverse of a given String was executed successfully and the
output was verified.
EX. No.: 10 STRING OPERATIONS
DATE:
Aim:
To write a C program to perform string operations on a given paragraph for the following
using built-in functions:
Find the total number of words.
Algorithm:
Step1: Start
Step5: Compute do while loops and for loop until the given conditions are
satisfied for function.
Step6: Find the first word of each sentence to capitalize by checks to see if a character
is a punctuation mark used to denote the end of a sentence. ( . ? ‘ ‘)
Step7: Replace the word in the text by user specific word if match.
Step 9: Stop
Program:
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define MAX 100
void capitalize(char *);
int wordCount(char *);
void replace(char *);
void main()
{
char para[MAX]={0};
printf("Enter a paragraph:");
scanf("%[^\n]s",para);
capitalize(para);
printf("\n Capitalize paragraph is :%s",para);
printf("\n Number of words in given paragraph are : %d",wordCount(para));
replace(para);
printf("\n New paragraph is :%s",para);
getch();
}
void capitalize(char *p)
{
int i=0;
do
{
if(i==0||(p[i-1]==' '&& (p[i-2]=='.'|| p[i-2]=='?')))
{
do
{
p[i]=toupper(p[i]);
i++;
}while(p[i]!=' ');
}
i++;
}while(p[i]!='\0');
}
count++;
}
return count+1;
}
char str[10],rp[100],pp[100];
int j=0,k,len,length;
printf("\nEnter the word to be replaced:");
scanf("%s",word);
printf("\n Enter the word the %s to be replaced:",word);
scanf("%s",rpword);
length=strlen(p);
for(k=0;k<length;k++)
{
j=0;
do
{
if(p[k+j]!=' '||p[k+j]!='.'||p[k+j]!='?')
{
else
{
str[j]=p[k+j];
j++;
}
}while((p[k+j]!=' ') && (p[k+j]!='.') && (p[k+j]!='?') &&(p[k+j]!='\0'));
str[j]='\0';
if(strcmp(str,word)==0)
{
len=strlen(str);
strncpy(pp,p,k);
strncpy(rp,p+k+len,length-k-len);
strcpy(p,pp);
strcat(p,rpword);
strcat(p,rp);
}
}
}
Output:
Result:
Thus a C Program for String operations using built in functions was executed
successfully and the output was verified.
EX.No. : 11 CONVERSION OF DECIMAL NUMBER INTO OTHER BASES
DATE :
Aim:
To write a C Program to convert the given decimal number into binary, octal and
hexadecimal numbers using user defined functions.
Algorithm:
Step1: Start
Step2: Declare variables.
Step3: Read a decimal number.
Step4: Develop the procedure for conversion of different base by modulus and divide
operator.
Step5: Display the output of the conversion value.
Step6: Stop
Program:
#include <stdio.h>
#include <conio.h>
void swap(char *s1, char *s2)
{
char temp;
temp = *s1;
*s1 = *s2;
*s2 = temp;
}
void reverse(char *str, int length)
{
int start = 0;
int end = length -1;
}
}
if (num == 0)
{
str[i++] = '0';
str[i] = '\0';
return str;
}
while (num != 0)
{
int rem = num % base;
str[i++] = (rem > 9)? (rem-10) + 'a' : rem + '0';
num = num/base;
}
str[i] = '\0';
reverse(str, i);
return str;
}
void main()
{
char str[100];
int n;
printf("Enter the given decimal number : ");
scanf("%d",&n);
printf("\nThe Binary value : %s\n",convert(n,str,2));
printf("\nThe Octal value : %s\n",convert(n,str,8));
printf("\nThe Hexa value : %s\n",convert(n,str,16));
getch();
}
Output:
Result:
Thus a C program for conversion of decimal number into other bases was
executed successfully and the output was verified.
EX. No. : 12 TOWERS OF HANOI USING RECURSION
DATE:
Aim:
To write a C Program to solve towers of Hanoi using recursion.
Algorithm:
Step1: Start
Step4: Check the condition for each transfer of discs using recursion.
Step6: Stop
Program:
#include <stdio.h>
#include <conio.h>
void towerofhanoi(int n, char from, char to, char aux)
{
if (n == 1)
{
printf("\n Move disk 1 from pole %c to pole %c", from, to);
return;
}
int main()
int n;
printf("Enter the number of disks : ");
scanf("%d",&n);
towerofhanoi(n, 'A', 'C', 'B');
return 0;
}
Output:
Result:
Thus a C Program for Towers of Hanoi using Recursion was executed successfully
and the output was verified.
EX.No. : 13 SORTING USING PASS BY REFERENCE
DATE:
Aim:
Algorithm:
Step 1: Start
Step 3: Read the Input for number of elements and each element.
Step 5: Compare the elements in each pass till all the elements are sorted.
Step 7: Stop
Program:
#include <stdio.h>
#include <conio.h>
void main()
{
int n,a[100],i;
void sortarray(int*,int);
clrscr();
printf("\nEnter the Number of Elements in an array : ");
scanf("%d",&n);
printf("\nEnter the Array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sortarray(a,n);
printf("\nAfter Sorting....\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}
int i,j,temp;
for(i=0;i<num;i++)
for(j=i+1;j<num;j++)
arr[i] = arr[j];
arr[j] = temp;
}
Output:
Result:
Thus a C Program Sorting using pass by reference was executed and the output was
verified.
EX.No. : 14 BANKING APPLICATION
DATE:
Aim:
To write a C Program to Count the number of account holders whose balance is
less than the minimum balance using sequential access file.
Algorithm:
Step 1: Start
Step 8: Stop
Program:
/* Count the number of account holders whose balance is less than the minimum
balance using sequential access file.*/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#define MINBAL 500
struct Bank_Account
{
char no[10];
char name[20];
char balance[15];
};
struct Bank_Account acc;
void main()
{
long int pos1,pos2,pos;
FILE *fp;
char *ano,*amt;
char choice;
int type,flag=0;
float bal;
do
{
clrscr();
fflush(stdin);
printf("1. Add a New Account Holder\n");
printf("2. Display\n");
printf("3. Deposit or Withdraw\n");
printf("4. Number of Account Holder Whose Balance is less than the
MinimumBalance\n");
printf("5. Stop\n");
printf("Enter your choice : ");
choice=getchar(); switch(choice)
{
case '1' :
fflush(stdin);
fp=fopen("acc.dat","a");
printf("\nEnter the Account Number : ");
gets(acc.no);
printf("\nEnter the Account Holder Name : ");
gets(acc.name);
printf("\nEnter the Initial Amount to deposit : ");
gets(acc.balance); fseek(fp,0,2);
fwrite(&acc,sizeof(acc),1,fp);
fclose(fp);
break;
case '2' :
fp=fopen("acc.dat","r");
if(fp==NULL)
printf("\nFile is Empty");
else
{
printf("\nA/c Number\tA/c Holder Name Balance\n");
while(fread(&acc,sizeof(acc),1,fp)==1)
printf("%-10s\t\t%-20s\t%s\n",acc.no,acc.name,acc.balance); fclose(fp);
}
break;
case '3' :
fflush(stdin);
flag=0;
fp=fopen("acc.dat","r+");
printf("\nEnter the Account Number : ");
gets(ano);
for(pos1=ftell(fp);fread(&acc,sizeof(acc),1,fp)==1;pos1=ftell(fp))
{
if(strcmp(acc.no,ano)==0) {
printf("\nEnter the Type 1 for deposit & 2 for withdraw : ");
scanf("%d",&type);
printf("\nYour Current Balance is : %s",acc.balance);
printf("\nEnter the Amount to transact : ");
fflush(stdin);
gets(amt);
if(type==1)
bal = atof(acc.balance) + atof(amt);
else {
bal = atof(acc.balance) - atof(amt);
if(bal<0) {
printf("\nRs.%s Not available in your A/c\n",amt);
flag=2;
break;
}
}
flag++;
break;
}
}
if(flag==1)
{
pos2=ftell(fp);
pos = pos2-pos1;
fseek(fp,-pos,1);
sprintf(amt,"%.2f",bal);
strcpy(acc.balance,amt);
fwrite(&acc,sizeof(acc),1,fp);
}
else if(flag==0)
printf("\nA/c Number Not exits... Check it again");
fclose(fp);
break;
case '4' :
fp=fopen("acc.dat","r");
flag=0;
while(fread(&acc,sizeof(acc),1,fp)==1)
{
bal = atof(acc.balance);
if(bal<MINBAL)
flag++;
}
printf("\nThe Number of Account Holder whose Balance less than the Minimum
balance : %d", flag);
fclose(fp);
break;
case '5' :
fclose(fp);
exit(0);
}
printf("\nPress any key to continue....");
getch();
}
while (choice!='5');
}
Output:
1. Add a New Account Holder
2. Display
3. Deposit or Withdraw
4. Number of Account Holder Whose Balance is less than the Minimum Balance
5. Stop
The Number of Account Holder whose Balance less than the Minimum Balance : 0
Result:
Thus a C Program for Banking Application was executed and the output was verified.
EX.No.: 15 RAILWAY RESERVATION SYSTEM
DATE:
Aim:
Create a Railway reservation system in C with the following modules
• Booking
• Availability checking
• Cancellation
• Prepare chart
Algorithm:
Step 1: Start
Step 2: Declare variables
Step 3: Display the menu options
Step 4: Read the option.
Step 5: Develop the code for each option.
Step 6: Display the output of the selected option based on existence.
Step 7: Stop
Program:
#include<stdio.h>
#include<conio.h>
int first=5,second=5,third=5;
struct node
{
int ticketno;
int phoneno;
char name[100];
char address[100];
}s[15];
int i=0;
void booking()
{
printf("enter your details");
printf("\nname:");
scanf("%s",s[i].name);
printf("\nphonenumber:");
scanf("%d",&s[i].phoneno);
printf("\naddress:");
scanf("%s",s[i].address);
printf("\nticketnumber only 1-10:");
scanf("%d",&s[i].ticketno);
i++;
}
void availability()
{
int c;
printf("availability checking");
printf("\n1.first class\n2.second class\n3.third class\n");
printf("enter the option");
scanf("%d",&c);
switch(c)
{
case 1:
if(first>0)
{
printf("seat available\n");
first--;
}
else
{
printf("seat not available");
}
break;
case 2:
if(second>0)
{
printf("seat available\n");
second--;
}
else
{
printf("seat not available");
}
break;
case 3:
if(third>0)
{
printf("seat available\n");
third--;
}
else
{
printf("seat not available");
}
break;
default:
break;
}
}
void cancel()
{
int c;
printf("cancel\n");
printf("which class you want to cancel");
printf("\n1.first class\n2.second class\n3.thired class\n");
printf("enter the option");
scanf("%d",c);
switch(c)
{
case 1:
first++;
break;
case 2:
second++;
break;
case 3:
third++;
break;
default:
break;
}
printf("ticket is canceled");
}
void chart()
{
int c;
for(c=0;c<I;c++)
{
printf(“\n Ticket No\t Name\n”);
printf(“%d\t%s\n”,s[c].ticketno,s[c].name);
}
}
main()
{
int n;
clrscr();
printf("welcome to railway ticket reservation\n");
while(1)
{
printf("1.booking\n2.availability cheking\n3.cancel\n4.Chart \n5. Exit\n enter
your option:");
scanf("%d",&n);
switch(n)
{
case 1:
booking();
break;
case 2:
availability();
break;
case 3:
cancel();
break;
case 4:
chart();
break;
case 5:
printf(“\n Thank you visit again!”);
getch();
exit(0);
default:
break;
}
}
getch();
}
Output:
availability checking
1.first class
2.second class
3.third class
Result:
Thus a C Program for Railway reservation system was executed and the output was
verified.