Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

cs3271 C Programming Labaratory

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 38

EX.

NO:1a PROGRAM USING I/O STATEMENTS AND EXPRESSIONS


DATE:
(a) For a given product with a VAT OF 7%,find the VAT amount and the total value of
the product.

AIM:
To find the VAT amount and the total value of the product.

ALGORITHM:
1.Start the program.
2.Read the item Details.
3.Read discount and tax % details.
4.Calculate amountqty x value.
5.Calculate discount(amount x discount)/100.
6.Calculate taxamt(subtot x tax)100
7.Calculate totamt(subtot + taxamt)
8.Print all Details
9.Stop the Program

PROGRAM:
#include<stdio.h>
#include<conio.h>
main()
{
float totamt, amount,subtot,disamt,taxamt,quantity,value,discount,tax;
clrscr();
printf("\n Enter the quantity of item sold:");
scanf("%f",&quantity);
printf("\n Enter the value of item:");
scanf("%f",&value);
printf("\n Enter the Discount percentage:");
scanf("%f",&discount);
printf("\n Enter the tax:");
scanf("%f",&tax);
amount=quantity * value;
disamt=(amount*discount)/100.0;
subtot=amount-disamt;
taxamt=(subtot*tax)/100.0;
totamt=subtot+taxamt;
printf("\n\n\n ******BILL****** ");
printf("\nQuantitysold: %f", quantity);
printf("\npriceperitem: %f", value);
printf("\nAmount: %f", amount);
printf(" \n Discount: - %f", disamt) ;
printf("\n Discounted Total: %f", subtot) ;
printf("\n Tax:=+ %f", taxamt);
printf("\n Total Amount %f", totamt);
getch();
}

RESULT:
Thus the program for a given product with a VAT OF 7% to find the VAT Amount and
the total value of the product was executed and the output was verified.
EX.NO:1b PROGRAM USING I/O STATEMENTS AND EXPRESSIONS
DATE:
(b) Write a C Program to find area and perimeter of Circle.

AIM:
To write a C Program to find area and perimeter of Circle.

ALGORITHM:
1.Start the program.
2.Read radius.
3.AreaP1 * Radius * Radius , PI=3.14
4.Perm2 * PI * Radius
5.Print area Prem
6.Stop.

PROGRAM:
#include<stdio.h>
#include<conio.h>
#define pi 3.14
void main()
{
float r,area,perimeter;
clrscr();
printf("\n Enter r value:");
scanf("%f",&r);
area=pi*r*r;
printf(" area%f:",area);
perimeter=2*pi*r;
printf("perimeter%f:",perimeter);
getch();
}

RESULT:
Thus the program to find area and perimeter of Circle was executed and the output was
verified.
EX.NO:2a PROGRAMS USING DECISION-MAKING CONSTRUCTS
DATE:
(a) Write a C Program to Find the Eligibility of a Person to Vote or Not?

AIM:
To Write a C Program to find the eligibility of a Person to Vote or not?

ALGORITHM:
1.Start the program.
2.Read name, age.
3.IF age>=18 print “Eligible”.
4.ELSE print “Not Eligible”.
5.ENDIF.
6.Stop the Program.

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int age;
char name[50]; // or *name
clrscr();
printf("\n Type the Name of the candidate: ");
gets(name);
printf("\n Enter The Age : ");
scanf("%d",&age);
if(age>=18)
printf("\n %s is Eligible for Vote",name);
else
printf("\n %s is Not Eligible for Vote",name);
getch();
}

RESULT:
Thus the program to find the eligibility of a Person to Vote or not was executed and the
output was verified.
EX.NO:2b PROGRAMS USING DECISION-MAKING CONSTRUCTS
DATE:
(b) Write a C Program to find the Largest Numbers Among Three Numbers.

AIM:
To Write a C Program to find the Largest Numbers among Three Numbers.

ALGORITHM:

1.Start the program.


2.Read a, b ,c.
3.IF(a>b and a>c) big = a.
4.ELSEIF (b>a and b>c) big =b.
5.ELSE big = c.
5. ENDIF.
6. print big.
6.Stop the Program.

PROGRAM:

#include <stdio.h>
#include<conio.h>
int main()
{
int a,b,c;
int big;
clrscr();
printf("Enter three numbers:");
scanf("%d%d%d",&a,&b,&c);
if(a>b && a>c)
big=a;
else if(b>a && b>c)
big=b;
else
big=c;
printf("Largest number is = %d",big);
return 0;
getch();
}

RESULT:
Thus the program to find the Largest Numbers among Three Numbers was executed and
the output was verified.
EX.NO:2 C WRITE A PROGRAM TO FIND WHETHER THE GIVEN YEAR IS LEAP YEAR
OR NOT.
DATE:

AIM:
To Write a program to find whether the given year is leap year or not.

ALGORITHM:
1.Start the program.
2.Read Year.
3. IF Year%4=0.
Step-3.1 IF Year%100=0.
Step-3.1.1 IF Year%400=0.
Step-3.1.2 Print “Leap Year”.
Step-3.1.3 ELSE Print “Not Leap Year.
Step-3.2 ELSE Print “Leap Year”.
4. Print “Not Leap
Year”. 5.Stop.

PROGRAM:
#include <stdio.h>
#include<conio.h>
void main()
{
int year;
clrscr();
printf("Enter a year: ");
scanf("%d",&year);
if(year%4 == 0)
{
if( year%100 == 0)
{
if ( year%400 == 0)
printf("%d is a leap year.", year);
else
printf("%d is not a leap year.", year);
}
else
printf("%d is a leap year.", year );
}
else
printf("%d is not a leap year.", year);
getch();
}

RESULT:
Thus the program to find whether the given year is leap year or not was executed and the
output was verified.
EX.NO:2D DESIGN A CALCULATOR TO PERFORM THE OPERATIONS,
NAMELY, ADDITION,SUBTRACTION, MULTIPLICATION,
DATE: DIVISION AND SQUARE OF A NUMBER.

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

ALGORITHM:

1.Start the program.


2.Read a, b ,c.
3.print menu
4.read choice
5.switch(ch):
Add
Resut->a+b
print result
subract
Resut->a-b
print result
multiply
resut->a*b
print result
divide
result->a/b
print result
square
result->a*a
result1->b*b
print result
6.stop
PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,result,sq1,sq2,ch;
float divide;
clrscr();
printf("Enter two integers:");
scanf("%d%d",&a,&b);
printf("1.add,2.subtract,3.multiply,4.divide,5.square");
printf("\n Enter the choice;");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
result=a+b; printf("Sum=
%d\n",result); break;
}
case 2:
{ result=a-b; printf("Difference=
%d\n",result); break;

}
case 3: result=a*b; printf("Multiplication=%d\
{ n",result); break;

} result=a/(float)b;
case 4: printf("Division=%.2f\n",result);
{ break;

} sq1=a*a; printf("Square=
case 5: %d\n",sq1); sq2=b*b;
{ printf("Second square=%d\n",sq2);
break;

}
getch();
}

RESULT:
Thus the program to Design a calculator to perform the operation, namely, addition,
subtraction, multiplication, division and square of a number was executed and the output was
verified.
EX.NO:3 LOOPS: FOR, WHILE, DO-WHILE

DATE:

AIM:
To write a C Program to print positive integers from 1 to 10.

ALGORITHM:
1. Start
2. Declare variables
3. Read the Input number.
4. Sort the positive integers from the given number .
5. Match the result with input number.
6. Print the positive numbers.
7. Stop

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
//Using FOR LOOP
void main()
{
int i; for(i=1; i<=10;i++)
printf(“%d \n”, i);
getch();
}
//Using WHILE LOOP
void main()
{
int i=1;
while(i<=10)
{
printf(“%d \n”, i);
}
i++;
getch();
}
//Using DO-WHILE LOOP
void main()
{
int i=1;
do
{
printf(“%d \n”, i); i++; }
while(i<=10);
getch();
}
RESULT:
Thus a C Program to print positive integers from 1 to 10 was executed and the output
was obtained.
EX.NO:4 A 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:

1. Start
2. Declare variables
3. Read the number of elements .
4. Get the individual elements.
5. Calculate the weight for each element by the conditions
• 5 if it is a perfect cube (pow)
• 4 if it is a multiple of 4 and divisible by 6 (modulus operator)
• 3 if it is a prime number(modulus operator)
6. Display the output of the weight calculations after sorting .
7. Stop

PROGRAM:

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

RESULT:
Thus a C Program for Sort the numbers based on the weight was executed and the output
was obtained.
EX.NO:4B 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:

1. Start
2. Declare variables
3. Read the total number of persons and their height.
4. Calculate avg=sum/n and find number of persons their h>avg.
5. Display the output of the calculations .
6. Stop

PROGRAM:

/* Get a Height of Different Persons and find how many of them are are above average */
#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("\n Enter the Height of each person in centimeter\n");
for(i=0;i<n;i++)
{
scanf("%d",&height[i]);
sum = sum + height[i];
}
avg = (float)sum/n;
//Counting
for(i=0;i<n;i++)
if(height[i]>avg)
count++;
printf("\n Average Height of %d persons is : %.2f\n",n,avg);
printf("\n The number of persons above average : %d ",count);
getch();
}

RESULT:
Thus a C Program average height of persons was executed and the output was obtained.
EX.NO:4 B 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:

1. Start
2. Declare variables
3. Read the number of persons and their height and weight.
4. Calculate BMI=W/H2for each person
5. Display the output of the BMI for each person.
6. Stop

PROGRAM:

#include <stdio.h>
#include <conio.h>
void main()
{
int stu[100][2];`
int index[100];
int i,n;
float h;
clrscr();
printf("Enter the number of students : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the Height(cm) and Weight(kg) of student %d :",i+1);
scanf("%d%d",&stu[i][0],&stu[i][1]);
h = (float)(stu[i][0]/100.0);
index[i] = (float)stu[i][1]/(float)(h*h);
}
printf("\nStu.No.\tHeight\tWeight\tBMI\tResult\n");
for(i=0;i<n;i++)
{
printf("\n%d\t%d\t%d\t%d\t",i+1,stu[i][0],stu[i][1],index[i]);
if(index[i]<15)
printf("Starvation\n");
else if(index[i]>14 && index[i] < 18)
printf("Underweight\n");
else if(index[i] > 17 && index[i] < 26)
printf("Healthy\n");
else if(index[i] > 25 && index[i] < 31)
printf("Over weight\n");
else if(index[i] > 30 && index[i] < 36)
printf("Obese\n");
else
printf("Severe Obese\n");
} // for loop
getch();
}

RESULT:
Thus a C Program Body Mass Index of the individuals was executed and the output was
obtained.
EX.NO:5A 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:

1. Start
2. Declare variables .
3. Read a String.
4. Check each character of string for alphabets or a special character by using isAlpha() .
5. Change the position of a character vice versa if it is alphabet otherwise remains same.
6. Repeat step 4 until reach to the mid of the position of a string.
7. Display the output of the reverse string without changing the position of special characters .
8. 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;
}
// Main program
void main()
{
charstr[100];
// Function Prototype
void reverse(char *);
intisAlpha(char);
void swap(char *a ,char *b);
clrscr();
printf("Enter the Given String : ");
// scanf("%[^\n]s",str);
gets(str);
reverse(str);
printf("\nReverse String : %s",str);
getch();
}
void reverse(char str[100])
{
// Initialize left and right pointers
int r = strlen(str) - 1, l = 0;
// Traverse string from both ends until
// 'l' and 'r'
while (l < r)
{
// Ignore special characters
if (!isAlpha(str[l]))
l++;
else if(!isAlpha(str[r]))
r--;
else
{
swap(&str[l], &str[r]);
l++;
r--;
}
}
}
// To check x is alphabet or not if it an alphabet then return 0 else 1
intisAlpha(char x)
{
return ( (x >= 'A'&& x <= 'Z') ||
(x >= 'a'&& x <= 'z') );
}

RESULT:
Thus a c program of given string was executed and output was obtained.
EX.NO:5 B STRING OPERATIONS

DATE:
AIM:
To write a C Program to perform string operations on a given paragraph for the following
using built-in functions:
a. Find the total number of words.
b. Capitalize the first word of each sentence.
c. Replace a given word with another word.

ALGORITHM:

1. Start
2. Declare variables
3. Read the text.
4. Display the menu options
5. Compare each character with tab char „\t‟ or space char „ „ to count no of words
6. 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. (! . ?)
7. Replace the word in the text by user specific word if match.
8. Display the output of the calculations .
9. Repeat the step 4 till choose the option stop.
10. Stop

PROGRAM:

A. Find the total number of words.

#include<stdio.h>
#include<conio.h>
#define MAX_SIZE 100 // Maximum string size
void main()
{
char str[MAX_SIZE];
char prevChar;
int i, words;
clrscr();
/* Input string from user */
printf("\nEnter any string: ");
gets(str);
i = 0;
words = 0;
prevChar = '\0'; // The previous character of str[0] is null
/* Runs loop infinite times */
while(1)
{
if(str[i]==' ' || str[i]=='\n' || str[i]=='\t' || str[i]=='\0')
{
/**
* It is a word if current character is whitespace and
* previous character is non-white space.
*/
if(prevChar != ' ' && prevChar != '\n' && prevChar != '\t' && prevChar != '\0')
{
words++;
}
}
/* Make the current character as previous character
*/ prevChar = str[i];

if(str[i] == '\0')
break;
else
i++;
}
printf("Total number of words = %d", words);
getch();
}

b. Capitalize the first word of each sentence.

#include<stdio.h>
#include<conio.h>
#define MAX 100
void main()
{
char str[MAX]={0};
int i;
clrscr();
//input string
printf("Enter a string: ");
scanf("%[^\n]s",str); //read string with spaces
//capitalize first character of words
for(i=0; str[i]!='\0'; i++)
{
//check first character is lowercase alphabet
if(i==0)
{
if((str[i]>='a' && str[i]<='z'))
str[i]=str[i]-32; //subtract 32 to make it capital
continue; //continue to the loop
}
if(str[i]==' ')//check space
{
//if space is found, check next character
++i;
//check next character is lowercase alphabet

if(str[i]>='a' && str[i]<='z')


{
str[i]=str[i]-32; //subtract 32 to make it capital
continue; //continue to the loop
}
}
else
{
//all other uppercase characters should be in lowercase
if(str[i]>='A' && str[i]<='Z')
str[i]=str[i]+32; //subtract 32 to make it small/lowercase
}
}
printf("Capitalize string is: %s\n",str);
getch();
}

c. Replace a given word with another word.

#include <stdio.h>
#include <string.h>
#include<conio.h>
void main()
{
char s[] = "All work and no play makes Jack a dull boy.";
char word[10],rpwrd[10],str[10][10];
int i=0,j=0,k=0,w,p;
printf("All work and no play makes Jack a dull boy.\n"); printf("\
nENTER WHICH WORD IS TO BE REPLACED\n");
scanf("%s",word);
printf("\nENTER BY WHICH WORD THE %s IS TO BE REPLACED\n",word);
scanf("%s",rpwrd);
p=strlen(s);
for (k=0; k<p; k++)
{
if (s[k]!=' ')
{
str[i][j] = s[k];
j++;
}
else
{
str[i][j]='\0';
j=0; i++;
}
}
str[i][j]='\0';
w=i;
for (i=0; i<=w; i++)
{
if(strcmp(str[i],word)==0)
strcpy(str[i],rpwrd);
printf("%s ",str[i]);
}
getch();
}

RESULT:
Thus a C Program String operations was executed and the output was obtained.
EX.NO:6 SORTING USING PASS BY REFERENCE

DATE:
AIM:
To write a C Program to Sort the list of numbers using pass by reference.
ALGORITHM:
1. Start
2. Declare variables and create an array
3. Read the Input for number of elements and each element.
4. Develop a function to sort the array by passing reference
5. Compare the elements in each pass till all the elements are sorted.
6. Display the output of the sorted elements .
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();
}
void sortarray(int* arr,int num)
{
int i,j,temp;
for(i=0;i<num;i++)
for(j=i+1;j<num;j++)
if(arr[i] > arr[j])
{ temp=arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
RESULT:
Thus a C Program Sorting using pass by reference was executed and the output was
obtained.

Ex.No: 7
DATE: TOWERS OF HANOI USING RECURSION

AIM: To write a C Program to Solve towers of Hanoi using recursion.

ALGORITHM:

1. Start
2. Declare variables
3. Read the Input for number of discs.
4. Check the condition for each transfer of discs using recursion.
5. Display the output of the each move .
6. Stop

PROGRAM:

#include <stdio.h>
void towers(int, char, char, char);
void main()
{
int num;
clrscr();
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');
getch();
}
void towers(int num, 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);
}
RESULT:
Thus a C Program tower of Hanoi using recursion was executed and the output was obtained.
EX.NO:8A GENERATE SALARY SLIP OF EMPLOYEES USING STRUCTURES AND POINTERS

DATE:
AIM:
To write a C Program to Generate salary slip of employees using structures and pointers.

ALGORITHM:

1. Start
2. Declare variables
3. Read the number of employees .
4. Read allowances, deductions and basic for each employee.
5. Calculate net pay= (basic+ allowances)-deductions
6. Display the output of the Pay slip calculations for each employee.
7. Stop

PROGRAM:

#include<stdio.h>
#include<conio.h>
struct emp
{
int empno ;
char name[10] ;
nt bpay, allow, ded, npay ;
} e[10] ;
void main()
{
int i, n ;
clrscr() ;
printf("Enter the number of employees : ") ;
scanf("%d", &n) ;
for(i = 0 ; i < n ; i++)
{
printf("\nEnter the employee number : ") ;
scanf("%d", &e[i].empno) ; printf("\
nEnter the name : ") ;
scanf("%s", e[i].name) ;
printf("\nEnter the basic pay, allowances & deductions : ") ;
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("\nEmp. No. Name \t Bpay \t Allow \t Ded \t Npay \n\n") ;
for(i = 0 ; i < n ; i++)
{
printf("%d \t %s \t %d \t %d \t %d \t %d \n", e[i].empno,e[i].name, e[i].bpay, e[i].allow, e[i].ded,
e[i].npay) ;
}
getch() ;
}

RESULT:
Thus a C Program Salary slip of employees was executed and the output was obtained.
EX.NO:8B
INTERNAL MARKS OF STUDENTS
DATE:

AIM:
To write a C Program to Compute internal marks of students for five different subjects
using structures and functions.

ALGORITHM:

1. Start
2. Declare variables
3. Read the number of students .
4. Read the student mark details
5. Calculate internal mark by i=total of three test marks / 3 for each subject per student.
6. Display the output of the calculations for all the students .
7. Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
struct mark_sheet{
char name[20];
long int rollno;
int marks[10];
int total;
float average;
char rem[10];
char cl[20];
}students[100];
int main(){
int a,b,n,flag=1;
char ch;
clrscr();
printf("How many students : \n");
scanf("%d",&n); for(a=1;a<=n;+
+a){
clrscr();
printf("\n\nEnter the details of %d students : ", n-a+1);
printf("\n\nEnter student %d Name : ", a);
scanf("%s", students[a].name);
printf("\n\nEnter student %d Roll Number : ", a);
scanf("%ld", &students[a].rollno);
students[a].total=0;
for(b=1;b<=5;++b){
printf("\n\nEnter the mark of subject-%d : ", b);
scanf("%d", &students[a].marks[b]);
students[a].total += students[a].marks[b];
if(students[a].marks[b]<40)
flag=0;
}
students[a].average =(float)(students[a].total)/5.0;
if((students[a].average>=75)&&(flag==1))
strcpy(students[a].cl,"Distinction");
else
if((students[a].average>=60)&&(flag==1))
strcpy(students[a].cl,"First Class");
else
if((students[a].average>=50)&&(flag==1))
strcpy(students[a].cl,"Second Class");
else
if((students[a].average>=40)&&(flag==1))
strcpy(students[a].cl,"Third Class");
if(flag==1)
strcpy(students[a].rem,"Pass");
else
strcpy(students[a].rem,"Fail");
flag=1;
}
for(a=1;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 :
%ld", students[a].rollno); printf("\n ");
for(b=1;b<=5;b++)
{
printf("\n\n\t Subject %d \t\t :\t %d", b, students[a].marks[b]);
}
printf("\n\n \n");
printf("\n\n Totl Marks : %d", students[a].total); printf("\t\t\
t\t Average Marks : %5.2f", students[a].average); printf("\n\
n Class : %s", students[a].cl);
printf("\t\t\t\t\t Status : %s", students[a].rem);
printf("\n\n\n\t\t\t\t Press Y for continue . . . ");
ch = getche();
if((ch=="y")||(ch=="Y"))
continue;
}
return(0);
}
RESULT:
Thus a C Program for Internal marks of students was executed and the output was
obtained.
EX.NO:9 TELEPHONE DIRECTORY

DATE:

AIM:
To write a C Program to add, delete ,display ,Search and exit options for telephone details
of an individual into a telephone directory using random access file.

ALGORITHM:

1. Start.
2. Declare variables, File pointer and phonebook structures.
3. Create menu options.
4. Read the option .
5. Develop procedures for each option.
6. Call the procedure (Add, delete ,display ,Search and exit)for user chosen option.
7. Display the message for operations performed.
8. Stop

PROGRAM:

#include <stdio.h>
#include <conio.h>
#include <string.h>
struct person
{
char name[20];
long telno;
};
void appendData()
{
FILE *fp;
struct person obj;
clrscr();
fp=fopen("data.txt","a");
printf("*****Add Record****\n");
printf("Enter Name : ");
scanf("%s",obj.name);
printf("Enter Telephone No. : ");
scanf("%ld",&obj.telno);
fprintf(fp,"%20s %7ld",obj.name,obj.telno);
fclose(fp);
}
void showAllData(){
FILE *fp;
struct person obj;
clrscr();
fp=fopen("data.txt","r");
printf("*****Display All Records*****\n");
printf("\n\n\t\tName\t\t\tTelephone No.");
printf("\n\t\t=====\t\t\t==========\n\n");
while(!feof(fp))
{
fscanf(fp,"%20s %7ld",obj.name,&obj.telno);
printf("%20s %30ld\n",obj.name,obj.telno);
}
fclose(fp);
getch();
}
void findData()
{
FILE *fp;
struct person obj;
char name[20];
int totrec=0;
clrscr();
fp=fopen("data.txt","r");
printf("*****Display SpecificRecords*****\n");
printf("\nEnter Name : ");
scanf("%s",&name);
while(!feof(fp))
{
fscanf(fp,"%20s %7ld",obj.name,&obj.telno);
if(strcmpi(obj.name,name)==0){ printf("\n\
nName : %s",obj.name); printf("\
nTelephone No : %ld",obj.telno); totrec++;
}
}
if(totrec==0)
printf("\n\n\nNo Data Found");
else
printf("\n\n===Total %d Record found===",totrec);
fclose(fp);
getch();
}
void main(){
char choice;
while(1){
clrscr();
printf("*****TELEPHONE DIRECTORY*****\n\n");
printf("1) Append Record\n");
printf("2) Find Record\n");
printf("3) Read all record\n");
printf("4) exit\n");
printf("Enter your choice :
"); fflush(stdin);
choice = getche();
switch(choice){
case'1' : //call append record
appendData();
break;
case'2' : //call find record
findData();
break;
case'3' : //Read all record
showAllData();
break;
case'4' :
exit(1);
}
}
}

RESULT:
Thus a C Program was executed and the output was obtained.
EX.NO:10 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:

1. Start
2. Declare variables and file pointer.
3. Display the menu options.
4. Read the Input for transaction processing.
5. Check the validation for the input data.
6. Display the output of the calculations .
7. Repeat step 3 until choose to stop.
8. Stop

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;
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 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');
}

RESULT:
Thus a C Program for Banking Application was executed and the output was obtained.

You might also like