Ex - No.1a Programs Using Input & Output Statements

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 34

Ex.No.

1a PROGRAMS USING INPUT & OUTPUT STATEMENTS

SAMPLE PROGRAMS:
P1: Get & Display a single character using getch()
#include<stdio.h>
#include<conio.h>
main()
{
char c1;
c1=getch();
putch(c1);
}

P2: Get & Display a single character using getche()


#include<stdio.h>
#include<conio.h>
main()
{
char c1;
c1=getche();
putch(c1);
}

P3: Get & Display a single character using getchar()


#include<stdio.h>
#include<conio.h>
main()
{
char c1;
c1=getchar();
putch(c1);
}

P4: Get & Display a More characters using gets()


#include<stdio.h>
#include<conio.h>
main()
{
char name[20];
gets(name);
puts(name);
}
P5: Get & Display a student’s regno,Name and GPA using scanf & printf
#include<stdio.h>
#include<conio.h>
void main()
{
int regno;
char name[25];
float GPA;
printf(“\nEnter a Student Regno,Name & GPA\n”);
scanf(“%d%s%f”,&regno,name,&GPA);
printf(“\n------------------------------------------------------\n”);
printf(“\n\t\t REG.NO \t\t NAME \t\t GPA \t\t\n”);
printf(“\n------------------------------------------------------\n”);
printf(“\t\t%d\t\t%s\t\t%f\t\t”,regno,name,GPA);
}
OUTPUT:
P1:
A

P2:
AA

P3:
Apple
A

P4:
Apple is good
Apple is good

P5:

Enter a Student Regno,Name & GPA


4001 TS 8.4
------------------------------------------------------
REG.NO NAME GPA
------------------------------------------------------
4001 TS 8.400000

RESULT:
Thus the program for I/O statements has been written & executed successfully.
EX.NO. :1b PROGRAMS USING EXPRESSIONS

PROGRAM:
P1: Arithmetic Operators
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr(); //to clear the output screen
a=20;
b=5;
printf(“Addition of %d + %d is = %d\n”,a,b,a+b);
printf(“Subtraction of %d - %d is = %d\n”,a,b,a-b);
printf(“Multiplication of %d * %d is = %d\n”,a,b,a*b);
printf(“Division of %d / %d is = %d\n”,a,b,a/b);
printf(“Modulo Division of %d % %d is = %d\n”,a,b,a%b);
getch();
}
P2: Bit wise Operators
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr(); //to clear the output screen
a=20;
b=5;
printf(“Bit wise OR of %d | %d is = %d\n”,a,b,a|b);
printf(“Bit wise AND of %d & %d is = %d\n”,a,b,a&b);
printf(“One’s complement of %d is = %d\n”,a,~a );
getch();
}
P3: Relational Operators
#include<stdio.h>
#include<conio.h>
main()
{
int a,b;
clrscr(); //to clear the output screen
a=20;
b=5;
printf(“a>b : %d\n”,a>b);
getch();
}
P4: Logical, Ternary Operators, Increment & decrement Operators
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr(); //to clear the output screen
a=20;
b=5;
c=(a>b)?(a+b):(a-b);
printf(“The value of c is =%d\n ”, c );
d=(a>b)&&(a>c);
printf(“The value of d is =%d\n ”, d);
d=(b<c)&&(a<c);
printf(“The value of d is =%d\n ”, d);
printf(“a++ & ++b is = %d,%d\n”,++a,b++);
getch();
}
OUTPUT:
Addition of 20 + 5 is = 25
Subtraction of 20 - 5 is = 15
Multiplication of 20 *5 is = 100
Division of 20 / 5 is = 4
Modulo Division of 20 % %d is = %d

Bit wise OR of 20 | 5 is = 21
Bit wise AND of 20 & 5 is = 4
One's complement of 20 is = -21

a>b:1

The value of c is =25


The value of d is =0
The value of d is =1
a++ & ++b is = 21,5

RESULT:
Thus the C program for expressions has been written & executed successfully.
EX.NO.:2 FINDING ODD OR EVEN

SAMPLE PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int Num,remainder;
printf(“Enter a Number”);
scanf(“%d”,&Num);
remainder= Num%2; ;
if ( remainder==0)
printf(“%d is EVEN”,Num );
else
printf(“%d is ODD”,Num);
getch();
}

OUTPUT:

Enter a Number
10
10 is EVEN

Enter a Number
3
3 is ODD

RESULT:
Thus the program is written & executed Successfully.
EX.NO.:3 FINDING LEAP YEAR OR NOT

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int year;
printf(“Enter a year”);
scanf(“%d”,&year);
if( (year%100 !=0 && year%4 ==0) || year%400==0)
printf(“%d is a LEAP YEAR”, year );
else
printf(“%d is NOT a LEAP YEAR”, year);
}

OUTPUT:

Enter a year1900
1900 is NOT a LEAP YEAR

Enter a year2000
2000 is a LEAP YEAR

RESULT:
Thus the program is written & executed Successfully.
EX.NO.:4 CREATING A CALCULATOR

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int option,a,b,c=0;
clrscr();
printf(“Enter the option”);
printf(“\n 1.ADD \t 2.SUB \t 3.MUL \t 4.DIV \t 5.SQUARE\n”);
scanf(“%d”,&option);
switch(option)
{
case 1:
printf(“Enter two values”);
scanf(“%d%d”,&a,&b);
c=a+b;
break;
case 2:
printf(“Enter two values”);
scanf(“%d%d”,&a,&b);
c=a-b;
break;

case 3:
printf(“Enter two values”);
scanf(“%d%d”,&a,&b);
c=a*b;
break;

case 4:
printf(“Enter two values”);
scanf(“%d%d”,&a,&b);
c=a/b;
break;

case 5:
printf(“Enter two values”);
scanf(“%d”,&a);
c=a*a;
break;

default:
printf(“Choose the correct option\n”);
}
printf(“Result is =%d”,c);
getch();
}

OUTPUT:

Enter the option


1.ADD 2.SUB 3.MUL 4.DIV 5.SQUARE
1
Enter two values 10 20
Result is=30

Enter the option


1.ADD 2.SUB 3.MUL 4.DIV 5.SQUARE
5
Enter a value2
Result is=4

Enter the option


1.ADD 2.SUB 3.MUL 4.DIV 5.SQUARE
9
Choose the correct option
Result is=0

RESULT:
Thus the program is written & executed Successfully.
EX.NO.: 5 FINDING ARMSTRONG NUMBER

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int N,A,digit,cube,sum=0;
printf(“Enter a Number”);
scanf(“%d”,&N);
A=N;
while(N>0 )
{
digit=N%10;
cube=(digit*digit*digit);
sum=sum+cube;
N=N/10;
}
if ( sum==A)
printf(“%d is Armstrong Number”, );
else
printf(“%d is not an Armstrong Number”, );
getch();
}

OUTPUT:

Enter a Number
153
153 is Armstrong Number

Enter a Number
123
123 is not an Armstrong Number

RESULT:
Thus the program is written & executed Successfully.
Ex:6 FINDING SUM OF WEIGHTS & SORTING THE ELEMENTS BASED ON
WEIGHTS

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
double cubroot,cube;
int a[10]={ },w[10],i,j,N=6,count=0,A,sum=0;
clrscr();
for(i=0;i<N;i++)
{
A=a[i];
w[i]=0;
cubroot=ceil(pow(A,(1/3)));
cube=cubroot*cubroot*cubroot;
if(A==cube)
w[i]=w[i]+5;
if(A%4==0 && A%6==0)
w[i]=w[i]+4;

for(j=2;j<=A;j++)
{
if(A%j == 0)
count=count+j;
}
if(A==1 || A==count)
w[i]=w[i]+3;

}
printf("\n\nELEMENT\t\t\t\tWEIGHT \n\n");
for(i=0;i<N;i++)
{
printf("%d \t\t\t\t%d\n\n",a[i],w[i]);
sum=sum+w[i];
}
printf("The sum of Weights is = sum);
printf("Sorting of the Elements based on weight values\n");
for(i=0;i<N;i++)
for(j=0;j<N;j++)
if(w[j]>w[j+1])
{
t=w[j];
w[j]=w[j+1];
w[j+1]=t;

k=a[j];
a[j]=a[j+1];
a[j+1]=k;
}

for(i=0;i<N;i++)
{
printf("<%d,%d> \t",a[i],w[i]);
}

getch();
}

OUTPUT:

RESULT:
Thus the program is written & executed Successfully.
Ex.No:7 FINDING THE PERSONS HAVING ABOVE AVERAGE HEIGHT

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
float height[20],avg;
int N,i,sum=0,count=0;
clrscr();
printf("Enter the no. of persons");
scanf("%d",&N);
printf("Enter the persons height on eby one\n");
for(i=0;i<N;i++)
{
scanf("%f",&height[i]);
sum=sum+height[i];
}
avg=sum/N;
for(i=0;i<N;i++)
{
if(height[i]>avg)
{ count=count+1; }
}
printf("Totally %d Persons are having above average height",count);
getch();
}

OUTPUT:

RESULT:
Thus the program is written & executed Successfully.
EX.NO.: 8 FINDING BODY MASS INDEX USING ARRAY
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
float height[20],weight[20],BMI[20],HIM[20];
int i,j,N;
clrscr();
printf("\nEnter the No.of the elements\n");
scanf("%d",&N);
printf("\n Enter the Height & Weight values\n");
for(i=0;i<N;i++)
{
scanf("%f%f",&height[i],&weight[i]);
HIM[i]=height[i]/100;
}

printf("\nPerson\tHeight\tWeight\tBMS\n");
for(i=0;i<N;i++)
{
BMI[i]=weight[i]/(HIM[i]*HIM[i]);
printf("\n%d\t%.2f\t%.2f\t%.2f\n",(i+1),HIM[i],weight[i],BMI[i]);
}
getch();
}
OUTPUT:
Enter No. of the elements
3

Enter the Height & Weight values


152 58
122 28
135 40

Person Height Weight BMS

1 1.52 58.00 25.10

2 1.22 28.00 18.81

3 1.35 40.00 21.95

RESULT:
Thus the program is written & executed Successfully.

EX.NO.: 9 REVERSE STRING WITHOUT CHANGING THE POSITION OF SPECIAL


CHARACTER
PROGRAM:
#include<stdio.h>
#include<conio.h>
void reverse(char *);
void main()
{
char str[50];
clrscr();
printf("Enter a string\n");
scanf("%s",str);
printf( "Input string: %s\n",str);
reverse(str);
printf("Output string:%s\n",str);
getch();
}
void reverse(char *str)
{
int r = strlen(str) - 1, f= 0;
char t;
while (f < r)
{
if(isalnum(str[f])!=0 && isalnum(str[r])!=0)
{
t=str[r];
str[r]=str[f];
str[f]=t;
f++;
r--;
}
else if(isalnum(str[f])!=0 && isalnum(str[r])==0)
{ r--; }

else if(isalnum(str[f])==0 && isalnum(str[r])!=0)


{ f++; }
else
{
f++;
r--;
}

}
}
OUTPUT:
Enter a string:
a@gh%;j
Input string: a@gh%;j
Output string:j@hg%;a

RESULT:
Thus the program is written & executed Successfully.

Ex.No.: 10 SORT THE LIST OF NUMBERS USING PASS BY REFERENCE


PROGRAM:
#include<stdio.h>
void sort(int *,int);
void main()
{
int a[20],N,i;
printf("Enter the no. of elements\n");
scanf("%d",&N);
printf("Enter the Elements one by one\n");
for(i=0;i<N;i++)
scanf("%d",&a[i]);
sort(a,N);

printf("\n Sorted Order\n");


for(i=0;i<N;i++)
printf("%d\t",a[i]);
getch();
}

void sort(int *x,int n)


{
int t,i,j;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if(*(x+j) > *(x+(j+1)))
{
t=*(x+j);
*(x+j) =*(x+(j+1));
*(x+(j+1))=t;
}
}
}
}

OUTPUT:
Enter the no. of elements
3
Enter the Elements one by one
100
3
45
Sorted Order
3 45 100

RESULT:
Thus the program is written & executed successfully

Ex.No:11
NUMBER SYSTEM CONVERSION USING USER DEFINED FUNCTION
PROGRAM:
#include<stdio.h>
void oct(int);
void hex(int);
void bin(int);
void main()
{
int dec;
clrscr();
printf("enter a decimal value\n");
scanf("%d",&dec);
oct(dec);

bin(dec);
hex(dec);
getch();
}

void oct(int x)
{
int octalNum[100],n,i,j;
n=x;
i = 0;
while (n != 0)
{
octalNum[i] = n % 8;
n = n / 8;
i++;
}

for (j = i - 1; j >= 0; j--)


printf("%d",octalNum[j]);
printf("\n\n");
}

void bin(int x)
{
int binaryNum[1000];

// counter for binary array


int i = 0,n=x,j;
while (n > 0)
{

// storing remainder in binary array


binaryNum[i] = n % 2;
n = n / 2;
i++;
}
// printing binary array in reverse order
for (j = i - 1; j >= 0; j--)
printf("%d",binaryNum[j]);
printf("\n\n");
}

void hex(int x)
{
int n=x,j=0,i,remainder,hexadecimal[20];

while (n != 0)
{
remainder = n % 16;
if (remainder < 10)
hexadecimal[j++] = 48 + remainder;
else
hexadecimal[j++] = 55 + remainder;
n =n / 16;
}

// display integer into character


for (i = j-1; i >=0; i--)
printf("%c", hexadecimal[i]);
}

OUTPUT:
enter a decimal value
15
17

00000000000001111

RESULT:
Thus the program is written & executed successfully

Ex. No. : 12 FINDING NUMBER OF WORDS AND CAPITALIZE THE FIRST WORD
PROGRAM:
#include<stdio.h>
void main()
{
char text[200],t[100],ch;
int n,i,words=0;
clrscr();
printf("Enter a text\n ");
gets(text);
i=0;
text[i]=toupper(text[i]);
while((ch=text[i])!='\0')
{

if(ch==' '||ch=='.')
words++;
if(ch=='.')
text[i+1]=toupper(text[i+1]);
i++;

}
printf("\n\tSentence =%s",text);
printf("\n\tNo. of Words=%d",words+1);
getch();
}

OUTPUT:
Enter a text
hai welcome to all.good morning.how are you?
Sentence =Hai welcome to all.Good morning.How are you?
No. of Words=9

RESULT:
Thus the program is written & executed successfully.

EX.NO.: 13 EMPLOYEE SALARY SLIP

PROGRAM:
#include<stdio.h>
struct employee
{
char ename[25];
int eid;
char edes[20];
char edept[20];
int esal;
};
void salaryslip(struct employee *e,int n)
{
int id,i;
printf("\nEnter the employee id to generate the salary slip\n");
scanf("%d",&id);
for(i=0;i<=n;i++)
{
if((e+i)->eid==id)
{
printf("\n----------------------------------------------------------------------");
printf("\nNAME\tDEPARTMENT\tDESIGNATION\tSALARY");
printf("\n----------------------------------------------------------------------");
printf("\n%s\t\t%s\t\t%s\t\t%d",(e+i)->ename,(e+i)->edept,(e+i)->edes,(e+i)->esal);
printf("\n----------------------------------------------------------------------");
}
}
}
void main()
{
struct employee emp[20],*emp1;
int m,i;
printf("Enter the no. of employee details");
scanf("%d",&m);
printf("\nEnter employee id, name, department, designation & salary\n");
for(i=0;i<m;i++)
scanf("%d%s%s%s
%d",&emp[i].eid,&emp[i].ename,&emp[i].edes,&emp[i].edept,&emp[i].esal);

emp1=emp;
salaryslip(emp1,m);
getch();
}

OUTPUT:
Enter the no. of employee details2
Enter employee id, name, department, designation & salary
101 A CSE AP 20000
202 B ECE AP 25000
Enter the employee id to generate the salary slip101
-----------------------------------------------------------------------------
NAME DEPARTMENT DESIGNATION SALARY
-----------------------------------------------------------------------------
A CSE AP 20000
-----------------------------------------------------------------------------

RESULT:

Thus the program is written & executed successfully.

EX.NO.: 14 STUDENTS INTERNAL MARK SHEET

.
PROGRAM:
#include<stdio.h>

struct student
{
char name[20];
int t[15][15];
int mark[5];
};
void internal( struct student);
void main()
{
struct student s;
int j,k;
clrscr();
for(j=0;j<3;j++)
{
printf("Enter the internal test %d marks for five subjects:\t",j+1);
for(k=0;k<5;k++)
scanf("%d",&s.t[j][k]);
}

internal(s);
getch();
}
void internal(struct student s1)
{
int i,j,k,sum[20],c=0;
for(j=0;j<5;j++)
{
c=0;
for(k=0;k<3;k++)
{
c=c+s1.t[k][j];
}
s1.mark[j]=((c/3)/5);
}
for(i=0;i<5;i++)
printf("\nSubject %d Internal Mark (max. marks 20)= %d",i+1,s1.mark[i]);
}

OUTPUT:
Enter the internal test 1 marks for five subjects: 10 10 10 10 10
Enter the internal test 2 marks for five subjects: 70 80 90 10 80
Enter the internal test 3 marks for five subjects: 90 90 90 90 90

Subject 1 Internal Mark (max. marks 20)= 11


Subject 2 Internal Mark (max. marks 20)= 12
Subject 3 Internal Mark (max. marks 20)= 12
Subject 4 Internal Mark (max. marks 20)= 7
Subject 5 Internal Mark (max. marks 20)= 12

RESULT:

Thus the program is written & executed successfully.

Ex.No: 15 SEQUENTIAL ACCESS


PROGRAM:
#include <stdio.h>

void insert();
void count();

int main(void)
{
int choice = 0;
while (choice != 3)
{
printf("\n1 insert records\n");
printf("2 Count min balance holders\n");
printf("3 Exit\n");
printf("Enter choice:");
scanf("%d", &choice);
switch(choice)
{
case 1: insert(); break;
case 2: count(); break;
}
}
}

void insert()
{
unsigned int account,i;
char name[30];
double balance;
FILE* cfPtr;

if ((cfPtr = fopen("clients.dat", "w")) == NULL) {


puts("File could not be opened");
}
else {
int records,i=0;
printf("Enter the No. of records ");
scanf("%d", &records);
while (i<records)
{
printf("Enter the account, name, and balance.");
scanf("%d%29s%lf", &account, name, &balance);
fprintf(cfPtr, "%d %s %.2f\n", account, name, balance);
i++;
}
fclose(cfPtr);
}

void count()
{

unsigned int account;


char name[30];
double balance;
float minBal = 5000.00;
int count = 0;
FILE *cfPtr;
if ((cfPtr = fopen("clients.dat", "r")) == NULL)
printf("File could not be opened");
else
{
printf("%-10s%-13s%s\n", "Account", "Name", "Balance");
fscanf(cfPtr, "%d%29s%lf", &account, name, &balance);

while (!feof(cfPtr))
{
if (balance < minBal)
{
printf("%-10d%-13s%7.2f\n", account, name, balance);
count++;
}
fscanf(cfPtr, "%d%29s%lf", &account, name, &balance);
}

fclose(cfPtr);
printf("The number of account holders whose balance is less than the minimum balance: %d",
count);
}

OUTPUT:
1 insert records
2 Count min balance holders
3 Exit
Enter choice:1
Enter the No. of records 2
Enter the account, name, and balance.1001 A 10000
Enter the account, name, and balance.1002 B 300

1 insert records
2 Count min balance holders
3 Exit
Enter choice:2
Account Name Balance
1002 B 300.00
The number of account holders whose balance is less than the minimum balance: 1
1 insert records
2 Count min balance holders
3 Exit
Enter choice:

RESULT:

Thus the program is written & executed successfully.

Ex.No: 16 TOWERS OF HANOI


PROGRAM:

#include <stdio.h>

void towers(int,char,char,char);

void main()
{
int num;
printf("Enter the number of disks : ");
scanf("%d",&num);
printf("The sequence of moves involved in the Tower of Hanoi are :\n");
towers(num,'A','C','B');
}

void towers(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);
}

OUTPUT:
Enter the number of disks : 3
The sequence of moves involved in the Tower of Hanoi are :

Move disk 1 from peg A to peg C


Move disk 2 from peg A to peg B
Move disk 1 from peg C to peg B
Move disk 3 from peg A to peg C
Move disk 1 from peg B to peg A
Move disk 2 from peg B to peg C
Move disk 1 from peg A to peg C

RESULT:

Thus the program is written & executed successfully.

Ex.No: 17 RANDOM ACCESS FILE

PROGRAM:
#include<stdio.h>
struct teledir
{
int no;
char name[3];
};
void main()
{
struct teledir t1,t2,t3;
int i,n,p,newp;
FILE *fp,*fp1;
clrscr();
fp=fopen("td.txt","w");
printf("Enter the no of records\n");
scanf("%d",&n);
printf("Enter the record\n");
for (i=0;i<n;i++)
{
scanf("%d%s",&t1.no,t1.name);
fwrite(&t1,sizeof(struct teledir),1,fp);
}
fclose(fp);
fp=fopen("td.txt","r");

while(fread(&t2,sizeof(struct teledir),1,fp)!=NULL)
{
printf("\t%d%s\n",t2.no,t2.name);
}

printf("Enter number to be modified & a new number\n");


scanf("%d%d",&p,&newp);
fclose(fp);
fp=fopen("td.txt","r+");
fp1=fopen("td1.txt","w");
while(fread(&t2,sizeof(struct teledir),1,fp)!=NULL)
{
if(t2.no==p)
{
fseek(fp,-sizeof(struct teledir),SEEK_CUR);
t3.no=newp;
strcpy(t3.name,t2.name);
fwrite(&t3,sizeof(struct teledir),1,fp1);
}
else
{
fwrite(&t2,sizeof(struct teledir),1,fp1);
}
}
fclose(fp);
fclose(fp1);
fp=fopen("td1.txt","r");
while(fread(&t3,sizeof(struct teledir),1,fp)!=NULL)
{
printf("\t%d\t%s\n",t3.no,t3.name);
}

fclose(fp);

getch();
}

OUTPUT:
Enter the no of records
3
Enter the record
111 abc
222 xyz
333 nmo
111abc
222xyz
333nmo
Enter number to be modified & a new number
222 9898
111 abc
9898 xyz
333 nmo

RESULT:

Thus the program is written & executed successfully.

You might also like