C Programs for Basic Calculations and Conditions
C Programs for Basic Calculations and Conditions
NO:
1(a) SUM
DATE: AND
AIM:
ALGORITHM:
STEP 2: Read x, y
STEP 6: Stop
PROGRAM:
#include<stdio.h>
void main()
{
int x,y,sum,product;
printf("\nEnter two numbers: ");
scanf("%d %d",&x,&y);
sum=x+y;
product=x*y;
printf("\nSum = %d\nProduct = %d",sum,product);
}
OUTPUT:
Enter two numbers: 15 20
Sum = 35
Product = 300
RESULT:
Thus, C program using sum and product of two numbers was executed successfully
and the output was verified.
[Link]:
1(b) CONVER
DATE: T
AIM:
ALGORITHM:
STEP 2: Read f
STEP 4: Print c
STEP 5: Stop
PROGRAM:
#include<stdio.h>
void main()
{
float f,c;
printf("Enter the Fahrenheit: ");
scanf("%f",&f);
c=5.0/9.0*(f-32);
printf("\nCentigrade = %6.2f",c);
}
OUTPUT:
Centigrade = 40.56
RESULT:
AIM:
ALGORITHM:
STEP 2: Read r
STEP 4: Print a, c
STEP 5: Stop
PROGRAM:
#include<stdio.h>
void main()
{
float a,c,r;
printf("\nTo find the area and circumference of a circle");
printf("\nEnter the radius: ");
scanf("%f",&r);
a=(3.14)*r*r;
c=2*3.14*r;
printf("\nThe area of the circle is %f",a);
printf("\nThe circumference of the circle is %f",c);
}
OUTPUT:
RESULT:
Thus, C program using area and circumference of a circle was executed successfully
and the output was verified.
[Link]:
1(d) SIMPLE
DATE: INTERES
AIM:
ALGORITHM:
STEP 2: Read p, n, r
STEP 4: Print si
STEP 5: Stop
PROGRAM:
#include<stdio.h>
void main()
int p,n;
float r,si;
scanf("%d",&p);
scanf("%d",&n);
scanf("%f",&r);
si=(p*n*r)/100;
OUTPUT:
RESULT:
Thus, C Program using simple interest was executed successfully and the output was
verified.
[Link]:
1(e) AREA
DATE: AND
AIM:
ALGORITHM:
STEP 2: Read l, b
STEP 5: Stop
PROGRAM:
#include<stdio.h>
void main()
{
float area,p,l,b;
printf("\nEnter the length and width of a rectangle: ");
scanf("%f %f",&l,&b);
area=(l*b);
p=2*(l+b);
printf("\nArea of rectangle: %5.2f",area);
printf("\nPerimeter of the rectangle: %5.2f",p);
}
OUTPUT:
RESULT:
Thus, C program using area and perimeter of the rectangle was executed successfully
and the output was verified.
[Link]:
1(f) ILLUSTR
DATE: ATION
AIM:
ALGORITHM:
STEP 4: Stop
PROGRAM:
#include<stdio.h>
void main()
{
int a=5,b=2;
scanf(“%d%d”,&a,&b);
printf("\nAddition = %d",a+b);
printf("\nSubtraction = %d",a-b);
printf("\nMultiplication = %d",a*b);
printf("\nDivision = %d",a/b);
printf("\nModulo = %d",a%b);
}
OUTPUT:
Addition = 7
Subtraction = 3
Multiplication = 10
Division = 2
Modulo = 1
RESULT:
Thus, C program using arithmetic operators was executed successfully and the output
was verified.
[Link]: 2(a)
VOTER’S AGE VALIDATION
DATE:
AIM:
ALGORITHM:
STEP 4: Stop
PROGRAM:
#include<stdio.h>
void main()
{
int age;
printf("\nEnter your age: ");
scanf("%d",&age);
if(age>17)
{
printf("Eligible for Voting");
}
}
OUTPUT:
RESULT:
Thus, C Program using candidate eligible for voting using if statement was executed
and the output was verified.
[Link]:
2(b) ODD OR EVEN
DATE:
AIM:
To write C program to find the given number is even or odd using if-else statement.
ALGORITHM:
STEP 2: Read a
STEP 3: If(a%2==0) then print "even number" else print "odd number"
STEP 4: Stop
PROGRAM:
#include<stdio.h>
void main()
{
int a;
printf("\nEnter the number: ");
scanf("%d",&a);
if(a%2==0)
{
printf("\n%d is an even number",a);
}
else
{
printf("\n%d is an odd number",a);
}
}
OUTPUT:
24 is an even number
5 is an odd number
RESULT:
Thus, C program using the given number is odd or even using if-else statement was
executed successfully and the output was verified.
[Link]:
2(c) BIGGEST OF THREE NUMBERS
DATE:
AIM:
To write C program to find the biggest of three number using nested if statement
ALGORITHM:
STEP 2: Read x, y, Z
STEP 3: if ((x>y) &&(x>z)) then print "biggest number=x" else step 3a.
3 a) If (y>z) then print " Biggest number=y" else print "biggest number =z”
STEP 4: Stop
PROGRAM:
#include<stdio.h>
void main()
{
int x,y,z;
printf("\nEnter the three numbers: ");
scanf("%d %d %d",&x,&y,&z);
if((x>y)&&(x>z))
{
printf("The Biggest number = %d",x);
}
else
{
if(y>z)
{
printf("The Biggest number = %d",y);
}
else
{
printf("The Biggest number = %d",z);
}
}
}
OUTPUT:
RESULT:
Thus, C program using biggest of three number using nested id statement was
executed successfully and the output was verified.
[Link]: 2
(d) ELECTRICITY BILL CALCULATION
DATE:
AIM:
To write C program to prepare the electricity bill using if else ladder statement. An
electric charges and its domestic consumer as follows:
0-100 Rs.0.50/unit
ALGORITHM:
STEP 2: Declare the variable unit, number as integer and amount as float and name
as character
#include<stdio.h>
void main()
{
int units,num;
float amount;
char name[100];
printf("\n\tELECTRICITY BILL PREPARATION");
printf("\n\n\tEnter Consumer Number: ");
scanf("%d",&num);
printf("\n\tEnter the Consumer Name: ");
scanf("%s",name);
printf("n\tEnter the Consumed Units: ");
scanf("%d",&units);
if(units<=100)
{
amount=units*0.50;
}
else if(units<=200)
{
amount=50+0.75*(units-100);
}
else if(units<=300)
{
amount=125+1.00*(units-200);
}
else
{
amount=225+1.50*(units-300);
}
printf("\n\tELECTRICITY BILL");
printf("\n\t________________");
printf("\n\n\tNumber: %d\t\tName: %s",num,name);
printf("\n\n\tAmount to be paid: Rs.%6.2f",amount);
}
OUTPUT:
ELECTRICITY BILL
________________
RESULT:
Thus, C program using electricity bill preparation using if else ladder statement was
executed successfully and the output was verified.
[Link]: 3
TO
DATE: CHECK
AIM:
To write a c program to find whether the given year is leap year or not
ALGORITHM:
STEP 3: If the condition at step 2 ‘a becomes true’, then print the output as “It is a
leap year”.
STEP 4: If the condition at step 2 ‘b becomes true’, then print the output as “It is a
not leap year”.
STEP 5: If the condition at step 2 ‘c becomes true’, then print the output as “It is a
leap year”.
STEP 6: If neither of condition becomes true, then the year is not a leap year and
print the same.
PROGRAM:
#include<stdio.h>
void main()
{
int year;
printf("Enter a year: ");
scanf("%d",&year);
if((year%400)==0)
{
printf("%d is a leap year\n",year);
}
else if((year%100)==0)
{
printf("%d is not a leap year\n",year);
}
else if((year%4)==0)
{
printf("%d is a leap year\n",year);
}
else
{
printf("%d is not a leap year\n",year);
}
}
OUTPUT:
RESULT:
Thus, the program for checking the given year is leap year or not was executed
successfully and the output was verified.
[Link]: 4
SIMPLE
DATE: CALCULA
AIM:
ALGORITHM:
STEP 5: If the choice is 1, calculate c=a+b and print the c, then go to step11
STEP 6: If the choice is 2, calculate c=a-b and print the c, then goto step11
STEP 7: If the choice is 3, calculate c=a*b and print the c, then goto step11
STEP 8: If the choice is 4, calculate d=a/b and print the d, then goto step11
STEP 9: If the choice is 5, calculate c=a*a and print c then goto step11
#include<stdio.h>
#include<stdlib.h>
void main()
float d;
int a,b,c,n;
printf("\nMENU");
printf("\[Link]");
printf("\[Link]");
printf("\[Link]");
printf("\[Link]");
printf("\[Link] of a number");
printf("\[Link]");
scanf("%d",&n);
switch(n)
case 1:
scanf("%d %d",&a,&b);
c=a+b;
printf("\nAddition: %d",c);
break;
case 2:
scanf("%d %d",&a,&b);
c=a-b;
printf("\nSubtraction: %d",c);
break;
case 3:
scanf("%d %d",&a,&b);
c=a*b;
printf("\nMultiplication: %d",c);
break;
case 4:
scanf("%d %d",&a,&b);
d=a/b;
printf("\nDivision: %f",d);
break;
case 5:
printf("\nEnter a number:");
scanf("%d",&a);
c=a*a;
printf("\nSquare of a number: %d",c);
break;
case 6:
printf("\nProgram Terminated");
exit(0);
break;
default:
printf("Invalid choice");
break;
}
OUTPUT:
MENU
[Link]
[Link]
[Link]
[Link]
[Link] of a number
[Link]
Addition: 30
RESULT:
Thus, c program using simple calculator was executed successfully and the output
was verified.
[Link]: 5
TO
DATE: CHECK
AIM:
ALGORITHM:
STEP 4: Compute
a) r=n%10
b) s=s+(r*r*r)
c) n=n/10
STEP 5: If (t==s) then print” Armstrong number” else print “not an Armstrong
number”.
PROGRAM:
#include<stdio.h>
void main()
{
int n,t,s,r;
printf("\nEnter the number: ");
scanf("%d",&n);
t=n;s=0;
do
{
r=n%10;
s=s+(r*r*r);
n=n/10;
}
while(n>0);
if(t==s)
{
printf("%d is an Armstrong number",t);
}
else
{
printf("%d is not an armstrong number",t);
}
}
OUTPUT:
RESULT:
Thus, c program for checking the given number is Armstrong number or not was
executed successfully and the output was verified.
[Link]: 6 FIND
SUM OF
DATE: WEIGHT
BASED
AIM:
5 if it is perfect cube.
3 if it is a prime number
ALGORITHM:
t = ws[j=1];
ws[j+1] = ws[ j ];
t= nums[j+1];
nums[j+1] = nums[ j ];
nums[ j ] = k;
#include<stdio.h>
#include<math.h>
int cube(int num)
{
int a,flag=0;
a=ceil(pow(num,1.0/3.0));
if((a*a*a)==num)
{
flag=1;
}
return flag;
}
int prime(int num)
{
int i,flag=0;
for(i=2;i<=num/2;++i)
{
//condition for nonprime number
if(num%i==0)
{
flag=1;break;
}
}
return flag;
}
int getWeight(int n)
{
int w=0;
if(cube(n)==1)
{
w+=5;
}
if(n%4==0&&n%6==0)
{
w+=4;
}
if(prime(n)==0)
{
w+=3;
}
return w;
}
void main()
{
int nums[15];
int ws[15];
int i,j,t,n;
printf("Enter the limit:");
scanf("%d",&n);
printf("\nEnter the numbers:");
for(i=0;i<n;i++)
{
scanf("%d",&nums[i]);
}
for(i=0;i<n;i++)
{
ws[i]=getWeight(nums[i]);
printf("%d:%d\t",nums[i],ws[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(ws[j]>ws[j+1])
{
t=ws[j+1];
ws[j+1]=ws[j];
ws[j]=t;
t=nums[j+1];
nums[j+1]=nums[j];
nums[j]=t;
}
}
}
printf("\nSorted:\n");
for(i=0;i<n;i++)
{
printf("%d:%d\t",nums[i],ws[i]);
}
}
OUTPUT:
Sorted:
RESULT:
Thus, c program for finding sum of weights based on given conditions and sort
number based on weight was executed successfully and the output was verified.
[Link]: 7 POPULAT
E AN
DATE: ARRAY
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:
#include<stdio.h>
void main()
{
int i,n,sum=0,count=0,height[100];
float avg;
printf("Enter the Number of persons: ");
scanf("%d",&n);
printf("\nEnter the Height of each person in centimeter:");
for(i=0;i<n;i++)
{
scanf("%d",&height[i]);
sum=sum+height[i];
}
avg=(float)sum/n;
for(i=0;i<n;i++)
if(height[i]>avg)
{
count++;
}
printf("\nAverage Height of %d person is: %.2f",n,avg);
printf("\nThe number of persons above average: %d",count);
}
OUTPUT:
Enter the Height of each person in centimetre: 165 158 169 160 145 151
RESULT:
Thus, c program using array with height of persons and finding how many persons
are above the average height was executed and the output was verified.
[Link]: 8 COMPUT
E BMI OF
DATE: THE
AIM:
ALGORITHM:
#include<stdio.h>
void main()
{
int i,n,meter=100;
float hweight[10][2],temp,bmi;
printf("Enter the Number of persons:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the person [%d] height(in cm) and Weight(in kg) of each person:\
n",i+1);
scanf("%f%f",&hweight[i][0],&hweight[i][1]);
temp=hweight[i][0]/meter;
bmi=hweight[i][1]/(temp*temp);
printf("\nBody Mass Index of person %d = %f\n",i+1,bmi);
}
}
OUTPUT:
Enter the person [1] height(in cm) and Weight(in kg) of each person:
165 70
Enter the person [2] height(in cm) and Weight(in kg) of each person:
169 98
RESULT:
Thus, c program for computing BMI of the individual using 2D array with height and
weight was executed and output was verified.
[Link]: 9 REVERSE
THE
DATE: GIVEN
AIM:
To write a c program for the given a string a&bcd./[Link] its reverse without
changing the position of special characters. (Example input :a@gh%j and the output :j
%hg@a)
ALGORITHM:
Main() program:
IsAlpha(char X) function:
a) 1++;
b) r--;
#include<stdio.h>
#include<string.h>
int isAlpha(char X);
void swap(char*a,char*b)
{
char t;
t=*a;
*a=*b;
*b=t;
}
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
int isAlpha(char X)
{
return((X>='A'&&X<='Z')||(X>='a'&&X<='z'));
}
void main()
{
char str[100];
printf("Enter the Given string: ");
scanf("%s",str);
reverse(str);
printf("Reverse String: %s",str);
}
OUTPUT:
RESULT:
Thus, c program for reverse the given string without changing the position of special
characters was executed and the output was verified.
CONVERT
[Link]:
THE
10
GIVEN
DATE:
DECIMAL
NUMBER
AIM:
To write a c program to convert the given decimal number into binary, octal and
hexadecimal numbers using user defined functions.
ALGORITHM:
Main( ) program:
STEP 5: Stop
Binary(num) function:
STEP1: Take a decimal number as input and store it in the variable num.
STEP 4: Compute
i) remainder=num%2;
ii) num=num/2
iii) binary=binary+(remainder*i);
iv) i=i*10;
octal(num) function:
STEP 1: Take a decimal number as input and store it in the variable num.
STEP 3: Divide the variable quotient and obtain its remainder and quotient .store the
remainder in the array octal number and override the variable quotient with the
quotient
STEP 5: When it becomes zero. Print the array octal number in the reverse order to
get the output.
Hexa(num) function:
STEP 1: Take a decimal number as input and store it in the variable num.
STEP 2: Initialize the variable j=0 and copy num to variable quotient.
STEP 3: Obtain the quotient and remainder of the variable quotient. Store the
obtained remainder in the variable remainder and override the variable quotient
with obtained quotient.
STEP 6: When it becomes zero, print the array hexadecimal num in the reversed
fashion as output.
#include<stdio.h>
void octal(int);
void binary(int);
void hexa(int);
void main()
{
int num;
printf("\nEnter the Decimal number: ");
scanf("%d",&num);
binary(num);
octal(num);
hexa(num);
}
void binary(int num)
{
int remainder;
long binary=0,i=1;
while(num!=0)
{
remainder=num%2;
num=num/2;
binary=binary+(remainder*i);
i=i*10;
}
printf("\nEquivalent Binary value: %ld",binary);
}
void octal(int num)
{
long remainder,quotient;
int octalNumber[100],i=1,j;
quotient=num;
while(quotient!=0)
{
octalNumber[i++]=quotient%8;
quotient=quotient/8;
}
printf("\nEquivalent Octal value: ");
for(j=i-1;j>0;j--)
{
printf("%d",octalNumber[j]);
}
}
void hexa(int num)
{
long quotient,remainder;
int i,j=0;
char hexadecimalnum[100];
quotient=num;
while(quotient!=0)
{
remainder=quotient%16;
if(remainder<10)
{
hexadecimalnum[j++]=48+remainder;
}
else
{
hexadecimalnum[j++]=55+remainder;
}
quotient=quotient/16;
}
//display integer into character
printf("\nEquivalent Hexadecimal value:");
for(i=j-1;i>=0;i--)
{
printf("%c",hexadecimalnum[i]);
}
}
OUTPUT:
RESULT:
Thus, c program for converting the given decimal number into binary, octal and the
hexadecimal numbers using User defined functions was executed and the output was
verified.
[Link]:
11(a) FIND
DATE: THE
AIM:
ALGORITHM:
STEP 2: Using a for loop search for a space ‘ ‘ in the string and consecutively
increment a variable count.
STEP 4: Increment the variable count by 1 and print the variable count as input.
PROGRAM:
#include<stdio.h>
void main()
{
char s[200];
int count=0,i;
printf("Enter the string:\n");
scanf("%[^\n]s",s);
for(i=0;s[i]!='\0';i++)
{
if(s[i]==' ')
{
count++;
}
}
printf("\nNumber of words in given string are: %d\n",count+1);
}
OUTPUT:
This is a C program
RESULT:
Thus, the C program to find the total number of words in a given paragraph was
executed successfully and the output was verified.
[Link]: CAPITAL
11(b) IZE THE
DATE: FIRST
AIM:
ALGORITHM:
STEP 4: If(i==0) then check next character is lowercase alphabet. Subtract 32 to make
it capital and continue to the loop.
STEP 5: If (str[ i ]==’ ‘) then check next character is lowercase alphabet. Subtract 32
to make it capital and continue to the loop.
STEP 6: If neither (i.e. STEP 4 or 5 ) of the condition becomes true then all other
uppercase characters should be in lowercase. Subtract 32 to make it small /
lowercase.
STEP 9: Stop.
PROGRAM:
#include<stdio.h>
#include<string.h>
void main()
{
char str[100];
int i;
printf("Enter the string: ");
scanf("%[^\n]s",str); //read string with spaces
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("Capitalize string is: %s\n",str);
}
OUTPUT:
RESULT:
Thus, the C program for capitalizing the first character of each word in a given
paragraph was executed and the output was verified.
[Link]: REPLAC
11(c)
EA
DATE:
GIVEN
AIM:
ALGORITHM:
a) strcpy(str[ i ],rpwrd);
b) print = the word str[ i ];
#include<stdio.h>
#include<string.h>
void main()
{
char s[100];
char word[10],rpwrd[10],str[10][10];
int i=0,j=0,k=0,w,p;
printf("Enter the string:\n");
scanf("%[^\n]s",s);
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);
printf("\n");
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]);
}
}
OUTPUT:
C is a programming language.
Java
RESULT:
Thus, the C program for replacing a given word with another word in a given
paragraph was executed successfully and the output was verified.
[Link]:
12 TOWERS
DATE: OF
AIM:
ALGORITHM:
Main Program:
STEP 3: Stop.
STEP 1: If (num ==1) then print ”Move disk 1 freom source to dest “ return ;
#include<stdio.h>
void Hanoi(int,char,char,char);
void main()
{
int num;
printf("Enter the number of disks: ");
scanf("%d",&num);
printf("The sequence of moves involved in Tower of Hanoi are:\n");
Hanoi(num,'A','C','B');
}
void Hanoi(int num,char source,char dest,char aux)
{
if(num==1)
{
printf("\nMove disk 1 from %c to %c",source,dest);
return;
}
Hanoi(num-1,source,aux,dest);
printf("\nMode disk %d from %c to %c",num,source,dest);
Hanoi(num-1,aux,dest,source);
}
OUTPUT:
RESULT:
Thus, the C program using Towers of Hanoi using recursion was executed successfully
and the output was verified.
[Link]:
13 SORT
DATE: THE LIST
AIM:
ALGORITHM:
Main Program:
#include<stdio.h>
void sort(int *b,int num);
void main()
{
int n,i,a[50];
printf("\nEnter the limit: ");
scanf("%d",&n);
printf("\nEnter the elements:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nAfter Sorting...\n");
sort(a,n);
}
void sort(int *b,int num)
{
int i,j,t;
for(i=0;i<num;i++)
{
for(j=i+1;j<num;j++)
{
if(*(b+j) < *(b+i))
{
t = *(b + i);
*(b + i) = *(b + j);
*(b + j) = t;
}
}
}
for(i=0;i<num;i++)
{
printf("%d ",*(b+i));
}
}
OUTPUT:
12 34 56 7 8 10
After Sorting...
7 8 10 12 34 56
RESULT:
Thus, the C program for sorting the list of numbers using pass by reference was
executed successfully and the output was verified.
[Link]: GENERA
14 TE
DATE:
SALARY
AIM:
ALGORITHM:
Main Program:
STEP 1: Declare a structure as employee with following fields no, name, design_code
and days_worked.
STEP 2: Declare structure variable as emp[ 12 ] and assign 12 employee records using
arrays.
STEP 4: If (EMPNO && EMPNO<13) then call gen_payslip (EMPNO) function else
print”You have entered wrong employee no”.
STEP 5: Stop.
#include<stdio.h>
#include<dos.h>
#include<conio.h>
struct employee
{
int NO;
char NAME[10];
int DESIGN_CODE;
int DAYS_WORKED;
}EMP[12]=
{
{1,"GANESH",1,25},
{2,"MAHESH",1,30},
{3,"SURESH",2,28},
{4,"KALPESH",2,26},
{5,"RAHUL",2,24},
{6,"SUBBU",2,25},
{7,"RAKESH",2,23},
{8,"ATUL",2,22},
{9,"DHARMESH",3,26},
{10,"AJAY",3,26},
{11,"ABDUL",3,27},
{12,"RASHMI",4,29}
};
void main()
{
int EMPNO;
void gen_payslip(int);
clrscr();
printf("ENTER THE EMPLOYEE NO TO GENERATE PAYSLIP: ");
scanf("%d",&EMPNO);
if(EMPNO>0 && EMPNO<13)
{
gen_payslip(EMPNO);
}
else
{
printf("\nYOU HAVE ENTERED WRONG EMP NO.!!");
}
}
void gen_payslip(int EMPNO)
{
struct date D;
char DESG[10];
float NETPAY,BASIC,PF,PAYRATE,PTAX=200;
getdate(&D);
printf("\n\tSHREE KRISHNA CHEMISTS AND DRUGGIST");
printf("\n\tSALARY MONTH %d %d",D.da_mon,D.da_year);
printf("\n\[Link].: %d \[Link]: %s",EMPNO,EMP[EMPNO-1].NAME);
switch(EMP[EMPNO-1].DESIGN_CODE)
{
case 1:
PAYRATE=400;
printf("\tDESIGNATOION:CLERK");
break;
case 2:
PAYRATE=300;
printf("\tDESIGNATION:SALESMEN");
break;
case 3:
PAYRATE=250;
printf("\tDESIGNATION:HELPER");
break;
case 4:
PAYRATE=350;
printf("\tDESIGNATION:[Link]");
break;
}
BASIC=PAYRATE*EMP[EMPNO-1].DAYS_WORKED;
PF=BASIC/10;
printf("\n\tDAYS WORKED: %d",EMP[EMPNO-1].DAYS_WORKED);
printf("\tPAY RATE: %.0f \t\[Link]:
%d/%d/%d",PAYRATE,D.da_day,D.da_mon,D.da_year);
printf("\n\
t______________________________________________________________");
printf("\n\tEARNINGS \tAMOUNT(RS.) \tDEDUCTIONS \tAMOUNT(RS.)");
printf("\n\
t______________________________________________________________");
printf("\n\tBASIC PAY \t%.0f \t\tP.F. \t\t%.0f",BASIC,PF);
printf("\n\[Link] \t%.0f",PTAX);
printf("\n\
t______________________________________________________________");
printf("\n\tGROSS EARN. \t%.0f \tTOTAL DEDUCT. \t%.0f",BASIC,PF+PTAX);
NETPAY=BASIC-(PF+PTAX);
printf("\n\tNET PAY \t%.0f",NETPAY);
printf("\n\
t______________________________________________________________");
}
OUTPUT:
______________________
______________________
[Link] 200
______________________
______________________
RESULT:
Thus, the C program for generating salary slip of employees using structures and
pointers was executed successfully and the output was verified.
[Link]: COMPU
15 TE
DATE: INTERN
AL
AIM:
ALGORITHM:
STEP 1: Declare a structure as stud with following field rollno, name, s1, s2, s3, s4, s5 ,
tot and avg.
STEP 5: Read s[ i ].rollno, S[ i ].name, s[ i ].s1, s[ i ].s2, s[ i ].s3, s[ i ].s4 and s[ i ].s5.
s[ i ].avg.
#include<stdio.h>
#include<conio.h>
struct stud
{
int rollno,s1,s2,s3,s4,s5,tot;
char name[100];
float avg;
}s[10];
void main()
{
int i,n;
printf("Enter the number of students: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the roll number: ");
scanf("%d",&s[i].rollno);
printf("Enter the name: ");
scanf("%s",s[i].name);
printf("Enter the marks in 5 subjects:\n");
scanf("%d %d %d %d %d",&s[i].s1,&s[i].s2,&s[i].s3,&s[i].s4,&s[i].s5);
s[i].tot=s[i].s1+s[i].s2+s[i].s3+s[i].s4+s[i].s5;
s[i].avg=s[i].tot/5.0;
}
printf("\n************************Student's Mark Details*************");
printf("\n_______________");
printf("\[Link] Name\t\tSub1\tSub2\tSub3\tSub4\tSub5\tTotal\tAverage");
printf("\n_________________\n");
for(i=0;i<n;i++)
{
printf("%d\t%s\t\t%d\t%d\t%d\t%d\t%d\t%d\t%.2f\
n",s[i].rollno,s[i].name,s[i].s1,s[i].s2,s[i].s3,s[i].s4,s[i].s5,s[i].tot,s[i].avg);
}
}
OUTPUT:
45 56 89 73 95
89 98 75 63 72
_______________
_________________
RESULT:
Thus, the C program to compute internal marks of students for five different subjects
using structures and functions was executed successfully and the output was verified.
[Link]: GENERA
16 TE
DATE: TELEPH
ONE
DETAILS
AIM: OF AN
INDIVID
To write a C program to insert, update, delete and append telephone details of an
individual or a company into a telephone directory using random access file.
ALGORITHM:
1. Append Record
2. Find Record
3. Read all record
4. Exit
STEP 6: Stop
appenddate( ):
STEP 5: Write the [Link] and [Link] to [Link] by using fprintf( ) function.
showalldate( ):
STEP 3: Open the file “[Link]” under read mode by assigning file pointer fp.
STEP 5: Read [Link] and [Link] from [Link] by using fscanf( ) function.
finddata( ):
STEP 3: Open the file “[Link]” under read mode by assigning file pointer fp.
STEP 4: Read the value of the variable name from the user.
STEP 6: Read [Link] and [Link] from [Link] by using fscanf( ) function.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct person
char name[20];
long telno;
};
void appendData()
FILE*fp;
fp=fopen("[Link]","a");
printf("\n****Add Record****\n");
scanf("%s",[Link]);
scanf("%ld",&[Link]);
fprintf(fp,"%20s %7ld",[Link],[Link]);
fclose(fp);
void showAllData()
{
FILE*fp;
fp=fopen("[Link]","r");
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",[Link],&[Link]);
printf("%20s %30ld\n",[Link],[Link]);
fclose(fp);
void findData()
FILE*fp;
char name[20];
int totrec=0;
fp=fopen("[Link]","r");
while(!feof(fp))
fscanf(fp,"%20s %7ld",[Link],&[Link]);
if(strcmp([Link],name)==0)
printf("\n\nName: %s",[Link]);
totrec++;
if(totrec==0)
else
fclose(fp);
void main()
int choice;
while(1)
printf("\n\n****TELEPHONRE DIRECTORY****\n\n");
printf("1)Append Record\n");
printf("2)Find Record\n");
printf("4)Exit\n");
fflush(stdin);
scanf("%d",&choice);
switch(choice)
appendData();
break;
findData();
break;
showAllData();
break;
case 4:
exit(1);
}
OUTPUT:
****TELEPHONRE DIRECTORY****
1)Append Record
2)Find Record
4)Exit
****Add Record****
****TELEPHONRE DIRECTORY****
1)Append Record
2)Find Record
4)Exit
==== =======
Raju 9638527419
RESULT:
AIM:
To write a C program to count the number of account holders whose balance is less
than minimum balance using sequential access file.
ALGORITHM:
STEP 2: Declare a structure as account with following fields acno, acname and bal.
1. Add Account
2. Display
3. Deposit or withdraw
4. [Link] Account holders whose bal is less than the min_bal
5. Delete all records
6. Exit using do while loop.
a) Open the file “[Link]” under append mode by assigning file pointer fp1.
b) Read the [Link], [Link] and [Link]
c) Move the file pointer fp1 to last position in the file.
d) Write the [Link], [Link] and [Link] to [Link] by using fwrite( )
function.
e) Close the file pointer fp1.
STEP 9: If ch = 2 then
a) Open the file “[Link]” under read mode by assigning file pointer fp1.
b) If (fp1==NULL) then print “No record exists in the file” else steps 9c to 9e.
c) Repeat the step 9d until (fread(&[Link](a1),1,fp1)==1)
d) Print [Link], [Link] and [Link].
e) Close the file pointer fp1.
a) Assign f=0.
b) Open the file “[Link]” under read mode by assigning file pointer fp1.
c) Read the value of the variable [Link]
d) Assign pos1 = ftell(fp1).
e) Repeat the steps 10f until (fread(&[Link](a1),1,fp1)==1)
f) If (strcmp([Link],aacno)==0) then
i) Read the variable type
ii) Print [Link]
iii) Read the variable amt.
iv) If (type==1) then bal = atof([Link]) + atof(amt)
else
bal = atof([Link])-atof(amt)
if (bal<0) then print amt and assign f=2 and exit the condition.
v) Assign f = f+1 and exit from if condition.
vi) Assign pos1=ftell(fp1)
vii) If (f=1) then
pos2=ftell(fp1)
pos=pos2-pos1
fseek(fp1,-pos,1)
sprint(amt,”%.2f”,bal)
strcpy([Link],amt)
fwrite(&a1,sizeof(a1),1,fp1)
else if (f==0)
print “A/C Number not exists……Check it again”
g) close the file pointer fp1.
a) OPen the file “[Link]” under read mode by assigning file pointer fp1
b) Assign f=0
c) Repeat the steps 11d to 11e until (fread(&a1,sizeof(a1),1,fp1)==1)
d) Assign bal=atof([Link])
e) If (bal<MBAL) then f++
f) Printf
g) Close the file pointer fp1
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MBAL 1000
struct account
{
char acno[10];
char acname[20];
char bal[15];
};
struct account a1;
void main()
{
long int pos1,pos2,pos;
FILE*fp1;
char *aacno,*amt;
int ch;
int type,f=0;
float bal;
do
{
fflush(stdin);
printf("\n");
printf("[Link] Account\n");
printf("[Link]\n");
printf("[Link] or Withdraw\n");
printf("[Link]. of Account Holder Whose Bal is less than the Min_Bal\n");
printf("[Link] all records\n");
printf("[Link]\n");
printf("Enter your ch:");
scanf("%d",&ch);
switch(ch) {
case 1:
fflush(stdin);
fp1=fopen("[Link]","a");
printf("\nEnter the Account Number: ");
scanf("%s",[Link]);
printf("\nEnter the Account Holder Name:");
scanf("%s",[Link]);
printf("\nEnter the Initial Amount to deposit: ");
scanf("%s",[Link]);
fseek(fp1,0,2);
fwrite(&a1,sizeof(a1),1,fp1);
fclose(fp1);
break;
case 2:
fp1=fopen("[Link]","r");
if(fp1==NULL)
printf("\nNo record exists in the File");
else
{
printf("\nA/c Number\tA/c Holder Name\tBalance\n");
while(fread(&a1,sizeof(a1),1,fp1)==1)
printf("%-10s\t%20s\t%s\n",[Link],[Link],[Link]);
fclose(fp1);
}
break;
case 3:
fflush(stdin);
f=0;
fp1=fopen("[Link]","r");
printf("\nEnter the Account Number:");
scanf("%s",[Link]);
for(pos1=ftell(fp1);fread(&a1,sizeof(a1),1,fp1)==1;pos1=ftell(fp1))
{
if(strcmp([Link],aacno)==0)
{
printf("\nEnter the Type 1 for deposit & Type 2 for withdraw:");
scanf("%d",&type);
printf("\n Your Current Balance is :%s",[Link]);
printf("\nEnter the Amount to transact:");
fflush(stdin);
scanf("%s",amt);
if(type==1)
{
bal=atof([Link])+atof(amt);
}
else
{
bal=atof([Link])-atof(amt);
if(bal<0)
{
printf("\nRs.%sNot available in your A/c\n",amt);
f=2;
break;
}
}
f++;
break;
if(f==1)
{
pos=ftell(fp1);
pos=pos2-pos1;
fseek(fp1,-pos,1);
sprintf(amt,"%.2f",bal);
strcpy([Link],amt);
fwrite(&a1,sizeof(a1),1,fp1);
}
else if(f==0)
{
printf("\nA/c Number Not exits...Check it again");
fclose(fp1);
break;
}}
}
break;
case 4:
fp1=fopen("[Link]","r");
f=0;
while(fread(&a1,sizeof(a1),1,fp1)==1)
{
bal=atof([Link]);
if(bal<MBAL)
f++;
}
printf("\nThe Number of Account Holder Whose Balance less than the minimum
Balance:%d",f);
fclose(fp1);
break;
case 5:
remove("[Link]");
break;
case 6:
fclose(fp1);
exit(1);
}
printf("\nPress any key to continue...");
}while(ch!='6');
}
OUTPUT:
[Link] Account
[Link]
[Link] or Withdraw
[Link]
[Link] Account
[Link]
[Link] or Withdraw
[Link]
Thus, the C program to count the number of account holders whose balance is less
than minimum balance using sequential access file was executed successfully and the
output was verified.
[Link]: 18 MINI
PROJEC
DATE:
T
PROGRAM:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define TRUE 1
void Booking(); void Display(); void Cancel();void Availability();
struct node
{
int seatno;
char name[20];
int age;
char sex;
struct node *next;
};
struct node *head=NULL, *end;
void main()
{
int op;
while(TRUE)
{
printf("\n Welcome to Railway Reservation System ");
printf("\n [Link]\n [Link] Chart\n [Link]\n [Link] Checking\n
[Link] ");
printf("\nEnter the choice: ");
scanf("%d",&op);
switch(op)
{
case 1:
Booking();
break;
case 2:
Display();
break;
case 3:
Cancel();
break;
case 4:
Availability();
break;
case 5:
exit(0);
}
}
}
void Booking()
{
struct node *newnode;
newnode=malloc(sizeof(struct node));
newnode->next=NULL;
if(head==NULL)
{
printf("Enter the Passenger name : ");
scanf("%s",newnode->name);
printf("Enter the Passenger age : ");
scanf("%d",&newnode->age);
printf("Enter the Passenger sex : ");
scanf("%s",&newnode->sex);
newnode->seatno=1;
head=newnode;
}
else
{
end = head;
while(end->next!=NULL)
{
end=end->next;
}
if(end->seatno==100)
{
printf("\nTrain Seat Capacity is Full");
return;
}
printf("Enter the Passenger name :");
scanf("%s",newnode->name);
printf("Enter the Passenger age : ");
scanf("%d",&newnode->age);
printf("Enter the Passenger sex : ");
scanf("%s",&newnode->sex);
newnode->seatno=end->seatno+1;
end->next = newnode;
}
}
void Display()
{
struct node *temp;
temp=head;
printf("\n\t\tDisplay Chart \n");
printf("\n SeatNo. Passenger Name\tAge\tSex \n");
while(temp!=NULL)
{
printf("\n %d\t%s\t%d\t%c", temp->seatno,temp->name,temp->age,temp->sex);
temp = temp->next;
}
}
void Cancel()
{
struct node *del;
struct node *temp;
int a;
del=head;
printf("Enter the Seat Number to be deleted: ");
scanf("%d",&a);
while(del!=NULL)
{
if(del->seatno== a)
{
if(del== head)
{
head=del->next;
}
else
{
temp->next=del->next;
free(del);
printf("\n Seat Number %d deleted \n",a);
return;
}
}
temp=del;
del=del->next;
}
if(del==NULL)
{
printf("No record exist");
return;
}
}
void Availability()
{
int a,b;
struct node *temp; temp=head;
while(temp!=NULL)
{
b = temp->seatno;
temp = temp->next;
}
printf("Last Booked Seat no = %d",b);
a=100-b;
printf("\n The Available Seats : %d", a);
return;
}
OUTPUT:
[Link]
[Link] Chart
[Link]
[Link] Checking
[Link]
[Link]
[Link] Chart
[Link]
[Link] Checking
[Link]
[Link]
[Link] Chart
[Link]
[Link] Checking
[Link]
1 Rajesh 29 M
2 Malar 26 F
[Link]
[Link] Chart
[Link]
[Link] Checking
[Link]