THE SUM AND AVERAGE OF N NUMBER OF MARKS OF A STUDENT
# include<conio.h>
# include<stdio.h>
main()
{
int tam,eng,mat,phy,che,bio,tot;
float avg;
clrscr();
printf("enter the six marks");
scanf("%d%d%d%d%d%d",&tam,&eng,&mat,&phy,&che,&bio);
tot=tam+eng+mat+phy+che+bio;
avg=tot/12;
printf("the total is %d",tot);
printf("the average is %f",avg);
getch();
}
OUTPUT:
Enter the six marks
138
138
85
93
112
111
The total is 677
the average is 56.00
BIGEST NUMBER
# include<stdio.h>
# include<conio.h>
main()
{
int fno,sno,tno;
clrscr();
printf("enter any three numbers");
scanf("%d%d%d",&fno,&sno,&tno);
if(fno>sno&&fno>tno)
{
printf("the greatest number is=>%d",fno);
}
else if(sno>tno)
{
printf("the greatest number is=>%d",sno);
}
else
{
printf("the greatest number is=>%d",tno);
}
getch();
}
OUTPUT:
Enter the three numbers
55
76
90
The greatest number is 90
THE GIVEN NUMBER IS PRIME OR NOT AND DISPLAY THE N RANGE OF
PRIME
# include<stdio.h>
# include<conio.h>
main()
{
int n,c=2,i=3,count;
clrscr();
printf("enter a number to check if it is prime\n");
scanf("%d",&n);
for(c=2;c<=n-1;c++)
{
if(n%c==0)
{
printf("%d is not a prime.\n",n);
break;
} }
if(c==n)
printf("%d is a prime.\n",n);
printf("enter the number of prime numbers required\n");
scanf("%d",&n);
if(n>1)
{
printf("first %d prime numbers are:\n",n);
printf("2\n");
}
for(count=2;count<=n;)
{
for(c=2;c<=i-1;c++)
{
if(i%c==0)
break;
}
if(c==i)
{
printf("%d\n",i);
count++;
}
i++;
}
getch();
}
OUTPUT:
Enter a number to check if it is prime
7
7is prime
Enter the number of prime number required
5
First 5 prime numbers are
2
3
5
7
11
OPERATION STACK
# include<stdio.h>
# define MAXSIZE 5
struct stack
{
int stk[MAXSIZE];
int top;
};
typedef struct stack STACK;
STACK s;
void push(void);
int pop(void);
void display(void);
void main()
{
int choice;
int option=1;
s.top=-1;
printf("STACK OPERATION\n");
while(option)
{
printf("-------------------------------\n");
printf(" 1 --> PUSH \n");
printf(" 2 --> POP \n");
printf(" 3 --> DISPLAY \n");
printf(" 4 --> EXIT \n");
printf("-------------------------------\n");
printf("enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
return;
}
fflush (stdin);
printf("do you want to continue(type 0 or 1)?\n");
scanf("%d",&option);
}
}
void push()
{
int num;
if(s.top==(MAXSIZE-1))
{
printf("stack is full\n");
return;
}
else
{
printf("enter the element to be pushed\n");
scanf("%d",&num);
s.top=s.top+1;
s.stk[s.top]=num;
}
return;
}
int pop()
{
int num;
if(s.top==1)
{
printf("stack is empty\n");
return(s.top);
}
else
{
num=s.stk[s.top];
printf("poped element is =%dn",s.stk[s.top]);
s.top=s.top-1;
}
return(num);
}
void display()
{
int i;
if(s.top==-1)
{
printf("stack is empty\n");
return;
}
else
{
printf("\n the status of the stack is\n");
for(i=s.top;i>=0;i--)
{
printf("%d\n",s.stk[i]);
}
}
printf("\n");
}
OUTPUT:
Enter your choice:
5
Do you want to continue (type 0 or 1)
1
The stack is
1
11
111
1111
11111
QUEUE OPERATION
# include<stdio.h>
# define MAX 50
int queue_array[MAX];
int rear=-1;
int front=-1;
main()
{
int choice;
while(1)
{
printf("1.insert element to queue \n");
printf("2.delete element from queue \n");
printf("3.display all element of queue \n");
printf("4.quit\n");
printf("enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("wrong choice\n");
}
}
}
insert()
{
int add_item;
if(rear==MAX-1)
{
if(front==-1)
front=0;
printf("insert the element in queue:");
scanf("%d",&add_item);
rear=rear+1;
queue_array[rear]=add_item;
}
}
delete()
{
if(front==-1||front>rear)
{
printf("queue underflow\n");
return;
}
else
{
printf("element deleted from queue is:%d\n",queue_array[front]);
front=front+1;
}
}
display()
{
int i;
if(front==-1)
printf("queue is empty\n");
else
{
printf("queue is:\n");
for(i=front;i<=rear;i++)
printf("%d",queue_array[i]);
printf("\n");
}
}
OUTPUT:
Enter the size of queue
2
1. insert
2. delete
3. print
4. exit
Enter your choice:
1
Enter the item
5
5 is inserted
FILE OPERATION
# include<stdio.h>
# include<conio.h>
struct student
{
char name[50];
int height;
};
void main()
{
struct student stud1[5],stud2[5];
FILE*fptr;
int i;
clrscr();
fptr=fopen("file.txt","wb");
for(i=0;i<5;++i)
{
fflush(stdin);
printf("enter name:");
gets(stud1[i]:Name)
printf("enter height:");
scanf("%d",&stud1[i].height);
}
fwrite(stud1,sizeof(stud1),1,fptr);
fclose(fptr);
fptr=fopen("file.txt","rb");
fread(stud2,sizeof(stud2),1,fptr);
for(i=0;i<5;++i)
{
printf("\n\t name: %s\theight:%d",stud2[i].name,stud2[i].height);
}
fclose(fptr);
getch();
}
OUTPUT:
Enter name: karthik
Enter height: 189
Enter name: ahamed
Enter height: 186
Enter name: shankar
Enter height: 185
Enter name: aravind
Enter height: 183
Enter name: tamil
Enter height: 184
Name: karthik height: 189
Name: ahamed height: 186
Name: Shankar height: 185
Name: aravind height: 183
Name: tamil height: 184
STRING FUNCTION
# include<stdio.h>
# include<conio.h>
# include<string.h>
void main()
{
char str1[20],str2[20],str3[20];
int len,r;
clrscr();
printf("\n enter the first string=>");
scanf("%s",str1);
printf("\n enter the second string=>");
scanf("%s",str2);
r=strcmp(str1,str2);
if(r==0)
printf("\n two strings are equal\n");
else
printf("\n two strings are not equal\n");
strcpy(str3,str1);
printf("strcpy(str3,str1):%s\n",str3);
strcat(str1,str2);
printf("strcat(str1,str2):%s\n",str1);
len=strlen(str1);
printf("strlen(str1):%d\n",len);
strupr(str1);
printf("\n the upper case is%s",str1);
strlwr(str1);
printf("\n the lower case is%s",str1);
getch();
}
OUTPUT:
Enter the first string =bsc
Enter the second string =it
Two strings are not equal
Strcpy(str3,str1)
Bsc
Strcat(str1,str2)
Bscit
Strlen(str1)=5
The upper case is BSCIT
The lower case is bscit
SORT AND STORE THE ELEMENT USING ARRAY
# include<stdio.h>
# include<conio.h>
main()
{
int first[3]={22,33,44};
int second[5];
int i,j,s=0;
clrscr();
printf("array a value are\n");
for(i=0;i<3;i++)
{
printf("\n the first array values are %d",first[i]);
}
printf("\n enter five value for second array %d");
for(j=0;j<=4;j++)
{
scanf("%d",&second[j]);
}
for(j=0;j<=4;j++);
{
s=s+second[j];
printf("\n the second array values are %d",second[j]);
}
printf("\n the sum of second array is=>%d",s);
getch();
}
OUTPUT:
Array a values are
The first array values are
22
33
44
Enter the value to second array
2
1
2
3
4
The second array is 12
POINTERS
# include<stdio.h>
# include<conio.h>
void swap(int *n1,int *n2)
void main()
{
int num1,num2;
clrscr();
printf("enter any two numbers\n");
scanf("%d%d",&num1,&num2);
swap(&num1,&num2);
printf("number1=%d\n",num1);
printf("number2=%d",num2);
getch();
}
void swap(int *n1,int *n2)
{
int temp;
temp=*n1;
*n1=*n2;
*n2=temp;
}
OUTPUT:
Enter the two numbers
Num1=50
Num2=30
The number is
Num1=30
Num2=50
STRUCTURES
# include<stdio.h>
struct student
{
char name[50];
int roll;
float marks;
}s;
int main()
{
printf("enter information:\n");
printf("enter name:");
scanf("%s",s.name);
printf("enter roll number:\n");
scanf("%d",&s.roll);
printf("enter mark:");
scanf("%f",&s.marks);
printf("displaying information:\n");
printf("name:");
puts(s.name);
printf("roll number:%d\n",s.roll);
printf("marks:%1f\n",s.marks);
return 0;
}
OUTPUT:
Enter the name :karthi
Enter the roll no:n7bit0016
Enter the marks:
110
120
140
160
170
180
The average mark is :76.8
RECURSIVE FUNCTION
# include<stdio.h>
# include<conio.h>
int factorial(unsigned int i)
{
if(i<=1)
return 1;
}
return i*factorial(i-1);
}
void main()
{
int i;
clrscr();
printf("enter the value for factorial");
scanf("%d",&i);
printf("factorial of %d is %d\n",i,factorial(i));
getch();
}
OUTPUT:
Enter the factorial
3
The factorial of 3 is 6
BINARY SEARCH
# include<stdio.h>
# include<conio.h>
void main()
{
int array[10];
int i,j,num,temp,keynum;
int low,mid,high;
clrscr();
printf("enter the value of number\n");
scanf("%d",&num);
printf("enter the element one by one\n");
for(i=0;i<num;i++)
{
scanf("%d",&array[i]);
}
printf("input array elements\n");
for(i=0;i<num;i++)
{
printf("%d\n",array[i]);
}
for(i=0;i<num;i++)
{
for(j=0;j<(num-i-1);j++)
{
if(array[i]>array[j+1]);
{
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
printf("sorted array is...\n");
for(i=0;i<num;i++);
{
printf("%d\n",array[i]);
}
printf("enter the element to be searched\n");
scanf("%d",&keynum);
low=1;
high=num;
do
{
mid=(low+high)/2;
if(keynum<array[mid])
high=mid-1;
else if(keynum>array[mid])
low=mid+1;
}
while(keynum!=array[mid]&&low<=high);
if(keynum==array[mid])
{
printf("SEARCH SUCCESFUL\n");
}
else
{
printf("SEARCH FAILED\n");
}
getch();
}
OUTPUT:
Enter the value of numbers
5
Enter the element one by one
1
2
3
4
5
Input array elements
1
2
3
4
5
Sorted array is 12290
Enter the element to be searched
2
Search successful