Professional Documents
Culture Documents
Ex - No.1a Programs Using Input & Output Statements
Ex - No.1a Programs Using Input & Output Statements
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:
AA
P3:
Apple
A
P4:
Apple is good
Apple is good
P5:
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
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:
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
RESULT:
Thus the program is written & executed Successfully.
}
}
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.
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++;
}
void bin(int x)
{
int binaryNum[1000];
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;
}
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.
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:
.
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
RESULT:
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;
void count()
{
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:
#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');
}
OUTPUT:
Enter the number of disks : 3
The sequence of moves involved in the Tower of Hanoi are :
RESULT:
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);
}
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: