You are on page 1of 58

Arithmetic Operations

Program:

#include<stdio.h>
void main()
{
inta,b,sum,sub,mul,div;
printf("\nEnter the values");
scanf("%d%d",&a,&b);
sum=a+b;
sub=a-b;
mul=a*b;
div=a/b;
printf("\n%d+%d=%d",a,b,sum);
printf("\n%d-%d=%d",a,b,sub);
printf("\n%d*%d=%d",a,b,mul);
printf("\n%d/%d=%d",a,b,div);
}
Output:
Enter the values 8 4
8+4=12
8-4=4
8*4=32
8/4=2
Area And Circumference Of The Circle

Program:

#include<stdio.h>
void main()
{

int r;
float PI = 3.14, area, ci;
printf("\nEnter radius of circle: ");
scanf("%d", &r);
area = PI * r * r;
printf("\nArea of circle : %f ", area);
ci = 2 * PI * r;
printf("\nCircumference : %f ", ci);
}
Output:

Enter radius of circle: 1


Area of circle : 3.140000
Circumference : 6.280000
Largest of three numbers

Program:

#include <stdio.h>
void main()
{
int n1, n2, n3;
printf("Enter three different numbers: ");
scanf("%d %d %d", &n1, &n2, &n3);
if( n1>=n2 && n1>=n3 )
printf("%d is the largest number.", n1);
else if( n2>=n3 )
printf("%d is the largest number.", n2);
else
printf("%d is the largest number.", n3);

}
Output:

Enter three different numbers: 5 8 1

8 is the largest number.


Factorial of a given number

Program:

#include <stdio.h>
void main()
{
int n, i, fact=1;
printf("Enter an integer: ");
scanf("%d",&n);
for(i=1; i<=n; ++i)
{
fact *= i; // fact = fact*i;
}
printf("Factorial of %d = %d", n, fact);
}
Output:

Enter an integer: 5
Factorial of 5 = 120
Fibonacci Series

Program:

#include <stdio.h>
void main()
{
int i, n, f1 = -1, f2 = 1, f3;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (i = 1; i <= n; ++i)
{
f3 = f1 + f2;
printf("\t%d",f3);
f1 = f2;
f2 = f3;
}

}
Output:

Enter the number of terms: 8


Fibonacci Series: 0 1 1 2 3 5 8 13
Leap year checking

Program:

#include <stdio.h>
void main()
{
int year;
printf("Enter a year to check if it is a leap year\n");
scanf("%d", &year);

if ( year%400 == 0) // Exactly divisible by 400 e.g. 1600, 2000


printf("%d is a leap year.\n", year);

else if ( year%100 == 0) // Exactly divisible by 100 and not by 400 e.g. 1900, 2100
printf("%d is not a leap year.\n", year);
else if ( year%4 == 0 ) // Exactly divisible by 4 and neither by 100 nor 400 e.g. 2016, 2020
printf("%d is a leap year.\n", year);
else // Not divisible by 4 or 100 or 400 e.g. 2017, 2018, 2019
printf("%d is not a leap year.\n", year);
}
Output:

Enter a year to check if it is a leap year 2012

2012 is a leap year.


Design a calculator to perform the operations, namely, addition, subtraction,
multiplication, division and square of a number.

Program:

# include <stdio.h>
void main()
{
char operator;
double firstNumber,secondNumber;
printf("Enter an operator (+, -, *,/,^): ");
scanf("%c", &operator);
if(operator!='^')
{
printf("Enter two operands: ");
scanf("%lf %lf",&firstNumber, &secondNumber);
}
else
{
printf("Enter the operand: ");
scanf("%lf",&firstNumber);
}
switch(operator)
{
case '+':
printf("%.1lf + %.1lf = %.1lf",firstNumber, secondNumber,
firstNumber + secondNumber);
break;

case '-':
printf("%.1lf - %.1lf = %.1lf",firstNumber, secondNumber,
firstNumber - secondNumber);
break;

case '*':
printf("%.1lf * %.1lf = %.1lf",firstNumber, secondNumber,
firstNumber * secondNumber);
break;

case '/':
printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber,
firstNumber / secondNumber);
break;
case '^':
printf("Square(%.1lf) = %.1lf",firstNumber, firstNumber *
firstNumber);
break;
// operator doesn't match any case constant (+, -, *, /,^)
default:
printf("Error! operator is not correct");
}

}
Output:

Enter an operator (+, -, *,/,^): *


Enter two operands: 8 9
8.0 * 9.0 = 72.0
Armstrong number

Program:

#include <stdio.h>
void main()
{
int number, originalNumber, remainder, result = 0;
printf("Enter a three digit integer: ");
scanf("%d", &number);
originalNumber = number;
while (originalNumber != 0)
{
remainder = originalNumber%10;
result += remainder*remainder*remainder;
originalNumber /= 10;
}
if(result == number)
printf("%d is an Armstrong number.",number);
else
printf("%d is not an Armstrong number.",number);

}
Output:

Enter a three digit integer: 371


371 is an Armstrong number.
Sort the numbers based on the weight

Program:

#include <stdio.h>
#include<conio.h>
#include <math.h>
void main()
{
intnArray[50],wArray[50],nelem,sq,i,j,t;
clrscr();
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]);
// Sorting an array
for(i=0;i<nelem;i++)
for(j=i+1;j<nelem;j++)
if(nArray[i] >nArray[j])
{
t = nArray[i];
nArray[i] = nArray[j];
nArray[j] = t;
}
//Calculate the weight
for(i=0; i<nelem; i++)
{
wArray[i] = 0;
// sq =(int) sqrt(nArray[i]);
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++)
printf("<%d,%d>", nArray[i],wArray[i]);
getch();

int prime(intnum)
{
int flag=1,i;
for(i=2;i<=num/2;i++)
if(num%i==0)
{
flag=0;
break;
}
return flag;
}
intpercube(intnum)
{
inti,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 : 6

Enter 6 elements
10
36
54
89
12
27
<10,0><12,4><27,5><36,4><54,0><89,3>
Average height of persons

Program:

#include <stdio.h>
void main()
{
float height[10];
inti,aavgh=0,n;
printf("Enter the number of persons:");
scanf("%d", &n);
printf("Enter the Height (in centimetres) \n");
for(i=0;i<n;i++)
scanf("%f", &height[i]);
for(i=0;i<n;i++)
if (height[i] > 165.0)
aavgh=aavgh+1;
printf("Number of persons above the average height is %d",aavgh);
}
Output:

Enter the number of persons: 5


Enter the Height (in centimetres) 132 167.2 162.3 180 155.4
Number of persons above the average height is 2
Body Mass Index of the individuals

Program:

#include <stdio.h>
void main()
{
float height[10],weight[10],bmi[10];
inti,n;
printf("Enter the number of persons:");
scanf("%d", &n);
printf("Enter the Height (in metres) \n");
for(i=0;i<n;i++)
scanf("%f", &height[i]);
printf("Enter the Weight (in kg) \n");
for(i=0;i<n;i++)
scanf("%f", &weight[i]);
for(i=0;i<n;i++)
bmi[i]=weight[i]/(height[i]*height[i]);
for(i=0;i<n;i++)
printf("\n bmi[%d] = %f",i,bmi[i]);
}
Output:

Enter the number of persons: 3


Enter the Height (in metres) 1.79 1.5 1.64
Enter the Weight (in kg) 70 65 60
bmi[0] = 21.847010
bmi[1] = 28.888889
bmi[0] = 22.308151
Reverse of a given string

Program:

#include<stdio.h>
void reverseString(char s[])
{
// Initialize l and r
int r = strlen(s) - 1, l = 0;
char c;
//Till l meets r, travers from both ends
while (l < r)
{
// ignore special characters
if (!isalpha(s[l]))
l++;
else if(!isalpha(s[r]))
r--;
else // if both s[l] and s[r] are alphabatical characters
{
c=s[l];
s[l]=s[r];
s[r]=c;
l++;
r--;
}
}
}

void main()
{
char s[] = "a!@bc&d";
printf("\n Input String:");
scanf("%s",&s);
reverseString(s);
printf("\n Output String:");
printf("%s",s);
}
Output:

Input String:ab%cd*e
Output String:ed%cb*a
Decimal number into binary

Program:

#include <stdio.h>
int convert(int);
void main()
{
intdec, bin;
printf("Enter a decimal number: ");
scanf("%d", &dec);
bin = convert(dec);
printf("The binary equivalent of %d is %d.\n", dec, bin);
}
int convert(intdec)
{
if (dec == 0)
{
return 0;
}
else
{
return (dec % 2 + 10 * convert(dec / 2));
}
}
Output:

Enter a decimal number: 13


The binary equivalent of 13 is 1101.
Decimal number into octal

Program:

#include <stdio.h>
#include <math.h>
intconvertDecimalToOctal(intdecimalNumber);
void main()
{
intdecimalNumber;
printf("Enter a decimal number: ");
scanf("%d", &decimalNumber);
printf("%d in decimal = %d in octal", decimalNumber,
convertDecimalToOctal(decimalNumber));
}
intconvertDecimalToOctal(intdecimalNumber)
{
intoctalNumber = 0, i = 1;
while (decimalNumber != 0)
{
octalNumber += (decimalNumber % 8) * i;
decimalNumber /= 8;
i *= 10;
}
return octalNumber;
}
Output:

Enter a decimal number: 1245


1245 in decimal = 2335 in octal
Decimal number to hexadecimal.

Program:

#include<stdio.h>
void main()
{
long intdecimalNumber;
void convertDecimalToHexa(intdecimalNumber);
printf("Enter any decimal number: ");
scanf("%ld",&decimalNumber);
convertDecimalToHexa(decimalNumber);
}
void convertDecimalToHexa(intdecimalNumber)
{
int i=1,j,temp;
char hexadecimalNumber[100];
long int quotient = decimalNumber;
while(quotient!=0) {
temp = quotient % 16;
//To convert integer into character
if( temp < 10)
temp =temp + 48; else
temp = temp + 55;
hexadecimalNumber[i++]= temp;
quotient = quotient / 16;
}
printf("Equivalent hexadecimal value of decimal number %d: ",decimalNumber);
for (j = i -1 ;j> 0;j--)
printf("%c",hexadecimalNumber[j]);

}
Output:

Enter a decimal number: 1245

Equivalent hexadecimal value of decimal number 1245: 4DD


String operations

Program:

#include <stdio.h>
#include<string.h>
#define MAX 100

void main()
{
char str[MAX]={0},text[10][10];
int i, count=0,k,w,j,p;
char word[MAX],rword[MAX];
printf("Enter a string: ");
scanf("%[^\n]s",str); //read string with spaces

//a. Count the number of words


for(i=0;str[i]!='\0';i++)
if(str[i]==' ')
count++;
printf("Number of words in given string is: %d\n", count+1);

//b. Capitalize each word


for(i=0; str[i]!=strlen(str); i++)
{
if(i==0)
str[i]=toupper(str[i]);
if(str[i]==' ')
{
++i;
str[i]=toupper(str[i]);
}
}
printf("Capitalize string is: %s\n",str);

//c. Replace the given word with another word


i=0,j=0;
printf("\nEnter the word which is to be replaced");
scanf("%s",word);
printf("\nEnter by which word the %s is to be replaced",word);
scanf("%s",rword);
p=strlen(str);
for(k=0;k<p;k++)
{
if(str[k]!=' ')
{
text[i][j]=str[k];
j++;
}
else
{
text[i][j]='\0';
j=0;i++;
}
}
text[i][j]='\0';
w=i;
for(i=0;i<=w;i++)
{
if(strcmp(text[i],word)==0)
strcpy(text[i],rword);
printf("%s",text[i]);
}
getch();
}
Output:

Enter a string: welcome to sec


Number of words in the given string is: 3
Capitalize string is: Welcome To Sec
Enter the word which is to be replaced Sec
Enter by which word the %s is to be replaced Sudharsan
Welcome To Sudharsan
Towers of Hanoi using recursion.

Program:

#include <stdio.h>
void towers(int, char, char, char);
void main()
{
intnum;
printf("Enter the number of disks : ");
scanf("%d", &num);
printf("The sequence of moves involved in the Tower of Hanoi are :\n");
towers(num, 'A', 'C', 'B');
}
void towers(intnum, char frompeg, char topeg, char auxpeg)
{
if (num == 1)
{
printf("\n Move disk 1 from peg %c to peg %c", frompeg, topeg);
return;
}
towers(num - 1, frompeg, auxpeg, topeg);
printf("\n Move disk %d from peg %c to peg %c", num, frompeg, topeg);
towers(num - 1, auxpeg, topeg, frompeg);
}
Output:

Move disk 1 frompeg A to peg C


Move disk 2 frompeg A to peg B
Move disk 1 frompeg C to peg B
Move disk 3 frompeg A to peg C
Move disk 1 frompeg B to peg A
Move disk 2 frompeg B to peg C
Move disk 1 frompeg A to peg C
Sorting using pass by reference

Program:

#include <stdio.h>
void main()
{

int i, j, a, n, number[30];
printf("Enter the value of N: \n");
scanf("%d", &n);
printf("Enter the numbers \n");
for (i = 0; i < n; ++i)
scanf("%d", &number[i]);
sort(n,number);
printf("The numbers arranged in ascending order are given below \n");
for (i = 0; i < n; ++i)
printf("%d\t", number[i]);
}
void sort(int n, intnum[])
{
inti,j,a;
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (num[i] >num[j])
{

a = num[i];
num[i] = num[j];
num[j] = a;

}
Output:

Enter the value of N: 5


Enter the numbers 34 51 21 76 11
The numbers arranged in ascending order are given below

11 21 34 51 76
Salary slip of employees

Program:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* structure to store employee salary details */


struct employee {
intempId;
char name[32];
int basic, hra, da, ma;
intpf, insurance;
float gross, net;
};

/* prints payslip for the requested employee */


void printSalary(struct employee e1)
{
printf("Salary Slip of %s:\n", e1.name);
printf("Employee ID: %d\n", e1.empId);
printf("Basic Salary: %d\n", e1.basic);
printf("House Rent Allowance: %d%\n", e1.hra);
printf("Dearness Allowance: %d%\n", e1.da);
printf("Medical Allowance: %d%\n", e1.ma);
printf("Gross Salary: %.2f Rupees\n", e1.gross);
printf("\nDeductions: \n");
printf("Provident fund: %d\n", e1.pf);
printf("Insurance: %d\n", e1.insurance);
printf("\nNet Salary: %.2f Rupees\n\n", e1.net);
return;
}

int main()
{
int i, ch, num, flag, empID;
struct employee *e1;\
printf("Enter the number of employees:");
scanf("%d", &num);
e1 = (struct employee *)malloc(sizeof(struct employee) * num);
printf("Enter your input for every employee:\n");
for (i = 0; i <num; i++)
{
printf("Employee ID:");
scanf("%d", &(e1[i].empId));
getchar();
printf("Employee Name:");
fgets(e1[i].name, 32, stdin);
e1[i].name[strlen(e1[i].name) - 1] = '\0';
printf("Basic Salary, HRA:");
scanf("%d%d", &(e1[i].basic), &(e1[i].hra));
printf("DA, Medical Allowance:");
scanf("%d%d", &(e1[i].da), &(e1[i].ma));
printf("PF and Insurance:");
scanf("%d%d", &(e1[i].pf), &(e1[i].insurance));
printf("\n");
}
for (i = 0; i <num; i++)
{
e1[i].gross = e1[i].basic + (e1[i].hra * e1[i].basic) / 100 + (e1[i].da * e1[i].basic) /
100 + (e1[i].ma * e1[i].basic) / 100;
e1[i].net = e1[i].gross - (e1[i].pf + e1[i].insurance);
}

while (1)
{
printf("Enter employee ID to get payslip:");
scanf("%d", &empID);
flag = 0;
for (i = 0; i <num; i++) {
if (empID == e1[i].empId)
{
printSalary(e1[i]);
flag = 1;
}
}

if (!flag)
{
printf("No Record Found!!\n");
}

printf("Do You Want To Continue(1/0):");


scanf("%d", &ch);
if (!ch)
{
break;
}
}
return 0;
}
Output:

Enter the number of employees:2

Employee ID: 123


Employee Name: Ramu
Basic Salary, HRA: 10000 10
DA, Medical Allowance:7 5
PF and Insurance: 600 300

Employee ID: 124


Employee Name: Ramya
Basic Salary, HRA: 8000 10
DA, Medical Allowance:7 5
PF and Insurance: 600 200

Enter employee ID to get payslip: 123

Salary Slip of : Ramu


Employee ID: 123
Basic Salary: 10000
House Rent Allowance: 10%
Dearness Allowance: 7%
Medical Allowance: 5%d
Gross Salary: 9579.00 Rupees
Deductions:
Provident fund: 600
Insurance: 300
Net Salary: 8679.00 Rupees
Internal marks of students

Program:

#include<stdio.h>
#include<conio.h>
structmark_sheet
{
char name[20];
introllno;
int sub[5][3];
int total;
float avg[5];
int internal[5];
}students[25];
int main()
{
inta,b,n,i;
char ch;
void display(structmark_sheet students[],int);
clrscr();
printf("How many students : \n");
scanf("%d",&n);
for(a=0;a<n;++a)
{
clrscr();
printf("\n\nEnter the details of %d students : ", a+1);
printf("\n\nEnter student %d Name : ", a+1);
scanf("%s", students[a].name);
printf("\n\nEnter student %d Roll Number : ", a+1);
scanf("%d", &students[a].rollno);
for(b=0;b<5;++b)
{
students[a].total=0;
students[a].avg[b]=0.0;
students[a].internal[b]=0;
for(i=0;i<3;i++)
{
printf("\n\nEnter the test %d mark of subject-%d : ", i+1,b+1);
scanf("%d", &students[a].sub[b][i]);
students[a].total += students[a].sub[b][i];
}
students[a].avg[b] = students[a].total/3.0;
students[a].internal[b]=students[a].avg[b]/5;
}
}
display(students,n);
}
void display(structmark_sheet students[], int n)
{
inta,b,i;
char ch;
for(a=0;a<n;++a)
{
clrscr();
printf("\n\n\t\t\t\tMark Sheet\n");
printf("\nName of Student : %s", students[a].name);
printf("\t\t\t\t Roll No : %d", students[a].rollno);
printf("\n------------------------------------------------------------------------");
printf("\n\t Subject \t Test1 \t Test2 \t Test3 \t Test Avg \t Internal");
for(b=0;b<5;b++)
{
printf("\n\t Subject%d",b+1);
for(i=0;i<3;i++)
printf("\t %d ",students[a].sub[b][i]);
printf("\t %3.2f",students[a].avg[b]);
printf("\t\t %d ",students[a].internal[b]);
}
printf("\n\n------------------------------------------------------------------------\n");
printf("\n\n\n\t\t\t\t Press Y for continue . . . ");
ch = getche();
if((ch=="y")||(ch=="Y"))
continue;
}
}
Output:

How many students : 2

Enter the details of 1 students :


Enter student 1 Name : Rani
Enter student 1 Roll Number : 12

Enter the test 1 mark of subject-1 : 84


Enter the test 2 mark of subject-1 : 77
Enter the test 3 mark of subject-1 : 63
Enter the test 1 mark of subject-2 : 55
Enter the test 2 mark of subject-2 : 64
Enter the test 3 mark of subject-2 : 71
Enter the test 1 mark of subject-3 : 88
Enter the test 2 mark of subject-3 : 83
Enter the test 3 mark of subject-3 : 81
Enter the test 1 mark of subject-4 : 79
Enter the test 2 mark of subject-4 : 59
Enter the test 3 mark of subject-4 : 75
Enter the test 1 mark of subject-5 : 89
Enter the test 2 mark of subject-5 : 75
Enter the test 3 mark of subject-5 : 83

Enter the details of 1 students :


Enter student 1 Name : Selvi
Enter student 1 Roll Number : 13

Enter the test 1 mark of subject-1 : 65


Enter the test 2 mark of subject-1 : 74
Enter the test 3 mark of subject-1 : 81
Enter the test 1 mark of subject-2 : 64
Enter the test 2 mark of subject-2 : 71
Enter the test 3 mark of subject-2 : 63
Enter the test 1 mark of subject-3 : 78
Enter the test 2 mark of subject-3 : 64
Enter the test 3 mark of subject-3 : 57
Enter the test 1 mark of subject-4 : 89
Enter the test 2 mark of subject-4 : 68
Enter the test 3 mark of subject-4 : 77
Enter the test 1 mark of subject-5 : 64
Enter the test 2 mark of subject-5 : 75
Enter the test 3 mark of subject-5 : 86
Telephone directory

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? 5


You have chosen to exit the Phonebook.
Banking Application

Program:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#define MINBAL 500
structBank_Account
{
char no[10];
char name[20];
char balance[15];
};
structBank_Accountacc;
void main()
{
long int pos1,pos2,pos;
FILE *fp;
char *ano,*amt;
char choice;
inttype,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
Minimum Balance\n");
printf("5. Delete All\n");
printf("6. 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' :
remove("acc.dat");
break;
case '6' :
fclose(fp);
exit(0);
}
printf("\nPress any key to continue....");
getch();
} while (choice!='6');
}
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. Delete All
6. Stop

Enter your choice : 1


Enter the Account Number : 12345
Enter the Account Holder Name : Kalai
Enter the Initial Amount to deposit : 2000
Press any key to continue....

Enter your choice : 1


Enter the Account Number : 12346
Enter the Account Holder Name : Lilly
Enter the Initial Amount to deposit : 5000
Press any key to continue....

Enter your choice : 2


A/c Number A/c Holder Name Balance
12345 Kalai 2000
12346 Lilly 5000
Press any key to continue....

Enter your choice : 3


Enter the Type 1 for deposit & 2 for withdraw : 2
Your Current Balance is : 2000
Enter the Amount to transact : 1600
Press any key to continue....

Enter your choice : 4

The Number of Account Holder whose Balance less than the Minimum
Balance : 1
Implementation of 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(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.third 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 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!”);
getch();
exit(0);
default:
break;
}
}
getch();
}
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.third 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!

You might also like