C Lab Program
C Lab Program
PROGRAM :
#include <stdio.h>
int main()
{
int num1,num2,sum=0;
printf("Enter the numbers :");
scanf("%d%d",&num1,&num2);
sum=num1+num2;
printf("Sum of two numbers is :%d",sum);
}
OUTPUT :
Enter the numbers :23
12
Sum of two numbers is :35
EXERCISE NO :2
AREA AND CIRCUMFERENCE OF CIRCLE
PROGRAM :
#include <stdio.h>
int main()
{
float radius,area,circumference;
printf("Enter the redius of circle : ");
scanf("%f",&radius);
area=3.14*radius*radius;
circumference=2*3.14*radius;
printf("\nArea of a circle :%.2f",area);
printf("\nCircumference of a circle :%.2f",circumference);
}
OUTPUT :
Enter the radius of circle : 3
Area of a circle :28.26
Circumference of a circle :18.84
EXERCISE NO :3
FIND WHETHER THE GIVEN NUMBERS IS EVEN OR ODD
PROGRAM :
#include <stdio.h>
int main()
{
int num;
printf("Enter the number : ");
scanf("%d",&num);
if(num%2==0){
printf("The number is even");
}else{
printf("The number is odd");
}
}
OUTPUT :
Enter the number :22
The number is even
EXERCISE NO :4
GREATEST OF THREE NUMBERS
PROGRAM :
#include <stdio.h>
int main()
{
int a,b,c;
printf("Enter three nummbers : \n");
scanf("%d%d%d",&a,&b,&c);
if(a>=b){
if(a>=c)
printf("%d is the largest number",a);
else
printf("%d is the largest number",c);
}else{
if(b>=c)
printf("%d is the largest number",b);
else
printf("%d is the largest number",c);
}
}
OUTPUT :
Enter three numbers :
23 24 29
29 is the largest number
EXERCISE NO :5
LEAP YEAR OR NOT
PROGRAM :
#include <stdio.h>
int main()
{
int year;
printf("Enter a year : ");
scanf("%d",&year);
if(((year%4==0) && (year%100!=0))||(year%400==0))
printf("%d is a leap year",year);
else
printf("%d is not a leap year",year);
}
OUTPUT :
PROGRAM :
#include <stdio.h>
int main()
{
int num;
printf("Enter any number : ");
scanf("%d",&num);
if(num>0){
printf("Number is POSITIVE");
}if(num<0){
printf("Number is NEGATIVE");
}if(num==0){
printf("Number is ZERO");
}
}
OUTPUT :
Enter any number :32
Number is POSITIVE
EXERCISE NO:7
SIMPLE CALCULATOR
PROGRAM :
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b,add,sub,mult,squaring;
printf("Enter the value of a and b :\n");
scanf("%d%d",&a,&b);
add=a+b;
sub=a-b;
mult=a*b;
float divi=(float)a/b;
squaring=a*a;
printf("%d %d %d %.3f %d",add,sub,mult,divi,squaring);
}
OUTPUT :
Enter a value of a and b :2 3
5 -1 6 0.667 4
EXERCISE NO :8
ARMSTRONG NUMBER CHECKING
PROGRAM :
#include <stdio.h>
#include <math.h>
int main()
{
int n,originalnum,rem,i=0,result=0;
printf("Enter a number :\n");
scanf("%d",&n);
originalnum=n;
for(originalnum=n;originalnum!=0;++i){
originalnum/=10;
}for(originalnum=n;originalnum!=0;originalnum/=10){
rem=originalnum%10;
result=result+pow(rem,i);
}
if(n==result){
printf("yes");
}else{
printf("no");
}
}
OUTPUT :
Enter a number :
1634
yes
EXERCISE NO :9
SUM OF DIGITS OF THE NUMBERS
PROGRAM :
#include <stdio.h>
int main()
{
int n,sum=0,m;
printf("Enter a number :");
scanf("%d",&n);
while(n>0){
m=n%10;
sum=sum+m;
n=n/10;
}
printf("sum is =%d",sum);
return 0;
}
OUTPUT :
Enter a number :345
Sum is =12
EXERCISE NO :10
FIND SUM OF WEIGHTS BASED ON CONDITIONS
PROGRAM :
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n,i,num,j,swap,is_prime;
printf("Enter a number of elements :");
scanf("%d",&n);
int a[n],b[n];
for(i=0;i<n;i++){
scanf("%d",&a[i]);
} for(i=0;i<n;i++){
num=a[i];
is_prime=1;
b[i]=0;
for(j=0;j<num;j++){
if(j*j*j==num){
b[i]+=5;
break;
}
}
for(j=2;j<num;j++){
if(num%j==0 || num%2==0){
is_prime=0;
break;
}
}
if(is_prime!=0){
b[i]+=3;
}
if(num%4==0 && num%6==0){
b[i]+=4;
}
}
for(i=0;i<n;i++){
for(j=0;j<n-i-1;j++){
if(b[j]>b[j+1]){
swap=b[j+1];
b[j+1]=b[j];
b[j]=swap;
swap=a[j+1];
a[j+1]=a[j];
a[j]=swap;
}
}
}
for(i=0;i<n;i++){
printf("<%d,%d>",a[i],b[i]);
if(i!=(n-1)){
printf(",");
}
}
return 0;
}
OUTPUT :
Enter a number of elements :6
10 36 54 89 12 27
<10,0>,<54,0>,<89,3>,<36,4>,<12,4>,<27,5>
EXERCISE NO :11
FIND THE AVERAGE HEIGHTS USING ARRAY
PROGRAM :
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,n,sum=0,height[100],count=0;
float avg;
printf("Enter the number of persons :\n");
scanf("%d",&n);
printf("\nEnter the height of each person in centimeter :\n");
for(i=1;i<=n;i++){
scanf("%d",&height[i]);
sum=sum+height[i];
}
avg=(float)sum/n;
for(i=1;i<=n;i++)
if(height[i]>avg)
count++;
printf("\nAverage height of %d persons is :%.2f\n",n,avg);
printf("\nThe number of persons above average :%d",count);
}
OUTPUT :
Enter the number of persons :
5
Enter the height of each person in centimeter :
150
155
158
162
154
Average height of 5 persons is :
155.80
The number of persons above average :2
EXERCISE NO :12
SUM OF ARRAY OF NUMBERS
PROGRAM :
#include <stdio.h>
#include <conio.h>
int main()
{
int a[1000],i,n,sum=0;
printf("Enter size of an array :");
scanf("%d",&n);
printf("Enter elements in array :");
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
}for(i=1;i<=n;i++){
sum+=a[i];
}printf("sum of array is:%d",sum);
return 0;
}
OUTPUT :
Enter size of an array :5
Enter elements in array :2
3
7
9
6
Sum of array is :27
EXERCISE NO :13
TO FIND BODY MASS INDEX USING 2-D ARRAY
PROGRAM :
#include <stdio.h>
void BMI(int,float);
int main(void)
{
int w;
float h;
printf("Input the weight :");
scanf("%d",&w);
printf("Input the height :");
scanf("%f",&h);
BMI(w,h);
}
void BMI(int weight,float height){
float temp=weight/(height*height);
printf("BMI=%f\n",temp);
}
OUTPUT :
Input the weight :65
Input the height :5.6
BMI=2.072704
EXERCISE NO :14
MATRIX ADDITION
PROGRAM :
#include <stdio.h>
#include <conio.h>
void main()
{
int a[2][3],b[2][3],c[2][3];
printf("ENTER VALUES FOR MATRIX A:\n");
for(int i=1;i<=2;i++)
for(int j=1;j<=3;j++)
scanf("%d",&a[i][j]);
printf("\nENTER VALUES FOR MATRIX B:\n");
for(int i=1;i<=2;i++)
for(int j=1;j<=3;j++)
scanf("%d",&b[i][j]);
for(int i=1;i<=2;i++)
for(int j=1;j<=3;j++)
c[i][j]=a[i][j]+b[i][j];
printf("\nTHE VALUES OF MATRIX C ARE:\n");
for(int i=1;i<=2;i++)
{
for(int j=1;j<=3;j++)
printf("%d ",c[i][j]);
printf("\n");
}
OUTPUT :
ENTER VALUES FOR MATRIX A:
1 2 3
4 5 6
ENTER VALUES FOR MATRIX B:
1 3 4
2 5 1
THE VALUES OF MATRIX C ARE:
2 5 7
6 10 7
EERCISE NO :15
MATRIX MULTIPLICATION
PROGRAM :
#include <stdio.h>
#include <conio.h>
void main()
{
int a[2][3],b[2][3],c[2][3];
printf("ENTER VALUES FOR MATRIX A:\n");
for(int i=1;i<=2;i++)
for(int j=1;j<=3;j++)
scanf("%d",&a[i][j]);
printf("\nENTER VALUES FOR MATRIX B:\n");
for(int i=1;i<=2;i++)
for(int j=1;j<=3;j++)
scanf("%d",&b[i][j]);
for(int i=1;i<=2;i++)
for(int j=1;j<=3;j++)
c[i][j]=a[i][j]*b[i][j];
printf("\nTHE VALUES OF MATRIX C ARE:\n");
for(int i=1;i<=2;i++)
{
for(int j=1;j<=3;j++)
printf("%d ",c[i][j]);
printf("\n");
}
OUTPUT :
ENTER VALUES FOR MATRIX A:
1 2 3
4 5 6
ENTER VALUES FOR MATRIX B:
1 3 4
2 5 1
THE VALUES OF MATRIX C ARE:
1 6 12
8 25 6
EXERCISE NO :16
TRANSPOSE OF THE MATRIX
PROGRAM :
#include <stdio.h>
int main()
{
int a[10][10],transpose[10][10],r,c;
printf("Enter rows and columns :");
scanf("%d %d",&r,&c);
printf("\nEnter matrix elements :\n");
for(int i=1;i<=r;i++)
for(int j=1;j<=c;j++){
printf("Enter element a%d%d :",i,j);
scanf("%d",&a[i][j]);
}printf("\nEntered matrix :\n");
for(int i=1;i<=r;++i)
for(int j=1;j<=c;++j){
printf("%d ",a[i][j]);
if(j==c)
printf("\n");
}for(int i=1;i<=r;++i)
for(int j=1;j<=c;++j){
transpose[j][i]=a[i][j];
}printf("\nTranspose of the matrix :\n");
for(int i=1;i<=c;++i)
for(int j=1;j<=r;++j){
printf("%d ",transpose[i][j]);
if(j==r)
printf("\n");
}
return 0;
}
OUTPUT :
Enter rows and columns :2
2
Enter matrix elements :
Enter element a11 :1
Enter element a12 :2
Enter element a21 :3
Enter element a22 :4
Entered matrix :
1 2
3 4
Transpose of the matrix :
1 3
2 4
EXERCISE NO :17
REVERSE OF GIVEN STRING
PROGRAM :
#include <stdio.h>
#include <string.h>
char isalphabet(char x){
return((x>='A' && x<='Z')||(x>='a' && x<='z'));
}
char reverse(char str[]){
int len=strlen(str)-1,r=0;
char t;
while(r<len){
if(!isalphabet(str[r])){
r++;
}else if(!isalphabet(str[len])){
len--;
}else{
t=str[r];
str[r]=str[len];
str[len]=t;
r++;
len--;
}
}
}
int main()
{
char ch[1000];
printf("Enter a given string :");
scanf("%[^\n]*%c",ch);
reverse(ch);
printf("Reverse the given string :%s",ch);
}
OUTPUT :
Enter a given string :ur#her%op&
Reverse the given string :po#reh%ru&
EXERCISE NO :18
NUMBER CONVERSION USING FUNCTIIONS
PROGRAM :
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num,choice;
printf("Enter a number :");
scanf("%d",&num);
conversion(num, 2);
printf(" ");
conversion(num, 8);
printf(" ");
conversion(num, 16);
printf(" ");
return 0;
}void conversion(int num,int base){
int remainder=num%base;
if(num==0){
return 0;
}conversion(num/base,base);
if(remainder<10){
printf("%d",remainder);
}else{
printf("%d",remainder-10+'A');
}
}
OUTPUT:
Enter a number :10
1010 12 65
EXERCISE NO :19
USER DEFINED FUNCTIONS-TO PRINT FIBONACCI SERIES
PROGRAM :
#include<stdio.h>
void fibonacciSeries(int range)
{
int a=0, b=1, c;
while (a<=range)
{
printf("%d\t", a);
c = a+b;
a = b;
b = c;
}
}
int main()
{
int range;
fibonacciSeries(range);
return 0;
}
OUTPUT :
Enter range: 20
The fibonacci series is:
0 1 1 2 3 5 8 13
EXERCISE NO :20
STRING HANDLING FUNCTIONS
PROGRAM :
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int main()
{
char str[1000];
int choice,i,words_count=0;
printf("Enter the string :\n");
scanf("%[^\n]s",str);
printf("Enter a choice :");
scanf("%d",&choice);
if(choice==1){
for(i=0;str[i]!='\0';i++){
if(str[i]==' ' && str[i+1]!=' ')
++words_count;
}
printf("Total number of words are %d",words_count+1);
}
else if(choice==2){
for(i=0;str[i]!='\0';i++){
if(i==0){
if((str[i]>='a' && str[i]<='z'))
str[i]=str[i]-32;
continue;
}
if(str[i]==' '){
++i;
if(str[i]>='a' && str[i]<='z'){
str[i]=str[i]-32;
continue;
}
}
else{
if(str[i]>='A' && str[i]<='Z')
str[i]=str[i]+32;
}
}
printf("%s",str);
}
else if(choice==3){
char old_word[1000],new_word[1000],str2[1000]=" ",neww[1000][1000];
const char del[1000]=" ";
int i=0,len=0;
char *token;
scanf("%s %s",old_word,new_word);
token=strtok(str,del);
if(strcmp(token,old_word)==0)
strcpy(neww[i],new_word);
else
strcpy(neww[i],token);
i=0;
while(token!=NULL)
{
if(strcmp(token,old_word)==0)
strcpy(neww[i],new_word);
else
strcpy(neww[i],token);
token=strtok(NULL,del);
i++;
}
len=i;
strcpy(str2,neww[0]);
for(i=1;i<len;i++){
strcat(str2, " ");
strcat(str2,neww[i]);
}
printf("%s",str2);
}
}
OUTPUT :
Enter the string :
This is not work
Enter a choice :3
is will
This will not work
Enter the string :
Shreya teja is the school topper
Enter a choice :1
Total number of words are 6
EXERCISE NO :21
TOWERS OF HANOI-USING RECURSION
PROGRAM :
#include <stdio.h>
void hanoifun(int n, char fr, char tr, char ar)
{
if (n == 1)
{
printf("Move disk 1 from peg %c to peg %c", fr, tr);
printf("\n");
return;
}
hanoifun(n-1, fr, ar, tr);
printf("Move disk %d from peg %c to peg %c", n, fr, tr);
printf("\n");
hanoifun(n-1, ar, tr, fr);
}
int main()
{
int n;
printf("Enter a number :");
scanf("%d",&n);
hanoifun(n, 'A', 'C', 'B');
return 0;
}
OUTPUT :
Enter a number :3
Move disk 1 from peg A to peg C
Move disk 2 from peg A to peg B
Move disk 1 from peg C to peg B
Move disk 3 from peg A to peg C
Move disk 1 from peg B to peg A
Move disk 2 from peg B to peg C
Move disk 1 from peg A to peg C
EXERCISE NO :22
FACTORIAL USING RECURSION
PROGRAM :
#include <stdio.h>
int fact(int num);
int main()
{
int num;
printf("Enter a positive number to find its Factorial: ");
scanf("%d", &num);
printf("\nFactorial of %d is %d", num, fact(num));
return 0;
}
int fact(int num)
{
if(num)
return(num * fact(num - 1));
else
return 1;
}
OUTPUT :
Enter a positive number to find its Factorial: 6
Factorial of 6 is 720
EXERCISE NO :23
SORTING
PROGRAM :
#include<stdio.h>
int main()
{
int counter1,counter2,chk,temp_val,val[100];
printf("Please enter the total count of the elements that you want to sort: \n");
scanf("%d",&chk);
printf("Please input the elements that has to be sorted:\n");
for(counter1=0;counter1<chk;counter1++)
{
scanf("%d",&val[counter1]);
}
for(counter1=1;counter1<=chk-1;counter1++)
{
temp_val=val[counter1];
counter2=counter1-1;
while((temp_val<val[counter2])&&(counter2>=0))
{
val[counter2+1]=val[counter2];
counter2=counter2-1;
}
val[counter2+1]=temp_val;
}
printf("\nAfter sorting the numbers :\n");
for(counter1=0;counter1<chk;counter1++)
{
printf("%d ",val[counter1]);
}
return 0;
}
OUTPUT :
Please enter the total count o number that you want to sort:
4
Please input the elements that has to be sorted:
12
9
32
21
After sorting the numbers:
9 12 21 32
EXERCISE NO:24
PASS BY VALUE,PASS BY REFERENCE
PROGRAM :
#include <stdio.h>
void sort(int *arr,int num)
{
int i,j,temp;
for(i=1;i<=num;i++){
for(j=i;j<=num;j++){
if(arr[i]>arr[j]){
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
}
void main()
{
int n,i;
int list[20];
printf("Enter the no of elements :");
scanf("%d",&n);
printf("\nEnter the %d number of elements :\n",n);
for(i=1;i<=n;i++){
scanf("%d",&list[i]);
}sort(list,n);
printf("\nsorted array :\n");
for(i=1;i<=n;i++){
printf("%d\n",list[i]);
}
}
OUTPUT :
Enter the no of elements :5
Enter the 5 number of elements :
10
37
9
23
64
Sorted array :
9
10
23
37
64
EXERCISE NO :25
EMPLOYEE SALARY SLIP-STRUCTURES AND POINTERS
PROGRAM :
#include<stdio.h>
struct emp
{
int empno;
char name[1000];
int bpay,allow,ded,npay;
}e[1000];
void main()
{
int i,n;
printf("Enter the numbers :");
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&e[i].empno);
scanf("%s",e[i].name);
scanf("%d %d %d",&e[i].bpay,&e[i].allow,&e[i].ded);
e[i].npay=e[i].bpay + e[i].allow -e[i].ded;
}
printf("Code Name Basic Pay Allowance Deduction Net Pay\n");
for(i=0 ;i<n ;i++)
{
printf("%d %s %d %d %d
%d\n",e[i].empno,e[i].name,e[i].bpay,e[i].allow,e[i].ded,e[i].npay);
}
}
OUTPUT :
Enter the numbers :2
45 ABC 4000000 300000 8000
23 XYZ 3000000 100000 3000
Code Name Basic Pay Allowance Deduction Net Pay
45 ABC 4000000 300000 8000 4292000
23 XYZ 3000000 100000 3000 3097000
EXERCISE NO:26
INTERNAL MARK CALCULATION-STRUCTURES AND FUNCTIONS
PROGRAM :
#include<stdio.h>
struct avg
{
int rollno;
char name[1000];
int a,b,c,d,e;
} s[1000];
void main()
{
int i,n;
printf("Enetr the numbers :");
scanf("%d",&n);
printf("Name Roll Subj1 Subj2 Subj3 Subj4 Subj5\n");
for(i=0;i<n;i++){
scanf("%s",s[i].name);
scanf("%d",&s[i].rollno);
scanf("%d %d %d %d %d",&s[i].a,&s[i].b,&s[i].c,&s[i].d,&s[i].e);
}
printf("Name Rollno Average\n");
for(i=0 ;i<n ;i++)
{
printf("%s %d %.2f\n",s[i].name,s[i].rollno,(s[i].a+s[i].b+s[i].c+s[i].d+s[i].e)/5.0);
}
}
OUTPUT :
Enter the numbers :2
Name Roll Subj1 Subj2 Subj3 Subj4 Subj5
XYZ 54 67 82 56 78 99
ABC 12 72 87 49 87 89
Name Rollno Average
XYZ 54 76.40
ABC 12 76.80
EXERCISE NO:27
RANDOM ACCESS FILE
PROGRAM :
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Phonebook_Contacts
{
char FirstName[20];
char LastName[20];
char PhoneNumber[20];
} phone;
void AddEntry(phone * );
void DeleteEntry(phone * );
void PrintEntry(phone * );
void SearchForNumber(phone * );
int counter = 0;
char FileName[256];
FILE *pRead;
FILE *pWrite;
int main (void)
{
phone *phonebook;
phonebook = (phone*)
malloc(sizeof(phone)*100);
int iSelection = 0;
if (phonebook == NULL) {
printf("Out of Memory. The program will now exit");
return 1;
} else {}
do {
printf("\n\t\t\tPhonebook Menu");
printf("\n\n\t(1)\tAdd Friend");
printf("\n\t(2)\tDelete Friend");
printf("\n\t(3)\tDisplay Phonebook Entries");
printf("\n\t(4)\tSearch for Phone Number");
printf("\n\t(5)\tExit Phonebook");
printf("\n\nWhat would you like to do? ");
scanf("%d", &iSelection);
if (iSelection == 1) {
AddEntry(phonebook);
} if (iSelection == 2) {
DeleteEntry(phonebook);
} if (iSelection == 3) {
PrintEntry(phonebook);
} if (iSelection == 4) {
SearchForNumber(phonebook);
} if (iSelection == 5) {
printf("\nYou have chosen to exit the Phonebook.\n");
return 0;
}
} while (iSelection <= 4);
} void AddEntry (phone * phonebook)
{
pWrite = fopen("phonebook_contacts.dat", "a");
if ( pWrite == NULL ) {
perror("The following error occurred ");
exit(EXIT_FAILURE);
} else {
counter++;
realloc(phonebook, sizeof(phone));
printf("\nFirst Name: ");
scanf("%s", phonebook[counter-1].FirstName);
printf("Last Name: ");
scanf("%s", phonebook[counter-1].LastName);
printf("Phone Number (XXX-XXX-XXXX): ");
scanf("%s", phonebook[counter-1].PhoneNumber);
printf("\n\tFriend successfully added to Phonebook\n");
fprintf(pWrite, "%s\t%s\t%s\n", phonebook[counter-1].FirstName, phonebook[counter-
1].LastName, phonebook[counter-1].PhoneNumber);
fclose(pWrite);
}
} void DeleteEntry (phone * phonebook) {
int x = 0;
int i = 0;
char deleteFirstName[20];
char deleteLastName[20];
printf("\nFirst name: ");
scanf("%s", deleteFirstName);
printf("Last name: ");
scanf("%s", deleteLastName);
for (x = 0; x < counter; x++) {
if (strcmp(deleteFirstName, phonebook[x].FirstName) == 0) {
if (strcmp(deleteLastName, phonebook[x].LastName) == 0) {
for ( i = x; i < counter - 1; i++ ) {
strcpy(phonebook[i].FirstName, phonebook[i+1].FirstName);
strcpy(phonebook[i].LastName, phonebook[i+1].LastName);
strcpy(phonebook[i].PhoneNumber, phonebook[i+1].PhoneNumber);
} printf("Record deleted from the phonebook.\n\n");
--counter;
return;
}
}
} printf("That contact was not found, please try again.");
} void PrintEntry (phone * phonebook) {
int x = 0;
printf("\nPhonebook Entries:\n\n ");
pRead = fopen("phonebook_contacts.dat", "r");
if ( pRead == NULL) {
perror("The following error occurred: ");
exit(EXIT_FAILURE);
} else {
for( x = 0; x < counter; x++) {
printf("\n(%d)\n", x+1);
printf("Name: %s %s\n", phonebook[x].FirstName, phonebook[x].LastName);
printf("Number: %s\n", phonebook[x].PhoneNumber);
}
} fclose(pRead);
} void SearchForNumber (phone * phonebook) {
int x = 0;
char TempFirstName[20];
char TempLastName[20];
printf("\nPlease type the name of the friend you wish to find a number for.");
printf("\n\nFirst Name: ");
scanf("%s", TempFirstName);
printf("Last Name: ");
scanf("%s", TempLastName);
for (x = 0; x < counter; x++) {
if (strcmp(TempFirstName, phonebook[x].FirstName) == 0) {
if (strcmp(TempLastName, phonebook[x].LastName) == 0) {
printf("\n%s %s's phone number is %s\n", phonebook[x].FirstName,
phonebook[x].LastName, phonebook[x].PhoneNumber);
}
}
}
}
OUTPUT :
Phonebook Menu
(1) Add Friend
(2) Delete Friend
(3) Display Phonebook Entries
(4) Search for Phone Number
(5) Exit Phonebook
What would you like to do? 1
First Name: Ram
Last Name: Mohan
Phone Number (XXX-XXX-XXXX): 717-675-0909
Friend successfully added to Phonebook
Phonebook Menu
(1) Add Friend
(2) Delete Friend
(3) Display Phonebook Entries
(4) Search for Phone Number
(5) Exit Phonebook
What would you like to do? 4
Please type the name of the friend you wish to find a number for.
First Name: Ram
Last Name: Mohan
Ram Mohan’s phone number is 717-675-0909
Phonebook Menu
(1) Add Friend
(2) Delete Friend
(3) Display Phonebook Entries
(4) Search for Phone Number
(5) Exit Phonebook
What would you like to do? 5
You have chosen to exit the Phonebook.
EXERCISE NO :28
SEQUENTIAL ACCESS FILE
PROGRAM :
#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;
main()
{
long int pos1,pos2,pos;
FILE *fp;
char *ano,*amt;
char choice;
int type,flag=0;
float bal;
do
{
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 Minimum Balance\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
Enter your choice : 1
Enter the Account Number : 547898760
Enter the Account Holder Name : Rajan
Enter the Initial Amount to deposit : 2000
Press any key to continue....
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
Enter your choice : 4
The Number of Account Holder whose Balance less than the Minimum Balance : 0
EXERCISE NO :29
RAILWAY RESERVATION SYSTEM
PROGRAM :
#include<stdio.h>
#include<conio.h>
int first=5,second=5,thired=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 cheking");
printf("\n1.first class\n2.second class\n3.thired 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(thired>0) {
printf("seat available\n");
thired--;
} 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:
thired++;
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);
}
}int main() {
int n;
printf("welcome to railway ticket reservation\n");
while(1) {
printf("1.booking\n2.availability checking\n3.cancel\n4.Chart \n5. Exit\nenter 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!");
exit(0);
default:
break;
}
}
}
OUTPUT :
welcome to railway ticket reservation
1.booking
2.availability checking
3.cancel
4.Chart
5. Exit enter your option: 2
availability checking
1.first class
2.second class
3.thired class
enter the option 1
seat available
1.booking
2.availability checking
3.cancel
4.Chart
5. Exit
enter your option: 5
Thank you visit again!