Sie sind auf Seite 1von 103

1.

//Palindrome and Perfect number


#include<stdio.h>
#include<conio.h>
int palindrome(int);
int perf_num(int);
void main()
{
int ch,n;
clrscr();
printf("enter the number to check:");
scanf("%d",&n);
printf("enter your choice: 1.Palindrome\n2.Perfect
Number");
scanf("%d",&ch);
switch(ch)
{
case 1:
palindrome(n);
break;
case 2:
perf_num(n);
break;
default:
printf("\nWrong input");
}
getch();
}
int palindrome(int num)
{
int r,sum=0,temp;

temp=num;
while(num)
{
r=num%10;
num=num/10;
sum=sum*10+r;
}
if(temp==sum)
printf("%d is a palindrome",temp);
else
printf("%d is not a palindrome",temp);

return 0;
}

int perf_num(int n)
{
int i=1,sum=0;

while(i<n)
{
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("%d is a perfect number",i);
else
printf("%d is not a perfect number",i);

return 0;
}

O/P:
2. //Fibonacci and sum of integers
#include<stdio.h>
#include<conio.h>
int fibo();
int int_sum();
void main()
{
int ch;
clrscr();
printf("enter your choice:\n1.Fibonacci
series\n2.sum of integers");
scanf("%d",&ch);
switch(ch)
{
case 1:
fibo();
break;
case 2:
int_sum();
break;
default:
printf("\nwrong input");
}
getch();
}

int fibo()
{
int a=0,b=1,temp,i;
printf("The first 10 terms of fibonnaci series are:
0,1,");
for(i=0;i<10;i++)
{
temp=a+b;
a=b;
b=temp;
printf("%d,",b);
}
return 0;
}

int int_sum()
{
int num,sum=0,r;
printf("Enter a number: ");
scanf("%d",&num);
while(num)
{
r=num%10;
num=num/10;
sum=sum+r;
}
printf("Sum of digits of number: %d",sum);
return 0;
}

O/P:
3. //simple calculator
#include<stdio.h>
#include<conio.h>
void main()
{
float a,b,res;
int ch;
printf("enter the two numbers:");
scanf("%f%f",&a,&b);
printf("enter your choice. \nmenu: 1.addition
2.subtraction 3.multiplication 4.division");
scanf("%d",&ch);
switch(ch)
{

case 1:
res=a+b;
break;
case 2:
res=a-b;
break;
case 3:
res=a*b;
break;
case 4:
res=a/b;
break;
default:
printf("wrong input");
getch();
exit (0);
}
printf("your result is: %f",res);
getch();
}

O/P:

4. //Electricity consumption bill


#include<stdio.h>
#include<conio.h>
void main()
{
int no,pow_con;
float rate;
clrscr();
printf("enter the customer number:");
scanf("%d",&no);
printf("enter the power consumed by the
customer:");
scanf("%d",&pow_con);
if(pow_con>=0&&pow_con<=200)
{
rate=0.5*pow_con;
}
else if(pow_con>=201&&pow_con<=400)
{
rate=100+(0.65*(pow_con-200));
}
else if(pow_con>=401&&pow_con<=600)
{
rate=230+(0.80*(pow_con-400));
}
printf("\nthe total amount to be paid by the
customer number: %d is %f",no,rate);
getch();
}

O/P:

5. //Special number
#include<stdio.h>
#include<conio.h>
int fac(int);
void main()
{
int n,temp,tot=0;
clrscr();

printf("Enter the number:");


scanf("%d",&n);
temp=n;
while(n!=0)
{
tot+=fac(n%10);
n/=10;
}
if(tot==temp)
{
printf("it is a special number");
}
else
{
printf("not a special number:");
}
getch();
}
int fac(int n)
{ int i,result=1;
for(i=1;i<=n;i++)
{
result*=i;
}
return result;
}

O/P:
6. //ticket charges
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
float amt,tot;
char name[20];
clrscr();
printf("enter the total number of customers:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the name of the customer %d:",i+1);
scanf("%s",name);
printf("Enter the ticket amount of the customer:");
scanf("%f",&amt);
if(amt>70000)
{
tot=(amt-((18/100)*amt));
}
else if(amt>=55001&&amt<=70000)
{
tot=(amt-((16/100)*amt));
}
else if(amt>=35001&&amt<55000)
{
tot=(amt-((12/100)*amt));
}
if(amt>=25001&&amt<35000)
{
tot=(amt-((10/100)*amt));
}
else if(amt<25001)
{
tot=(amt-((2/100)*amt));
}
printf("Amount to be paid by %d: %f",i+1,tot);
}
getch();
}

7. //Prime or automorphic number


#include <stdio.h>
#include<conio.h>
int automorph(int);
int prime(int);
int automorph(int n)

if ((n%10)*(n%10)==n || (n%100)*(n%100)==n ||
(n%1000)*(n%1000)==n)

printf("THIS IS AN AUTOMORPHIC
NUMBER");

else
printf("THIS IS NOT AN AUTOMORPHIC
NUMBER");

return 0;
}

int prime(int num)


{
int i, isPrime=0;
for(i=2;i<=(num/2);i++)
{
if(num%i==0)
{
isPrime=1;
break;
}
}

if(isPrime==0)
printf("%d is a Prime Number",num);
else
printf("%d is NOT a Prime Number",num);

return 0;
}

void main()
{
int n,ch;
clrscr();
printf("enter the number:");
scanf("%d",&n);
printf("enter your choice:\n1.automorphic
number\n2.prime number\n");
scanf("%d",&ch);
switch (ch)
{
case 1:
automorph(n);
break;
case 2:
prime(n);
break;
}
getch();
}
O/P:

8. //BUZZ number or G.C.D


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,temp,ch,no;
clrscr();
printf("enter your choice:\n1.BUZZ
NUMBER\n2.GCD");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("enter the number:");
scanf("%d",&no);
if((no%10)==7||(no%7==0))
{
printf("it is a buzz number");
}
else
printf("not a buzz number");
break;
case 2:
printf("enter the two numbers:\n");
scanf("%d%d",&a,&b);
if(b>a)
{
c=b;
b=a;
a=c;
}
while((a%b)!=0)
{
temp=a%b;
a=b;
b=temp;
}
printf("GCD IS: %d",b);
break;
};
getch();
}
O/P:

UNIT-2
1. //Splitting the array with the key
#include<stdio.h>
#include<conio.h>
void main()
{

int a[20],key,n,i,pos=0,temp;
clrscr();
printf("enter the array size:");
scanf("%d",&n);
printf("enter the elements of the array:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}

printf("enter the key to split the array:");


scanf("%d",&key);
for(i=0;i<n;i++)
{
if(a[i]!=key)
pos++;
else if(a[i]==key)
break;
}

for(i=0;i<=pos;i++)
{
temp=a[i];
a[i]=a[pos+i+1];
a[pos+i+1]=temp;
}
printf("changed array");
for(i=0;i<n;i++)
{
printf("%d",a[i]);

}
getch();
}

2. //Pancard
#include<stdio.h>
#include<conio.h>
void main()
{
char num[20],ch[20],no[20];
int i,j=0,k=0;
clrscr();
printf("enter the pancard number :");
scanf("%s",num);
for(i=0;num[i]!='\0';i++)
{
if(isalpha(num[i]))
{
ch[j]=num[i];
j++;
}
else
{
no[k]=num[i];
k++;
}
}

printf("numbers are: %s",no);


printf("\n characters are: %s",ch);
getch();
}

O/P:

3. //A+B=C+D
4. //Separating Odd and Even
#include<stdio.h>
#include<conio.h>
void main()
{
int num[20],odd[20],even[20],i,j=0,k=0,n;
clrscr();
printf("enter the size of array:");
scanf("%d",&n);
printf("enter the elements in the array:");
for(i=0;i<n;i++)
{
scanf("%d",&num[i]);
}
for(i=0;i<n;i++)
{
if(num[i]%2==0)
{
even[j]=num[i];
j++;
}
else
{
odd[k]=num[i];
k++;
}
}
printf("odd numbers are:");

for(i=0;i<k;i++)
{
printf("%d",odd[i]);
}
printf("\neven numbers are:");
for(i=0;i<j;i++)
{
printf("%d",even[i]);
}
getch();
}
O/P:

5. //Max even and MIN odd


#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,a[20],temp,tot=0;
clrscr();
printf("enter the array size:");
scanf("%d",&n);
printf("enter the elements:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
tot+=a[i];
}
for(i=1;i<=n;i++)
{
for(j=0;j<n-i;j++)
{
if(a[j]<a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\n first maximum even no:");
for(i=0;i<n;i++)
{
if(a[i]%2==0)
{
printf("%d",a[i]);
break;
}
}
printf("\n first minimum odd no:");
for(i=n-1;i>=0;i++)
{
if(a[i]%2!=0)
{
printf("%d",a[i]);
break;
}
}
printf("\nSum:%d",tot);
getch();
}

O/P:

6. //frequency of elements in a 2D array


#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],i,j,temp,b[10]={0},row,col;
clrscr();
printf("enter the row size:");
scanf("%d",&row);
printf("enter the column size:");
scanf("%d",&col);
printf("enter the elements of the 2D array:\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&a[i][j]);
temp=a[i][j];
b[temp]+=1;
}
}
printf("the entered 2D array is:\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
for(i=0;i<10;i++)
{ if(b[i]!=0)
printf("frequency of %d is: %d\n",i,b[i]);
}
getch();
}

O/P:
7. //merging the arrays and deleting the
duplicates.
#include<stdio.h>
#include<conio.h>
void main()
{
int
n1,n2,temp,a[100],b[100],c[200],i=0,j=0,k=
0,l,size;
clrscr();
printf("enter the size of the first array:");
scanf("%d",&n1);
printf("Enter the elements of the first
array:");
for(i=0;i<n1;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n1;i++)
{
for(j=0;j<n1-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("Enter the size of the second array:");
scanf("%d",&n2);
printf("enter the elements of the second
array:");
for(i=0;i<n2;i++)
{
scanf("%d",&b[i]);
}

for(i=0;i<n2;i++)
{
for(j=0;j<n2-1;j++)
{
if(b[j]>b[j+1])
{
temp=b[j];
b[j]=b[j+1];
b[j+1]=temp;
}
}
}
i=j=k=0;
while(i<n1&&j<n2)
if(a[i]>=b[j])
c[k++]= b[j++];
else
c[k++]=a[i++];

while(i<n1)
c[k++]=a[i++];

while(j<n2)
c[k++]= b[j++];
printf("The sorted and merged array is:");
for(i=0;i<k;i++)
{
printf("%d",c[i]);
}
for(i=0;i<k;i++)
{
for(j=i+1;j<k;j++)
{

if(c[i]==c[j])
{
for(l=j;l<k;l++)
{
c[l] = c[l+1];
}

k--;
j--;
}
}
}

printf("\nthe resultant array is:");


for(i=0;i<k;i++)
{
printf("%d",c[i]);
}
getch();
}

O/P:
8. //Matrix multiplication and printing odd and
even
#include<stdio.h>
#include<conio.h>

void main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10],
multiply[10][10];
clrscr();
printf("Enter the number of rows and
columns of first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of first
matrix\n");

for (c = 0; c < m; c++)


for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);

printf("Enter the number of rows and


columns of second matrix\n");
scanf("%d%d", &p, &q);

if (n != p)
printf("Matrices with entered orders can't
be multiplied with each other.\n");
else
{
printf("Enter the elements of second
matrix\n");

for (c = 0; c < p; c++)


for (d = 0; d < q; d++)
scanf("%d", &second[c][d]);

for (c = 0; c < m; c++) {


for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
sum = sum + first[c][k]*second[k][d];
}

multiply[c][d] = sum;
sum = 0;
}
}

printf("Product of entered matrices:-\n");

for (c = 0; c < m; c++) {


for (d = 0; d < q; d++)
printf("%d\t", multiply[c][d]);

printf("\n");
}
}
printf("Even numbers are:\n");
for(c=0;c<m;c++)
{
for(d=0;d<q;d++)
if(multiply[c][d]%2==0)
{
printf("Number is: %d, position:
%d,%d\n",multiply[c][d],c,d);
}
}

printf("Odd numbers are:\n");


for(c=0;c<m;c++)
{
for(d=0;d<q;d++)
if(multiply[c][d]%2!=0)
{
printf("Number is: %d, position:
%d,%d\n",multiply[c][d],c,d);
}
}
getch();

}
O/P:
UNIT-3
1. //Factorial functions
#include<stdio.h>
#include<conio.h>

//Function with no argument and no return


type
void func1()
{
int i,n,tot=1;
printf("enter the number to find the
factorial:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
tot*=i;
}
printf("the factorial is:%d",tot);
}

//Function with argument and no return type


void func2(int n)
{
int i,tot=1;
for(i=1;i<=n;i++)
{
tot*=i;
}
printf("The factorial is:%d",tot);
}

//Function without argument and with return


type
int func3()
{
int n,i,tot=1;
printf("Enter the number to find the
factorial:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
tot*=i;
}
return tot;
}

//Function with argument and with return


type
int func4(int n)
{
int i,tot=1;
for(i=1;i<=n;i++)
{
tot*=i;
}
return tot;
}

void main()
{
int ch,n,res;
clrscr();
printf("Press 1.Without argument and
without return type");
printf("\nPress 2.With argument and without
return type");
printf("\nPress 3.Without argument and with
return type");
printf("\nPress 4.With argument and with
return type");
scanf("\n%d",&ch);
switch(ch)
{
case 1:
func1();
break;
case 2:
printf("enter the number to find the
factorial:");
scanf("%d",&n);
func2(n);
break;
case 3:
res=func3();
printf("The factorial is:%d",res);
break;
case 4:
printf("Enter the number to find the
factorial:");
scanf("%d",&n);
res=func4(n);
printf("The factorial is:%d",res);
break;
default:
printf("Wrong input!");
}
getch();
}

O/P:

2. //odd and even till n.


#include<stdio.h>
#include<conio.h>
int find(int);
void main()
{
int n;
printf("enter the Nth number to find:");
scanf("%d",&n);
find(n);
getch();
}
int find(int n)
{
int i,odd=0,even=0;
for(i=1;i<=n;i++)
{
if(i%2==0)
even+=i;
else
odd+=i;
}
printf("even sum is:%d",even);
printf("\nodd sum is:%d",odd);
return 0;
}

O/P:

3. //Bike bill
#include<stdio.h>
#include<conio.h>
void input();
void compute();
void display();
int days,charge;
long long int bno;
char name[20];
void main()
{
clrscr();
printf("enter the respective details:\n");
input();
compute();
display();
getch();
}
void input()
{
printf("enter the phone number of the customer:");
scanf("%lld",&bno);
printf("\n enter the name of the customer:");
scanf("%s",name);
printf("\nenter the number of days bike taken as
rent:");
scanf("%d",&days);
}
void compute()
{
if(days<=5)
charge=days*500;
else if(days<=10)
charge=((days-5)*400)+(5*500);
else
charge=(((days-10)*200)+(5*400)+(5*500));
}
void display()
{
printf("%lld",bno);
printf("\t%s",name);
printf("\t%d",days);
printf("\t%d",charge);
} O/P:
4. //print design
#include<stdio.h>
#include<conio.h>
int print(int);
void main()
{
int n;
printf("enter the Nth number to print");
scanf("%d",&n);
print(n);
getch();
}
int print(int n)
{
int i,j;
for(i=1;i<=n;i++)
{
for(j=0;j<i;j++)
{
printf("%d",i);
}
printf("\n");
}
return 0;
}
O/P:
5. //Swapping using functions
#include<stdio.h>
#include<conio.h>
int swap(int,int);
void main()
{

int a,b;
clrscr();
printf("enter the two numbers to swap:");
scanf("%d %d",&a,&b);
printf("\n number before swapping:%d,%d",a,b);
swap(a,b);
getch();
}
int swap(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
printf("\nnumbers after swapping: %d,%d",a,b);
return 0;
}

O/P:
6. //Calculator using functions
#include<stdio.h>
#include<conio.h>
int calc(float,float,int);
void main()
{
float a,b;
int ch;
clrscr();
printf("enter the two numbers:");
scanf("%f%f",&a,&b);
printf("enter your choice. \nmenu: 1.addition
2.subtraction 3.multiplication 4.division");
scanf("%d",&ch);
calc(a,b,ch);
getch();
}
int calc(float a, float b,int ch)
{
float res;
switch(ch)
{

case 1:
res=a+b;
break;
case 2:
res=a-b;
break;
case 3:
res=a*b;
break;
case 4:
res=a/b;
break;
default:
printf("wrong input");
getch();
exit (0);
}
printf("your result is: %f",res);
return 0;
}
O/P:

7. //Concatinating two substrings with a key.


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char temp[20]={NULL},s[20]={NULL};
char s1[20]={NULL},s2[20]={NULL};
char key;
int i=0,j=0,k=0;
clrscr();
printf("Enter the string:");
gets(s);
strcpy(temp,s);
if(strcmp(temp,strrev(s))==0)
{
printf("it is a palindrome!");
}
else
{
printf("it is not a palindrome!");
printf("\nenter the key to split the string:");
scanf("%c",&key);
while(temp[i]!=key)
{
s1[i]=temp[i];
i++;
}
while(temp[i]!='\0')
{
s2[k]=temp[i];
i++;
k++;
}
printf("\n Concatinated string is:
%s",strcat(s2,s1));
}
getch();
}

O/P:
8. //Number of “The” in a sentence.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[100]={NULL},b[100][100]={NULL};
int i,j=0,k=0,len,count=0;
clrscr();
printf("enter the string:");
gets(a);
len=strlen(a);
for(i=0;i<len;i++)
{
if(a[i]!=' ')
{
b[j][k]=a[i];
k++;
}
else
{
j++;
k=0;
}
}
for(i=0;i<=j;i++)
{
if(strcmpi(b[i],"the")==0)
count++;
// printf("%d",count);
}
printf("Number of the's in the sentence are:

%d",count);
getch();
}

O/P:
UNIT-4
1. #include<stdio.h>
#include<conio.h>
struct emp
{
int emp_no;
char emp_name[20];
char emp_dept[10];
char emp_design[10];
int hra,da,ta;
int pf,loans,insur;
int basic_sal,allowances,ded;
int net_sal;
}obj;

void input()
{
printf("enter the employee number:");
scanf("%d",&obj.emp_no);
printf("enter the employee name:");
scanf("%s",obj.emp_name);
printf("enter the employee department:");
scanf("%s",obj.emp_dept);
printf("enter the employee designation:");
scanf("%s",obj.emp_design);
printf("enter the basic salary:");
scanf("%d",&obj.basic_sal);
printf("enter the HA,DA,and TA amount of
the employee:");

scanf("%d%d%d",&obj.hra,&obj.da,&obj.ta
);

printf("Enter the deductions amount -


PF,loansand Insurances:");
scanf("%d%d%d",&obj.pf,&obj.loans,&obj.
insur);
}
void calc()
{
obj.allowances=obj.hra+obj.da+obj.ta;
obj.ded=obj.pf+obj.loans+obj.insur;
obj.net_sal=(obj.basic_sal+obj.allowances)-
obj.ded;
}

void display()
{
printf("\n%d %s %s %s %d %d %d
%d",obj.emp_no,obj.emp_name,obj.emp_de
pt,obj.emp_design,obj.basic_sal,obj.allowan
ces,obj.ded,obj.net_sal);
}
void main()
{
clrscr();
input();
calc();
display();
getch();
}

O/P:
2.
#include<stdio.h>
#include<conio.h>
struct student
{
char name[20];
int reg,m[20],n;
float cgpa;
}s;
void input()
{
int i;
printf("Enter the name of the student:");
scanf("%s",s.name);
printf("Enter the student registration number:");
scanf("%d",&s.reg);
printf("Enter the number of subjects:");
scanf("%d",&s.n);
printf("\nEnter the subjects's marks:");
for(i=0;i<s.n;i++)
{
printf("\nenter the subject %d mark:",i+1);
scanf("%d",&s.m[i]);
}
}
void calc()
{ char g[20];
int gp[20];
int i,tot=0;
for(i=0;i<s.n;i++)
{
if(s.m[i]==100)
{
//g[i]='s';
gp[i]=10;
}
else if(s.m[i]>=90&&s.m[i]<100)
{
//g[i]='a';
gp[i]=9;
}
else if(s.m[i]>=80&&s.m[i]<90)
{
//g[i]='b';
gp[i]=8;
}
else if(s.m[i]>=70&&s.m[i]<80)
{
//g[i]='c';
gp[i]=7;
}
else if(s.m[i]>=60&&s.m[i]<70)
{
//g[i]='d';
gp[i]=6;
}
else if(s.m[i]>=50&&s.m[i]<60)
{
//g[i]='e';
gp[i]=5;
}
else if(s.m[i]>=20&&s.m[i]<50)
{
// g[i]="rx";
gp[i]=0;
}
else
{
// g[i]="ra";
gp[i]=0;
}
}
for(i=0;i<s.n;i++)
{
tot+=(gp[i]*3);
}
s.cgpa=tot/(s.n*3);
}
void display()
{
int i;
printf("\nStudent name:%s",s.name);
printf("\nStudent registration
number:%d",s.reg);
printf("\nStudent marks:");
for(i=0;i<s.n;i++)
{
printf("\nMark of subject %d is:
%d",i+1,s.m[i]);
}
printf("\nStudent's CGPA is:%f",s.cgpa);
}
void main()
{
clrscr();
input();
calc();
display();
getch();
}
O/P:

3.
#include<stdio.h>
#include<conio.h>
struct bank
{
char name[20],mail[50];
long int phone,acno;
float balance;
}b;
void input()
{
printf("enter the respective details:");
printf("\nEnter the name of the customer:");
scanf("%s",b.name);
printf(“\nEnter the account number:”);
scanf(“%ld”,&b.acno);
printf("enter the phone number of the
customer:");
scanf("%ld",&b.phone);
printf("enter the mail ID of the customer:");
scanf("%s",b.mail);
printf("enter the balance of the account:");
scanf("%f",&b.balance);
}
void display()
{
printf("Customer name: %s",b.name);
printf(“Account number: %ld”,b.acno);
printf("\n Customer phone number:
%ld",b.phone);
printf("\n Customer Mail ID: %s",b.mail);
printf("\n Customer balance: %f",b.balance);
}
void main()
{
clrscr();
input();
display();
getch();
}
O/P:
4.
#include<stdio.h>
#include<conio.h>
struct rail
{
char
name[20],t_name[30],ariv_time[10],dept_time[10]
;
int platform,seats,uprice,tot;
long int phone;
}r;
void input()
{

printf("enter the respective details:");


printf("\nEnter the name of the traveller:");
scanf("%s",r.name);
printf("enter the name of the train to travel:");
scanf("%s",r.t_name);
printf("enter the platform number of the train:");
scanf("%d",&r.platform);
printf("enter the arrival and departure time of the
train:");
scanf("%s %s",r.ariv_time,r.dept_time);
printf("enter the number of seats to be reserved:");
scanf("%d",&r.seats);
printf("enter the price of each seat:");
scanf("%d",&r.uprice);
r.tot=r.uprice*r.seats;
}
void display()
{
printf("\nName: %s",r.name);
printf("\nTrain name:%s",r.t_name);
printf("\nPlatform Number:%d",r.platform);
printf("\nArrival time of the
train:%s",r.ariv_time);
printf("\nDeparture time of the
train:%s",r.dept_time);
printf("\nNumber of seats to be
booked:%d",r.seats);
printf("\nPrice of each seat:%d",r.uprice);
printf("\nTotal amount to be paid:%d",r.tot);
}

void main()
{
clrscr();
input();
display();
getch();
}

O/P:

5.
#include<stdio.h>
#include<conio.h>
struct employee
{
int pan;
char name[20];
long int tax_income,tax;
}e;

void Input()
{
printf("enter the PAN number:");
scanf("%d",&e.pan);
printf("Enter the name:");
scanf("%s",e.name);
printf("\nEnter the taxable income of the
customer:");
scanf("%ld",&e.tax_income);
}
void calc()
{

if(e.tax_income<=100000)
{
e.tax=0;
}
else
if(e.tax_income>100000&&e.tax_income<=15000
0)
{

e.tax=(10/100)*(e.tax_income-100000);
}
else
if(e.tax_income>150000&&e.tax_income<=25000
0)
{
e.tax=5000+((20/100)*(e.tax_income-150000));
}
else
{
e.tax=25000+((30/100)*(e.tax_income-250000));
}
}
void display()
{
printf("\nPan Number:%d",e.pan);
printf("\nName:%s",e.name);
printf("\nIncome:%ld",e.tax_income);
printf("\nTax to be paid:%ld”,e.tax);
}
void main()
{
clrscr();
Input();
calc();
display();
getch();
}
O/P:
6.
#include<stdio.h>
#include<conio.h>
struct stock
{
char sname[20];
int sno;
int stot,sales,purchases;
}s;
void input()
{
printf("Enter the stock details:");
printf("enter the stock name:");
scanf("%s",s.sname);
printf("enter the stock number:");
scanf("%d",&s.sno);
printf("enter the total amount of the stock:");
scanf("%d",&s.stot);
printf("enter the sales of the stock:");
scanf("%d",&s.sales);
printf("enter the total purchases made:");
scanf("%d",&s.purchases);
}
void display()
{
printf("\nThe stock deatils are:");
printf("\nstock name:%s",s.sname);
printf("\nStock number:%d",s.sno);
printf("\nTotal stock:%d",s.stot);
printf("\ntotal sales of the stock:%d",s.sales);
printf("\nPurchases made:%d",s.purchases);
}
void main()
{
clrscr();
input();
display();
getch();
}
O/P:
7.
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct bank
{
long int acno;
char type[20];
struct amount
{
long int trans_amt;
long int cur_bal;
}s2;
}s1;
void input()
{
printf("enter the account number;");
scanf("%ld",&s1.acno);
printf("enter the current balance in the account:");
scanf("%ld",&s1.s2.cur_bal);
printf("enter the transaction type (withdraw or
deposit):");
scanf("%s",s1.type);
printf("Enter the transaction amount:");
scanf("%ld",&s1.s2.trans_amt);
}
void calc()
{
if(!strcmp(s1.type,"withdraw"))
{
s1.s2.cur_bal-=s1.s2.trans_amt;
}
else
{
s1.s2.cur_bal+=s1.s2.trans_amt;
}
}
void display()
{
printf("Account number: %ld",s1.acno);
printf("\nCurrent balance in the account, after
transaction: %ld",s1.s2.cur_bal);
printf("\nTransaction type chosen: %s",s1.type);
printf("\nTransaction amount:
%ld",s1.s2.trans_amt);
}
void main()
{
clrscr();
input();
calc();
display();
getch();
}

O/P:
UNIT-5
1.
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
FILE *ptr;
clrscr();
ptr=fopen("sample.txt","a");
printf("enter the infomation to be stored in the
file:");
while((ch=getchar())!=EOF)
{
fputc(ch,ptr);
}
fclose(ptr);
printf("reading the contents in the file:");
ptr=fopen("sample.txt","r");
while((ch=getc(ptr))!=EOF)
{
printf("%c",ch);
}
getch();
}
O/P:
2.
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char ch;
int nol=0,nos=0,noc=0;
clrscr();
printf(“Reading the file!”);
fp=fopen("q2file.txt","r");
while(1)
{
ch=fgetc(fp);

if(ch==EOF)
break;
if(ch==' '&&ch!='\n')
nos++;
if(ch=='\n')
nol++;
if(ch=='/')
{
noc++;
fp++;
}
}
fclose(fp);

printf("\n Number of blanks = %d",nos);


printf("\n Number of lines = %d",nol);
printf("\n Number of comments = %d",noc);
getch();
}

O/P:

3.
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
FILE *f1,*f2,*f3;
clrscr();
f1=fopen("file1.txt","r");
f2=fopen("file2.txt","r");
f3=fopen("file3.txt","w");
while((ch=getc(f1))!=EOF)
{
fputc(ch,f3);
}
while((ch=getc(f2))!=EOF)
{
fputc(ch,f3);
}
printf(“The two files are merged in a new file!”);
fclose(f1);
fclose(f2);
fclose(f3);
getch();
}
O/P:

4.
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
FILE *f1;
int n,i=0;
char ch,c[200]={NULL};
clrscr();
f1=fopen("PQR.txt","r");
while((ch=getc(f1))!=EOF)
{
c[i]=ch;
i++;
}
fclose(f1);
f1=fopen("abc.txt","w");
printf("Enter your choice (1.To convert into
upper, 2.To convert into lower) : ");
scanf("%d",&n);
switch(n)
{
case 1:
strupr(c);
fprintf(f1,"%s",c);
printf("Information has been converted to upper
case!");
break;
case 2:
strlwr(c);
fprintf(f1,"%s",c);
printf("Information has been converted to lower
case!");
break;

default:
printf("wrong input");
}

fclose(f1);
getch();
}
5.
#include<stdio.h>
#include<conio.h>
void input()
{
FILE *f1;
char name[20];
int sold,no;
long int sale;
f1= fopen("CAR.txt","a");
printf("Enter the car brand name:");
scanf("%s",name);
fprintf(f1,"\ncar name: %s\n",name);
printf("Enter the availability of the car:");
scanf("%d",&no);
fprintf(f1,"stock available:%d\n",no);
printf("Enter the total cars sold:");
scanf("%d",&sold);
fprintf(f1,"Total cars sold:%d",sold);
printf("Enter the total sales made:");
scanf("%ld",&sale);
fprintf(f1,"Total sales made:%ld",sale);
fclose(f1);
}
void display()
{
FILE *f1;
char ch;
f1=fopen("CAR.txt","r");
while((ch=getc(f1))!=EOF)
{
printf("%c",ch);

}
fclose(f1);
}
void main()
{int n;
clrscr();
printf("press 1.To display \n2.To input");
scanf("%d",&n);
if(n==1)
{
display();
}
else if(n==2)
{
input();
printf(“\nInformation has been updated in the
file”);
}
getch();
}
O/P:

6.
#include<stdio.h>
#include<conio.h>
void input()
{
FILE *f1;
char name[20],fname[20],prob[20];
int no;
long int phno;
f1= fopen("HOSPITAL.txt","w");
printf("Enter the patients name:");
scanf("%s",name);
fprintf(f1,"Name: %s\n",name);
printf("Enter the patient's bed number:");
scanf("%d",&no);
fprintf(f1,"Bed number:%d\n",no);
printf("Enter the patient's father name:");
scanf("%s",fname);
fprintf(f1,"Father name:%s\n",fname);
printf("Enter the patient's phone number:");
scanf("%ld",&phno);
fprintf(f1,"Phone number: %ld\n",phno);
printf("Enter the patient's problem:");
scanf("%s",prob);
fprintf(f1,"Problem: %s\n",prob);
fclose(f1);
}
void display()
{
FILE *f1;
char ch;
f1=fopen("ABCD.txt","r");
while((ch=getc(f1))!=EOF)
{
printf("%c",ch);

}
fclose(f1);
}
void main()
{int n;
clrscr();
printf("press 1.To display \n2.To input");
scanf("%d",&n);
if(n==1)
{
display();
}
else if(n==2)
{
input();
}
getch();
}

O/P: