CS8261 C Programming Lab Manual With All 18 Programs
CS8261 C Programming Lab Manual With All 18 Programs
1
2.CHECKING A NUMBER IS ODD OR EVEN
#include<stdio.h>
void main()
{
int a;
printf("Enter a number");
scanf("%d",&a); if(a
%2==0)
printf("%d is even",a);
else
printf("%d is odd",a);
}
Input and Output:
Enter a number:4
4 is even
3.LEAP YEAR OR NOT
#include<stdio.h>
void main()
{
int year;
printf("Enter the year");
scanf("%d",&year);
if(year%4==0&&year%100!=0||year%400==0)
printf("\nThe Given year %d is a leap year");
else
printf("\nThe Given year %d is not a leap year");
}
Input and output:
Enter the year2000
The Given year 2000 is a leap year
4.SIMPLE CALCULATOR
#include<stdio.h>
void main()
{
int choice,a,b,c,d;
printf("Enter two numbers:");
scanf("%d%d",&a,&b);
printf("\nYour choices:");
printf("\n1.Addition");
printf("\n2.Subtraction");
printf("\n3.Multiplication");
printf("\n4.Division");
printf("\n5.Sqaure");
printf("\n Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
c=a+b; printf("\nSum=
%d",c); break;
case 2:
c=a-b; printf("\nDifference=
%d",c); break;
case 3:
c=a*b; printf("\nProduct=
%d",c); break;
case 4:
c=a/b; printf("\nQuotient:
%d",c);
break;
case 5:
c=a*a;
d=b*b;
printf("\n Square of %d =%d",a,c);
printf("\n Square of %d =%d",b,d);
break;
default:printf("\nEnter your choice from 1 to 5");
}
}
Input and Output:
Enter two numbers:12 4
Your choices:
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Square
Enter your choice:1
Sum=16
5.CHECKING A NUMBER IS ARMSTRONG OR NOT
#include<stdio.h>
void main()
{
int a,r,n,rem,sum=0;
printf("Enter the number:");
scanf("%d",&n);
a=n;
while(n!=0)
{
r=n%10;
sum+=r*r*r;
n=n/10;
}
if(a==sum)
{
printf("\n The number is an armstrong number");
}
else
{
printf("\n The number is not an armstrong number");
}
}
OUTPUT
Enter the number:371
The number is an armstrong number
6.SUM OF WEIGHTS BASED ON CONDITIONS
#include <stdio.h>
#include<math.h>
void main()
{
int a[15],w[15],i,j,t,n;
printf("Enter the number of elements");
scanf("%d",&n);
printf("Enter the elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
w[i]=getweight(a[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(w[i]>w[j])
{
t=w[i];
w[i]=w[j];
w[j]=t;
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
for(i=0;i<n;i++)
{
printf("<%d,%d>",a[i],w[i]);
}
}
int getweight(int n)
{
int w=0,c=0,i,root;
root=round(pow(n,1.0/3.0));
if(root*root*root==n)
w+=5;
if(n%4==0&&n%6==0)
w+=4;
for(i=1;i<=n;i++)
{
if(n%i==0)
c+=1;
}
if(c==2)
w+=3;
return w;
}
Input and output:
Enter the number of elements6
Enter the elements10
36
54
89
12
27
<10,0><54,0><89,3><36,4><12,4><27,5>
7. FINDING AVERAGE HEIGHT USING ARRAYS
#include <stdio.h>
int main()
{
inti,n,sum=0,count=0,height[100];
float avg;
printf("Enter the Number of Persons : ");
scanf("%d",&n);
#include<stdio.h>
#include<string.h>
void main()
{
char a[50],t;
int l,r;
printf("Enter the string");
gets(a);
l=0;
r=strlen(a)-1;
while(l<r)
{
if(!isalpha(a[l]))
l++;
else if(!isalpha(a[r]))
r--;
else
{
t=a[l];
a[l]=a[r];
a[r]=t; l+
+;
r--;
}
}
printf(“The Reversed string is ..”);
puts(a);
#include<stdio.h>
struct Employee
{
int empno ;
char name[10] ;
int bpay, da, hra, npay, gpay, pf;
} emp[10] ;
void main()
{
int i, n ;
struct Employee *ptr = emp;
printf("Enter the number of employees : ") ;
scanf("%d", &n) ;
for(i = 0 ; i < n ; i++)
{
printf("\n Enter the employee number : ") ;
scanf("%d", &ptr->empno) ;
printf("\n Enter the name : ") ;
scanf("%s", ptr->name) ;
printf("\n Enter the basic pay") ;
scanf("%d", &ptr->bpay);
ptr->da=80* ptr->bpay / 100;
ptr->hra=20* ptr->bpay / 100;
ptr->pf= 10* ptr->bpay / 100;
ptr->gpay= ptr->bpay + ptr->da + ptr->hra;
ptr->npay = ptr->gpay - ptr->pf;
printf("\n emp. No. Name \t Bpay \t da \t hra \t pf \t gpay \t npay\n\n") ;
printf("%d \t %s \t %d \t %d \t %d \t %d \t %d\t %d\n", ptr->empno, ptr-
>name, ptr
->bpay, ptr->da, ptr->hra, ptr->pf, ptr->gpay , ptr->npay) ;
}
}
INPUT AND OUTPUT:
Enter the number of employees : 2
Enter the employee number : 1002
Enter the name :RAGUL
Enter the basic pay:15000
struct person
{
char name[20],no[20];
}p;
void insert()
{
FILE *fp;
int i;
fp=fopen("tel1.txt","w");
printf("Enter name: ");
scanf("%s",p.name);
printf("Enter phone number: ");
scanf("%s",p.no);
fwrite(&p,sizeof(p),1,fp);
fclose(fp);
}
void display()
{
FILE *fp;
int i;
fp=fopen("tel1.txt","rb");
rewind(fp);
while(fread(&p,sizeof(p),1,fp)==1)
{
printf("Name=%s",p.name);
printf("No=%s",p.no);
}
fclose(fp);
}
void append()
{
FILE *fp;
fp=fopen("tel1.txt","ab");
printf("Enter name: ");
scanf("%s",p.name);
printf("Enter no: ");
scanf("%s",p.no);
fwrite(&p,sizeof(p),1,fp);
fclose(fp);
}
void main()
{
int ch;
do
{
printf("1.insert\n 2.display\n 3.append\n 4.exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
display();
break;
case 3:
append();
break;
case 4:
exit(0);
}
}while(ch<=4);
}
2
Name=kala No=585989
Name=kayal No=787900
1.insert
2.display
3.append
4.exit
17. COUNT THE NUMBER OF ACCOUNT HOLDERS WHOSE
BALANCE IS LESS THAN THE MINIMUM BALANCE USING
SEQUENTIAL ACCESS FILE
#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[10],amt[10];
char choice;
int type,flag=0;
float bal;
do
{
fflush(stdin);
printf("1. Add a New Account Holder\n");
printf("2. Display\n");
printf("3. Deposit or Withdraw\n");
printf("4. Number of Account Holder Whose Balance is less than the Minimum
Balance\n");
printf("5. Stop\n");
printf("Enter your choice : ");
choice=getchar();
switch(choice)
{
case '1' :
fflush(stdin);
fp=fopen("acc.txt","a");
printf("\nEnter the Account Number : ");
gets(acc.no);
printf("\nEnter the Account Holder Name : ");
gets(acc.name);
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.txt","r");
flag=0;
while(fread(&acc,sizeof(acc),1,fp)==1)
{
bal = atof(acc.balance);
if(bal<MINBAL)
flag++;
}
printf("\nThe Number of Account Holder whose Balance less than the Minimum Balance
:%d",flag);
fclose(fp);
break;
case '5' :
fclose(fp);
exit(0);
}
printf("\nPress any key to continue. ");
} while (choice!='5');
}
#include<stdio.h>
#include<string.h>
struct air
{
char name[20];
int age;
long phno;
char address[50];
int ticketno;
}
s[100];
void view();
void reserve();
void cancel();
void form();
void menu()
{
int ch;
printf("\n\t RAILWAY RESERVATION SYSTEM");
printf("\n \n 1.VIEW ALL TRAINS");
printf("\n \n 2.RESERVE A TICKET");
printf("\n \n 3.CANCEL A TICKET");
printf("\n \n 4.EXIT");
printf("\n Enter your choice:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
view();
menu();
break;
case 2:
reserve();
menu();
break;
case 3:
cancel();
menu();
break;
case 4:
break;
default:
printf("\n Enter a valid choice");
}
}
void view()
{
printf("CODE-ROUTE-TIMINGS");
printf("\n 1021-Delhi to Mumbai-06:30");
printf("\n 1024-Delhi to Kolkata-06:30");
printf("\n 1098-Delhi to Amritsar-14:30");
printf("\n 1987-Delhi to Bengaluru-18:00");
printf("\n 1576-Delhi to Chennai-20:00\n");
}
void form()
{
int i,n;
printf("\n Enter the number of tickets: \t");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter name: ");
scanf("%s",s[i].name);
printf("\n Enter age: ");
scanf("%d",&s[i].age);
printf("\n Enter phone number: ");
scanf("%ld",&s[i].phno);
printf("Enter address: ");
scanf("%s",s[i].address);
printf("\n Enter your security number code: ");
scanf("%d",&s[i].ticketno);
}
printf("Your ticket is confirmed");
}
void reserve()
{
int code, total_seats=100, reserved=0, class;
if(reserved<total_seats)
{
reserved++;
printf("Enter the train code: ");
scanf("%d",&code);
if(code==1021||code==1024||code==1098||code==1987||code==1576)
{
printf("\n 1.First class (fare Rs.1500)");
printf("\n 2.Second class (fare Rs.800)");
printf("\n 3.Sleeper class (fare Rs.500)\n");
scanf("%d",&class);
if(class==1)
{
printf("Your fare is Rs.1500\n");
form();
menu();
}
else if (class==2)
{
printf("\n Your fare is RS.800\n");
form();
menu();
}
else if(class==3)
{
printf("\n Your fare is Rs.500\n");
form();
menu();
}
else
{
printf("Enter valid choice(1,2 or 3)");
menu();
}
}
else
printf("WARNING! YOU HAVE ENTERED THE WRONG CODE");
}
}
void cancel()
{
int ticket;
char ch;
int i,n;
printf("\n Enter the number of tickets:\t");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the ticket number: ");
scanf("%d",&ticket);
if(ticket==s[i].ticketno)
{
printf("Your ticket is cancelled");
}
else
{
printf("Ticket number is invalid");
menu();
}
}
}
void main()
{
printf("\n WELCOME TO RAILWAY RESERVATION SYSTEM \n");
menu();
}
2.RESERVE A TICKET
3.CANCEL A TICKET
4.EXIT
Enter your choice: 1
CODE-ROUTE-TIMINGS
1021-Delhi to Mumbai-06:30
1024-Delhi to Kolkata-06:30
1098-Delhi to Amritsar-14:30
1987-Delhi to Bengaluru-18:00
1576-Delhi to Chennai-20:00
RAILWAY RESERVATION SYSTEM
2.RESERVE A TICKET
3.CANCEL A TICKET
4.EXIT
Enter your choice: 2
Enter the train code: 1021
Enter name: aa
Enter age: 14
Enter phone number: 2345
Enter address: xxx
2.RESERVE A TICKET
3.CANCEL A TICKET
4.EXIT
Enter your choice: 3
2.RESERVE A TICKET
3.CANCEL A TICKET
4.EXIT
Enter your choice: 4