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

2.1.

#include<stdio.h>
int main()
{
int students,team,x,y;
printf("Enter the number of students in the class\n");
scanf("%d",&students);
printf("Enter the number of teams\n");
scanf("%d",&team);
x=students%team;
y=(students-x)/team;
printf("The number of students in each team is %d and the number of students l
eft out is %d",y,x);
return 0;
}
2.2.
#include<stdio.h>
int main()
{
int x,y,z,p;
printf("The bird said:\n");
scanf("%d",&x);
y=x%10;
z=x/10;
p=y+z;
printf("Alice must go in path-%d to find her way to home",p);
return 0;
}
2.3.
#include<stdio.h>
int main()
{
int l,b;
printf("Enter the length of the ground\n");
scanf("%d",&l);
printf("Enter the width of the ground\n");
scanf("%d",&b);
printf("The length of the rope needed is %dm\nThe quantity of carpet needed is
%dsqm",2*(l+b),l*b);
return 0;
}
2.4.
#include<stdio.h>
int main()
{
int x,y,l;
printf("Enter the x-coordinate of the left bottom vertex\n");
scanf("%d",&x);
printf("Enter the y-coordinate of the left bottom vertex\n");
scanf("%d",&y);
printf("Enter the length of a side\n");
scanf("%d",&l);
x=l/2+x;
y=l/2+y;

printf("The centre of the ground is at (%d,%d)",x,y);


return 0;
}
2.5.
#include<stdio.h>
#include<math.h>
int main()
{
int x1,y1,x2,y2,x3,y3;
float a,b,c;
printf("Enter the x-coordinate of vertex
scanf("%d",&x1);
printf("Enter the y-coordinate of vertex
scanf("%d",&y1);
printf("Enter the x-coordinate of vertex
scanf("%d",&x2);
printf("Enter the y-coordinate of vertex
scanf("%d",&y2);
printf("Enter the x-coordinate of vertex
scanf("%d",&x3);
printf("Enter the y-coordinate of vertex
scanf("%d",&y3);
a=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
b=sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3));
c=sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1));
printf("Length of side AB is %.1f\n",a);
printf("Length of side BC is %.1f\n",b);
printf("Length of side AC is %.1f\n",c);
return 0;
}

A\n");
A\n");
B\n");
B\n");
C\n");
C\n");

2.6.
#include<stdio.h>
int main()
{
int m,c,x,h;
printf("Enter the value of m\n");
scanf("%d",&m);
printf("Enter the value of c\n");
scanf("%d",&c);
x=-(c/m);
h=x+c;
printf("The line equation is y=%dx+%d\nThe x intercept is %d\nThe y intercept
is %d\nThe house number is %d",m,c,x,c,h);
return 0;
}
2.7.
#include<stdio.h>
int main()
{
int x1,y1,x2,y2;
float a,b;
printf("Enter x1\n");
scanf("%d",&x1);
printf("Enter y1\n");

scanf("%d",&y1);
printf("Enter x2\n");
scanf("%d",&x2);
printf("Enter y2\n");
scanf("%d",&y2);
a=(float)(x1+x2)/2;
b=(float)(y1+y2)/2;
printf("Binoy's house is located at (%.1f , %.1f)",a,b);
return 0;
}
2.8.
#include<stdio.h>
int main()
{
int x1,x2,x3,y1,y2,y3;
float a,b;
printf("Enter x1\n");
scanf("%d",&x1);
printf("Enter y1\n");
scanf("%d",&y1);
printf("Enter x2\n");
scanf("%d",&x2);
printf("Enter y2\n");
scanf("%d",&y2);
printf("Enter x3\n");
scanf("%d",&x3);
printf("Enter y3\n");
scanf("%d",&y3);
a=(float)(x1+x2+x3)/3;
b=(float)(y1+y2+y3)/3;
printf("Dinesh's house is located at (%.1f , %.1f)",a,b);
return 0;
}
2.9.
#include<stdio.h>
int main()
{
int a,b,c,d;
printf("Enter the number of copies sold\n");
scanf("%d",&a);
printf("Enter the cost of 1 copy of the newspaper\n");
scanf("%d",&b);
printf("Enter the cost spent by the agency on 1 copy of the newspaper\n");
scanf("%d",&c);
d=((a*b)-(a*c))-100;
printf("The profit obtained is Rs.%d",d);
return 0;
}
2.10.
#include<stdio.h>
int main()
{
int s1,s2,s3,s4,s5;
float x,a;

printf("Enter sale in first month\n");


scanf("%d",&s1);
printf("Enter sale in second month\n");
scanf("%d",&s2);
printf("Enter sale in third month\n");
scanf("%d",&s3);
printf("Enter sale in fourth month\n");
scanf("%d",&s4);
printf("Enter sale in fifth month\n");
scanf("%d",&s5);
printf("Enter the average sales in 6 months\n");
scanf("%f",&x);
a=(6*x)-(float)(s1+s2+s3+s4+s5);
printf("The sale in the sixth month is Rs.%0.2f",a);
return 0;
}

3.1.
#include<stdio.h>
int main()
{
int x=0;
scanf("%d",&x);
if(x%2==0)
printf("%d is an even number",x);
else
printf("%d is an odd number",x);
return 0;
}
3.2.
#include<stdio.h>
int main()
{
int x=0;
scanf("%d",&x);
if((x%7==0) & (x%3==0))
printf("%d is divisible by both 7 and 3",x);
else
printf("%d is not divisible by both 7 and 3",x);
return 0;
}
3.3.
#include<stdio.h>
int main()
{
int x=0;
scanf("%d",&x);
if((x%2==0)|(x%3==0))
printf("yes");
else
printf("no");
return 0;
}

3.4.
#include<stdio.h>
int main()
{
int x,y;
scanf("%d %d",&x,&y);
if(x>y)
printf("%d is the maximum number",x);
else
printf("%d is the maximum number",y);
return 0;
}
3.5.
#include<stdio.h>
int main()
{
int x,y,z;
scanf("%d %d %d",&x,&y,&z);
if((x>y)&(x>z))
printf("%d is the maximum number",x);
if((y>x)&(y>z))
printf("%d is the maximum number",y);
if((z>x)&(z>y))
printf("%d is the maximum number",z);
return 0;
}
3.6.
#include<stdio.h>
int main()
{
int x;
scanf("%d",&x);
if(x%4==0)
{
if(x%400==0)
printf("%d is a leap year",x);
else if(x%100==0)
printf("%d is not a leap year",x);
else
printf("%d is a leap year",x);
}
else
printf("%d is not a leap year",x);
return 0;
}
3.7.
#include<stdio.h>
int main()
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
a=(a+b+c)/3;
printf("The grade is ");

if(a>=90)
printf("A");
else if(a>=80)
printf("B");
else if(a>=70)
printf("C");
else if(a>=60)
printf("D");
else if(a>=50)
printf("E");
else
printf("F");
return 0;
}
3.8.
#include<stdio.h>
int main()
{
char c;
scanf("%c",&c);
if((c>=65)&(c<=90))
printf("%c is uppercase letter",c);
else if((c>=97)&(c<=122))
printf("%c is lowercase letter",c);
else
printf("%c is neither an uppercase or lowercase letter",c);
return 0;
}
3.9.
#include<stdio.h>
int main()
{
int a,b;
printf("Enter the number of problems given\n");
scanf("%d",&a);
printf("Enter the number of problems solved\n");
scanf("%d",&b);
if(a%2==0)
{
if(b>=(a/2))
{
printf("IN");
return 0;
}
else{
printf("OUT");
return 0;
}
}
if(a%2!=0)
{
if(b>=(a/2)+1)
{
printf("IN");
return 0;
}

else
{
printf("OUT");
return 0;
}
}
return 0;
}

3.10
#include<stdio.h>
int main()
{
int a,b;
printf("Enter the
scanf("%d",&a);
printf("Enter the
scanf("%d",&b);
if(a<b)
{
printf("College
return 0;
}
printf("College 2
return 0;
}

year of establishment of college 1\n");


year of establishment of college 2\n");

1 is older");
is older");

3.11.
#include<stdio.h>
int main()
{
int a;
printf("Enter the Balloon's number:\n");
scanf("%d",&a);
if(a%3==0&&a%7==0)
printf("This balloon can fly to miney.");
else
printf("This balloon cannot fly to miney.");
return 0;
}
3.12.
#include<stdio.h>
int main()
{
int a;
printf("Press a number:\n");
scanf("%d",&a);
if(a>0&&a<=100&&a%2==0)
printf("Doll will sing");
else
printf("Invalid number");
return 0;
}
3.13.

#include<stdio.h>
int main()
{
int a,b,c,d;
printf("Enter the Time Limit:\n");
scanf("%d",&a);
printf("Enter the time taken by the first person:\n");
scanf("%d",&b);
printf("Enter the time taken by the second person:\n");
scanf("%d",&c);
printf("Enter the time taken by the third person:\n");
scanf("%d",&d);
if(b<c&&b<d)
{
if(b<=a)
{
printf("First person wins!!!");
return 0;
}
}
if(c<b&&c<d)
{
if(c<=a)
{
printf("Second person wins!!!");
return 0;
}
}
if(d<b&&d<c)
{
if(d<=a)
{
printf("Third person wins!!!");
return 0;
}
}
printf("No person wins:-(");
return 0;
}
3.14.
#include<stdio.h>
int main()
{
int a,b;
printf("Enter your Age:\n");
scanf("%d",&a);
printf("Enter your Weight:\n");
scanf("%d",&b);
if(a>18&&b>40)
printf("Eligible to donate.");
else
printf("Not Eligible to donate.");
return 0;
}
3.15.
#include<stdio.h>
int main()
{

int a,b;
printf("Enter the number of students in college 1\n");
scanf("%d",&a);
printf("Enter the number of students in college 2\n");
scanf("%d",&b);
if(a>b)
{ printf("College 1 is better");
return 0;
}
printf("College 2 is better");
return 0;
}
3.16.
#include<stdio.h>
int main()
{
int a,b;
printf("Enter Year of Birth\n");
scanf("%d",&a);
printf("Enter Current year\n");
scanf("%d",&b);
if(a<b)
{
printf("Your age is %d",b-a);
return 0;
}
b=b+100;
printf("Your age is %d",b-a);
return 0;
}
3.17
#include<stdio.h>
int main()
{
int a;
scanf("%d",&a);
switch(a)
{
case 1:printf("January");
break;
case 2:printf("February");
break;
case 3:printf("March");
break;
case 4:printf("April");
break;
case 5:printf("May");
break;
case 6:printf("June");
break;
case 7:printf("July");
break;
case 8:printf("August");
break;
case 9:printf("September");
break;
case 10:printf("October");
break;

case 11:printf("November");
break;
case 12:printf("December");
break;
default:printf("Invalid month");
break;
}
return 0;
}
3.18
#include<stdio.h>
int main()
{
int a,c;
char b;
scanf("%d %c %d",&a,&b,&c);
switch(b)
{
case '+':printf("The sum is %d",a+c);
break;
case '-':printf("The difference is %d",a-c);
break;
case '*':printf("The product is %d",a*c);
break;
case '/':printf("The quotient is %d",a/c);
break;
case '%':printf("The remainder is %d",a%c);
break;
default:printf("Invalid Input");
break;
}
return 0;
}
3.19.
#include<stdio.h>
int main()
{
char c;
int a,b,d,e;
scanf("%c",&c);
scanf("%d%d%d%d",&a,&b,&d,&e);
switch(c)
{
case 'A':printf("The fee to be
break;
case 'B':printf("The fee to be
break;
case 'C':printf("The fee to be
break;
case 'D':printf("The fee to be
break;
default:break;
}
return 0;
}
3.20
#include<stdio.h>

paid is Rs.%d",(a*b)+(d*e));
paid is Rs.%d",(a*b)+(d*e)+5000);
paid is Rs.%d",(a*b)+(d*e)+1500);
paid is Rs.%d",(a*b)+(d*e)+6500);

int main()
{
int a,b;
float f;
scanf("%d",&a);
scanf("%f",&f);
scanf("%d",&b);
if(a>0&&a<13)
{
if((a>=4&&a<=6)||(a>10))
{
float n=f*(float)b;
n=n+(n/5);
printf("Hotel Tariff: Rs.%.2f",n);
return 0;
}
float i=f*(float)b;
printf("Hotel Tariff: Rs.%.2f",i);
return 0;
}
printf("Invalid Input");
return 0;
}

q4.1
#include<stdio.h>
int main()
{
int a,b,i;
printf("Enter the value of a\n");
scanf("%d",&a);
printf("Enter the value of b\n");
scanf("%d",&b);
for(i=a;i<=b;i++)
{printf("%d\n",i);}
return 0;
}
q4.2
#include<stdio.h>
int main()
{
int a,b,i;
printf("Enter the value of a\n");
scanf("%d",&a);
printf("Enter the value of b\n");
scanf("%d",&b);
for(i=a;i>=b;i--)
printf("%d\n",i);
return 0;
}
q4.3
#include<stdio.h>
int main()
{
int a,b,i;

printf("Enter the value of a\n");


scanf("%d",&a);
printf("Enter the value of b\n");
scanf("%d",&b);
if(a<b)
{
for(i=a;i<=b;i++)
{
printf("%d\n",i);
}
}
else
{
for(i=a;i>=b;i--)
{
printf("%d\n",i);
}
}
return 0;
}
q4.4
#include<stdio.h>
int main()
{
int n,i,s=0,m;
printf("Enter the value of n\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the number\n");
scanf("%d",&m);
s=s+m;
}
printf("The sum is %d",s);
return 0;
}
q4.5
#include<stdio.h>
int main()
{
int n,i,s=0,m,c=0;
printf("Enter the value of n\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the number\n");
scanf("%d",&m);
if(m>=0)
{s=s+m;c++;}
}
printf("Number of positive numbers entered is %d and the sum is %d",c,s);
return 0;
}
q4.6
#include<stdio.h>
int main()
{

int n,i,c=0,m=0,a;
printf("Enter the value of n\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the number\n");
scanf("%d",&a);
if(a>=0)
c++;
else
m++;
}
printf("Number of positive numbers entered is %d and the number of negative nu
mbers entered is %d",c,m);
return 0;
}
q4.7
#include<stdio.h>
int main()
{
int n,m,i;
printf("Enter n\n");
scanf("%d",&n);
printf("Enter m\n");
scanf("%d",&m);
printf("The multiplication table of %d is\n",n);
for(i=1;i<=m;i++)
{
printf("%d*%d=%d\n",i,n,i*n);
}
return 0;
}
q4.8
#include<stdio.h>
int main()
{
int a,b;
printf("Enter the value of a\n");
scanf("%d",&a);
printf("Enter the value of b\n");
scanf("%d",&b);
while(a<=b)
{
printf("%d\n",a);
a++;
}
return 0;
}
q4.9
#include<stdio.h>
int main()
{
int a,b;
printf("Enter the value of a\n");
scanf("%d",&a);
printf("Enter the value of b\n");
scanf("%d",&b);

while(a>=b)
{
printf("%d\n",a);
a--;
}
return 0;
}
q4.10
#include<stdio.h>
int main()
{
int a,b;
printf("Enter the value of a\n");
scanf("%d",&a);
printf("Enter the value of b\n");
scanf("%d",&b);
if(a<b)
{
while(a<=b)
{
printf("%d\n",a);
a++;
}
}
else
{
while(a>=b)
{
printf("%d\n",a);
a--;
}
}
return 0;
}
q4.11
#include<stdio.h>
int main()
{
int n,m,s=0;
printf("Enter the value of n\n");
scanf("%d",&n);
while(n)
{
printf("Enter the number\n");
scanf("%d",&m);
s=s+m;
n--;
}
printf("The sum is %d",s);
return 0;
}
q4.12
#include<stdio.h>
int main()
{
int n,m,s=0,c=0;

printf("Enter the value of n\n");


scanf("%d",&n);
while(n)
{
printf("Enter the number\n");
scanf("%d",&m);
if(m>=0)
{ s=s+m;
c++;}
n--;
}
printf("Number of positive numbers entered is %d and the sum is %d",c,s);
return 0;
}
q4.13
#include<stdio.h>
int main()
{
int n,m,c=0,s=0;
printf("Enter the value of n\n");
scanf("%d",&n);
while(n)
{
printf("Enter the number\n");
scanf("%d",&m);
if(m>=0)
c++;
else
s++;
n--;
}
printf("Number of positive numbers entered is %d and the number of negative nu
mbers entered is %d",c,s);
return 0;
}
q4.14
#include<stdio.h>
int main()
{
int n,m,i=1;
printf("Enter n\n");
scanf("%d",&n);
printf("Enter m\n");
scanf("%d",&m);
printf("The multiplication table of %d is\n",n);
while(m)
{
printf("%d*%d=%d\n",i,n,i*n);
i++;
m--;
}
return 0;
}
q4.15
#include<stdio.h>
int main()
{

int n,c=0;
while(1)
{
printf("Enter the number\n");
scanf("%d",&n);
if(n%8!=0)
break;
c++;
}
printf("The number of valid numbers entered is %d",c);
return 0;
}
q5.1
#include<stdio.h>
int main()
{
int n,i;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("%d ",i*i);
}
return 0;
}
q5.2
#include<stdio.h>
int main()
{
int n,m=20,i;
scanf("%d",&n);
for(i=0;i<n;i++)
{
m=m-i;
printf("%d ",m);
}
return 0;
}
q5.3
#include<stdio.h>
int main()
{
int n,m=2,j,c;
scanf("%d",&n);
while(n)
{
c=0;
j=1;
while(j<=m)
{
if(m%j==0)
{c++;}
if(j==m)
{
if(c==2)
{
printf("%d ",m);
m++;

n--;
break;
}
else
{
m++;
break;
}
}
j++;
}
}
return 0;
}
q5.4
#include<stdio.h>
int main()
{
int n,i,m,p;
scanf("%d",&n);
p=6;
for(i=0;i<n;i++)
{
m=i*5;
p=p+m;
printf("%d ",p);
}
return 0;
}
q5.5
#include<stdio.h>
int main()
{
int n,m=1,i;
scanf("%d",&n);
for(i=0;i<n;i++)
{
m=m*3;
printf("%d ",m);
}
return 0;
}
q5.6
#include<stdio.h>
int main()
{
int n,j;
scanf("%d",&n);
while(n)
{
for(j=1;j<=n;j++)
{
printf("%d ",j);
}
printf("\n");
n--;

}
return 0;
}
q5.7
#include<stdio.h>
int main()
{
int n,i,j;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=i;j<=n;j++)
{
printf("%d ",j);
}
printf("\n");
}
return 0;
}
q5.8
#include<stdio.h>
int main()
{
int n,j;
scanf("%d",&n);
while(n)
{
for(j=n;j>0;j--)
{
printf("%d ",j);
}
printf("\n");
n--;
}
return 0;
}
q5.9
#include<stdio.h>
int main()
{
int n,i,j;
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=n;j>i;j--)
{
printf("%d ",j);
}
printf("\n");
}
return 0;
}
q5.10
#include<stdio.h>
int main()
{

int n,i,j;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%d ",j);
}
printf("\n");
}
return 0;
}
q5.11
#include<stdio.h>
int main()
{
int n,i,j;
scanf("%d",&n);
for(i=n;i>0;i--)
{
for(j=i;j<=n;j++)
{
printf("%d ",j);
}
printf("\n");
}
return 0;
}
q5.12
#include<stdio.h>
int main()
{
int n,i,j;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%d ",i);
}
printf("\n");
}
return 0;
}
q5.13
#include<stdio.h>
int main()
{
int n,i,j;
scanf("%d",&n);
for(i=n;i>0;i--)
{
for(j=n;j>=i;j--)
{
printf("%d ",i);
}
printf("\n");

}
return 0;
}
q5.14
#include<stdio.h>
int main()
{
int n,i,j;
scanf("%d",&n);
for(i=n;i>0;i--)
{
for(j=i;j>0;j--)
{
printf("%d ",i);
}
printf("\n");
}
return 0;
}
q5.15
#include<stdio.h>
int main()
{
int n,i,j;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=n;j>=i;j--)
{
printf("%d ",i);
}
printf("\n");
}
return 0;
}

6.1
#include "stdio.h"
#include "string.h"
int checkLucky(char s[100]){
int x=0,temp=0,i=0;
x = strlen(s);
while(i<x){
temp= temp+ s[i];
i++;
}
if(temp%2!=0){
return 0;
}else{
return 1;
}
}
int main(){
char s1[100];
int temp=0;
printf("Enter the input string\n");

gets(s1);
temp=checkLucky(s1);
if(temp==0){
printf("%s is not lucky",s1);
}else{
printf("%s is lucky",s1);
}
return 0;
}

6.2
#include "stdio.h"
int factorial (int n) {
int temp= 1;
while(n>1){
temp=temp*n;
n--;
}
return temp;
}
int main(){
int i;
scanf("%d",&i);
i=factorial(i);
printf("\n%d",i);
return 0;
}
6.3
#include "stdio.h"
#include "math.h"
float findDistance (int x1, int y1, int x2, int y2){
float temp =0;
x1= x1-x2;
x1=x1*x1;
y1=y1-y2;
y1=y1*y1;
x1=x1+y1;
temp = sqrt(x1);
return temp;
}
int main(){
float temp;
int x1,x2,y1,y2;
scanf("%d",&x1);
scanf("%d",&y1);
scanf("%d",&x2);
scanf("%d",&y2);
temp=findDistance(x1,y1,x2,y2);
printf("Distance between the 2 points is %.2f",temp);
return 0;
}
7.1
#include <stdio.h>

#include <stdlib.h>
int main()
{
char ch, source_file[20], target_file[20];
FILE *source, *target;
printf("Enter the input file name\n");
scanf("%s",source_file);
source = fopen(source_file, "r");
printf("Enter the output file name\n");
scanf("%s",target_file);
target = fopen(target_file, "w");
while( ( ch = fgetc(source) ) != EOF )
fputc(ch, target);
fclose(source);
fclose(target);
return 0;
}
7.2
#include<stdio.h>
#include<stdlib.h>
int main()
{
char file1[100];
int count=0;
char c,ch,ck;
FILE *fptr;printf("Enter the file name\n");
scanf("%s",file1);
fptr=fopen(file1,"r");
printf("Enter the character to be counted\n");
scanf(" %c",&c);
ck=c;
if((int)c>=65 &&(int)c<=90)
c=(int)c+32;
while((ch=getc(fptr)))
{
if((int)ch>=65 && (int)ch<=90)
ch=(int)ch+32;
if(ch==EOF)
break;
else if(ch==c)
count+=1;
}
printf("File '%s' has %d instances of letter '%c'.",file1,count,ck);
fclose(fptr);
return 0;
}
8.1
#include<stdio.h>
int main()
{
int array[20],search,c,n;
scanf("%d",&n);
for(c=0;c<n;c++)

scanf("%d",&array[c]);
scanf("%d",&search);
for(c=0;c<n;c++)
{
if(array[c]==search)
{
printf("%d is present in the array",search);
break;
}
}
if(c==n)
printf("%d is not present in the array",search);
return 0;
}
8.2
#include<stdio.h>
#include<stdlib.h>
int BinarySearch(int first,int last,int *a,int x)
{
int mid;
while(first<=last)
{
mid=(first+last)/2;
if(a[mid]<x)
{
first=mid+1;
continue;
}
if(x<a[mid])
{
last=mid-1;
continue;
}
return mid;
}
return -1;
}
int main()
{
int n,array[10],search,c,s,lower,upper,i,j,temp=0;
printf("Enter the number of elements :\n");
scanf("%d",&n);
printf("Enter the elements :\n");
for(c=0;c<n;c++)
scanf("%d",&array[c]);
printf("Enter the element to be searched :\n");
scanf("%d",&search);
lower=0;
upper=n;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;++j)
{if(array[i]>array[j])
{
temp=array[i];
array[i]=array[j];
array[j]=temp;

}
}
}
s=BinarySearch(lower,upper,array,search);
if(s==-1)
printf("The element %d is not present in the array\n",search);
else
printf("The element %d is in position %d",search,s);
return 0;
}

9.1
#include <stdio.h>
int main()
{
int a[20],n,c,d,swap,i;
printf("Enter the number of elements in the array\n");
scanf("%d",&n);
for(c=0;c<n;c++)
{
printf("Enter element %d\n",c+1);
scanf("%d",&a[c]);
}
printf("Selection sort.\n");
printf("array before sorting:\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
for(c=0;c<n;c++)
{
for(d=c+1;d<n;d++)
{
if(a[c]>a[d])
{
swap=a[c];
a[c]=a[d];
a[d]=swap;
}
}
printf("\nAfter Iteration %d\n",c+1);
for(i=0;i<n;i++)
printf("%d ",a[i]);
}
printf("\narray after sorting:\n");
for(c=0;c<n;c++)
printf("%d ",a[c]);
return 0;
}

9.2
#include <stdio.h>
int main()
{
int n, array[1000], c, d, t,j=0;

printf("Enter the number of elements in the array\n");


scanf("%d", &n);
for (c = 0; c < n; c++) {
printf("Enter element %d\n",c+1);
scanf("%d", &array[c]);
}
printf("Insertion sort.\narray before sorting:\n");
for (c = 1 ; c <= n - 1; c++) {
d = c;
if(c>1)
printf("After Iteration %d\n",c-1);
for(j=0;j<n;j++)
printf("%d ",array[j]);
printf("\n");
while ( d > 0 && array[d] < array[d-1]) {
t
= array[d];
array[d] = array[d-1];
array[d-1] = t;
d--;
}
}
printf("After Iteration %d\n",c-1);
for(j=0;j<n;j++)
printf("%d ",array[j]);
printf("\n");
printf("array after sorting:\n");
for (c = 0; c <= n - 1; c++) {
printf("%d ", array[c]);
}
return 0;
}
9.3
#include<stdio.h>
int main()
{
int n,arr[20],i,j,temp,xchanges,c;
printf("Enter the number of elements :\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter element %d\n",i+1);
scanf("%d",&arr[i]);
}
printf("Unsorted list is :\n");
for(i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
for(i=0;i<n-1;i++)
{
xchanges=0;
for(j=0;j<n-1-i;j++)
{

if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
xchanges++;
}
}
printf("\nAfter Pass %d elements are :",i+1);
for(c=0;c<n;c++)
{
printf("%d ",arr[c]);
}
if(xchanges==0)
break;
}
printf("\nSorted list is :\n");
for(i=0;i<n;i++)
printf("%d ",arr[i]);
return 0;
}
9.4
#include<stdio.h>
int main()
{
int n,i,a[20],c=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
{
if(a[i]>a[i+1])
{
c=1;
break;
}
}
if(c==1)
printf("no");
else
printf("yes");
return 0;
}
9.5
#include<stdio.h>
int main()
{
int n,i,a[20],c=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
{
if(a[i]<a[i+1])
{
c=1;

break;
}
}
if(c==1)
printf("no");
else
printf("yes");
return 0;
}
9.6
#include<stdio.h>
int main()
{
int n,i,a[20],c=0,d=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
{
if(a[i]<a[i+1])
{
c=1;
break;
}
}
if(c==1)
{
for(i=0;i<n-1;i++)
{
if(a[i]>a[i+1])
{
d=1;
break;
}
}
if(d==1)
printf("no");
else
printf("yes");
}
else
printf("yes");
return 0;
}
10.1
#include <stdio.h>
int main()
{
int array[10],max,n,i;
scanf("%d",&n);
for (i=0;i<n;i++)
scanf("%d",&array[i]);
max=array[0];
for(i=1;i<n;i++)
{
if (array[i]>max)

{
max=array[i];
}
}
printf("%d is the maximum element in the array",max);
return 0;
}
10.2
#include <stdio.h>
int main()
{
int array[10],min,n,i;
scanf("%d",&n);
for (i=0;i<n;i++)
scanf("%d",&array[i]);
min=array[0];
for(i=1;i<n;i++)
{
if (array[i]<min)
{
min=array[i];
}
}
printf("%d is the minimum element in the array",min);
return 0;
}
10.3
#include<stdio.h>
#define MAX 15
int main()
{
int arr[MAX],i,n,sum;
sum=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
sum=sum+arr[i];
}
printf("\nThe sum of the elements in the array is %d",sum);
return 0;
}
10.4
#include <stdio.h>
int main()
{
int arr1[10],arr2[10],n,i,sum[10];
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&arr1[i]);
for(i=0;i<n;i++)
scanf("%d",&arr2[i]);
for(i=0;i<n;i++)
{

sum[i]=arr1[i]+arr2[i];
}
for(i=0;i<n;i++)
printf("%d ",sum[i]);
return 0;
}
10.5
#include <stdio.h>
int main()
{
int arr1[15],arr2[15],n,i=0,com[15],comp=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&arr1[i]);
for(i=0;i<n;i++)
scanf("%d",&arr2[i]);
for(i=0;i<n;i++)
{
if(arr1[i]>=arr2[i])
com[i]=0;
else
com[i]=1;
}
for(i=0;i<n;i++)
{
if(com[i]>0)
comp++;
}
if(comp==0)
printf("Compatible");
else
printf("Incompatible");
return 0;
}
10.6
#include <stdio.h>
int main()
{
int array[15],n,i,sum=0;
scanf("%d",&n);
for (i=0;i<n;i++)
scanf("%d",&array[i]);
for(i=0;i<n;i++)
{
if(array[i]>0)
sum+=array[i];
}
printf("The sum of the positive numbers in the array is %d",sum);
return 0;
}
10.7
#include <stdio.h>
int main()
{

int array[15],n,i,sum=0;
scanf("%d",&n);
for (i=0;i<n;i++)
scanf("%d",&array[i]);
for(i=0;i<n;i++)
{
if(array[i]%2==0)
sum+=array[i];
}
printf("The sum of the even numbers in the array is %d",sum);
return 0;
}
10.8
#include <stdio.h>
int main()
{
int array[15],n,i,sum=0,sum1=0;
scanf("%d",&n);
for (i=0;i<n;i++)
scanf("%d",&array[i]);
for(i=0;i<n;i++)
{
if(array[i]%2==0)
sum+=array[i];
else
sum1+=array[i];
}
printf(" The sum of the even numbers in the array is %d\nThe sum of the odd nu
mbers in the array is %d",sum,sum1);
return 0;
}
10.9
#include <stdio.h>
int main()
{
int arr1[15],arr2[15],n,i=0,com[15],comp=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&arr1[i]);
for(i=0;i<n;i++)
scanf("%d",&arr2[i]);
for(i=0;i<n;i++)
{
if(arr1[i]==arr2[i])
com[i]=0;
else
com[i]=1;
}
for(i=0;i<n;i++)
{
if(com[i]>0)
comp++;
}
if(comp==0)
printf("yes");
else

printf("no");
return 0;
}
10.10
#include<stdio.h>
int findMax(int n,int *a)
{
int x,i;
x=a[0];
for(i=0;i<n;i++)
{
if(a[i]>x)
x=a[i];
}
return x;
}
int main()
{
int arr[20],n,i,max;
printf("Enter the number of elements in the array\n");
scanf("%d",&n);
printf("Enter the elements in the array\n");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
max=findMax(n,arr);
printf("The maximum element in the array is %d",max);
return 0;
}
10.11
#include<stdio.h>
#include<malloc.h>
#define MAX 10
int findMax(int**,int,int);
int main()
{
int **mat;
int row,col,i,j,max;
printf("Enter the number of rows in the matrix");
scanf("%d",&row);
printf("\nEnter the number of columns in the matrix");
scanf("%d",&col);
mat=(int**)malloc(MAX);
printf("\nEnter the elements in the matrix");
for(i=0;i<row;i++)
{
mat[i]=malloc(2*MAX);
for(j=0;j<col;j++)
scanf("%d",&mat[i][j]);
}
printf("\nThe matrix is");
for(i=0;i<row;i++)
{
printf("\n");
for(j=0;j<col;j++)
printf("%d ",mat[i][j]);
}

max=findMax(mat,row,col);
printf("\nThe maximum element in the matrix is %d",max);
return 0;
}
int findMax(int **a,int m,int n)
{
int i,j,max;
for(i=0,max=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]>max)
max=a[i][j];
}
}
return max;
}
10.12
#include<stdio.h>
#include<malloc.h>
#define MAX 10
int checkNull(int**,int,int);
int main()
{
int **mat;
int row,col,ans;
int i,j;
printf("Enter the number of rows in the matrix");
scanf("%d",&row);
printf("\nEnter the number of columns in the matrix");
scanf("%d",&col);
printf("\nEnter the elements in the matrix");
mat=(int**)malloc(MAX);
for(i=0;i<row;i++)
{
mat[i]=malloc(2*MAX);
for(j=0;j<col;j++)
scanf("%d",&mat[i][j]);
}
printf("\nThe matrix is");
for(i=0;i<row;i++)
{
printf("\n");
for(j=0;j<col;j++)
printf("%d ",mat[i][j]);
}
ans=checkNull(mat,row,col);
printf("\nThe matrix is");
if(ans == 0)
printf(" not");
printf(" null");
return 0;
}
int checkNull(int **a,int m,int n)
{
int max,i,j;
max=0;

for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j] >= max)
max=a[i][j];
}
}
if(max>0)
return 0;
else
return 1;
}
11.1
#include<stdio.h>
#include<stdlib.h>
struct stack {
int * a;
int top;
int maxSize;
};
void initstack(struct stack * p, int maxSize);
void push(struct stack * p, int item);
void display(struct stack p);
int pop(struct stack * p);
void printMenu();
int main() {
struct stack p;
int data,ch, data1, m;
printf("Enter the maximum size of the stack\n");
scanf("%d",&m);
initstack(&p,m);
do {
printMenu();
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch) {
case 1:
printf("Enter the element to be pushed\n");
scanf("%d",&data);
push(&p, data);
break;
case 2:
data1 = pop(&p);
if(data1 != -1000)
printf("The popped element is %d\n",data1);
break;
case 3:
printf("The contents of the stack are");
display(p);
printf("\n");
break;
default:
return 0;
}
} while(1);
return 0;

}
void printMenu()
{
printf("Choice 1 : Push\n");
printf("Choice 2 : Pop\n");
printf("Choice 3 : Display\n");
printf("Any other choice : Exit\n");
}
void initstack(struct stack * p, int maxSize) {
p->a=malloc(maxSize*sizeof(int));
//p->top=(int)malloc(sizeof(int));
//p->maxSize=(int)malloc(sizeof(int));
p->top=-1;
p->maxSize=maxSize;
}
void push(struct stack * p, int item) {
if(p->top==(p->maxSize)-1)
printf("Stack is full\n");
else
{
p->top++;
p->a[p->top]=item;
}
}
void display(struct stack p) {
int i;
if(p.top==-1)
printf(" {}");
else
for(i=0;i<=p.top;i++)
printf(" %d",p.a[i]);
}
int pop(struct stack * p) {
if(p->top==-1)
{
printf("Stack is empty\n");
return -1000;
}
else
return p->a[p->top--];
}
11.2
#include<stdio.h>
#include<string.h>
struct queue {
int contents[5];
int front;
int count;
} ;
void initQueue(struct queue * q);
void enQueue(struct queue * q, int element);
int deQueue(struct queue * q);

void display( struct queue q);


void printMenu();
int main()
{
struct queue p;
int data,ch, data1;
initQueue(&p);
do {
printMenu();
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch) {
case 1:
printf("Enter the element to be inserted/entered\n");
scanf("%d",&data);
enQueue(&p, data);
break;
case 2:
data1 = deQueue(&p);
if(data1 != -1000)
printf("The deleted element is %d\n",data1);
break;
case 3:
printf("The contents of the queue are");
display(p);
printf("\n");
break;
default:
return 0;
}
} while(1);
return 0;
}
void printMenu()
{
printf("Choice 1 : Enter element into Queue\n");
printf("Choice 2 : Delete element from Queue\n");
printf("Choice 3 : Display\n");
printf("Any other choice : Exit\n");
}
void initQueue(struct queue * q)
{
q->front=0;
q->count=0;
}
void enQueue(struct queue * q, int element)
{
int index;
index=(q->count + q->front)%5;
if(index == q->front && q->count !=0)
printf("Queue is full\n");
else {
q->count++;
q->contents[index] = element;
}
}

int deQueue(struct queue * q)


{
int elem;
if(q->count==0){
printf("Queue is empty\n");
return (-1000);
}
elem = q->contents[q->front];
q->count--;
q->front = (q->front + 1) % 5;
return (elem);
}
void display( struct queue q)
{
int i,index;
if (q.count==0)
printf(" {}");
else {
for (index = q.front,i=1; i<= q.count; i++,index++)
printf(" %d", q.contents[index%5]);
}
}
12.1
#include<stdio.h>
int main (void)
{
int tkt = 0;
char berth[6][20] = {"Lower", "Middle", "Upper", "Side Lower", "Side Upper","Inv
alid Entry"};
int index = 0;
int rem = 0;
scanf("%d\n",&tkt);
rem = tkt % 8;
index = (rem == 1) || (rem == 4) ? 0 : ((rem == 2) ||(rem == 5) ? 1 : ((rem == 3
) ||(rem == 6) ? 2 : ((rem == 7) ? 3 : (rem == 0) ? 4:5)));
printf("%s\n",berth[index]);
return 0;
}
12.2
#include <stdio.h>
int main()
{
unsigned char c,r;
int k,r1=0,i=2;
scanf("%c",&c);
c=c-64;
if(c>=1 && c<=5)
k=1;
else if(c>=6&&c<=10)
k=2;
else if(c>=11&&c<=15)
k=3;
else if(c>=16&&c<=20)
k=4;

else if(c>=21&&c<=26)
k=5;
switch(k)
{
case 1:
r=c*2;
break;
case 2:
r=(c%3)*5;
break;
case 3:
r=(c%4)*8;
break;
case 4:
r=c+10;
break;
case 5:
{
for(i=2;i<c;i++)
{
r1=c%i;
if(r1==0)
r=i*12;
}
break;
}
}
if(r==0)
r=-29;
else if(r>26)
{
r=r%26;
if(r==0)
r=26;
}
r=r+64;
printf("%c",r);
return 0;
}
12.3
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void replace (char *);
int main(void) {
char o_string[200];
gets(o_string);
replace(o_string);
printf("\n%s",o_string);
return 0;
}
void replace(char * o_string) {
char buffer[200],s_string[4][10]={"yes","you","today","why"},r_string[4][5]={"
s","u","2day","y"};
int i;
char * ch;
for(i=0;i<4;i++)
if((ch = strstr(o_string, s_string[i]))){

strncpy(buffer, o_string, ch-o_string);


buffer[ch-o_string] = 0;
sprintf(buffer+(ch - o_string), "%s%s", r_string[i], ch + strlen(s_string[
i]));
o_string[0] = 0;
strcpy(o_string, buffer);
return replace(o_string);}
}
12.4
#include<stdio.h>
#include<math.h>
int main()
{
int m,n,a,ma,na,k;
scanf("%d",&n);
scanf("%d",&m);
scanf("%d",&a);
na=round((float)n/a);
ma=round((float)m/a);
k=na*ma;
printf("\n%d",k);
return 0;
}
12.5
#include<stdio.h>
int main(){
int num,palin=0,rem,temp;
scanf("%d",&num);
while(num!=palin){
num=num+1;
temp=num;
palin=0;
while(temp!=0){
rem=temp%10;
palin=rem+palin*10;
temp=temp/10;
}
}
printf("%d",palin);
return 0;
}
12.6
#include<stdio.h>
int main()
{
int i,j,n,a[100],c;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
c=0;
for(j=0;j<i;j++)

{
if(a[i]==a[j])
{
c=1;
break;
}
}
if(c==1)
printf("0\n");
else
printf("%d\n",a[i]);
}
return 0;
}
12.7
#include<stdio.h>
int check_Anagrams(char [], char []);
int main()
{
char a[100],b[100],flag;
printf("\nEnter the first string");
gets(a);
printf("\nEnter the second string");
gets(b);
flag=check_Anagrams(a,b);
if(flag==1)
printf("\n%s and %s are anagrams",a,b);
else
printf("\n%s and %s are not anagrams",a,b);
return 0;
}
int check_Anagrams(char a[], char b[])
{
int first[26]={0},second[26]={0},c=0;
while(a[c]!='\0')
{
first[a[c]-'a']++;
c++;
}
c=0;
while(b[c]!='\0')
{
second[b[c]-'a']++;
c++;
}
for(c=0;c<26;c++)
{
if(first[c]!=second[c])
{
return 0;
}
}
return 1;
}
12.8
#include<stdio.h>

int main()
{
char c,vow=0,con=0;
printf("Enter a character");
scanf("%c",&c);
if(((c>='a') && (c<='z')) || ((c>='A') && (c<='Z')))
{
if((c=='a')||(c=='e')||(c=='i')||(c=='o')||(c=='u')||(c=='A')||(c=='E')||(c=
='I')||(c=='O')||(c=='U'))
vow++;
else
con++;
}
else
printf("\nNot an alphabet");
if(vow)
printf("\nVowel");
if(con)
printf("\nConsonant");
return 0;
}
12.9
#include<stdio.h>
int main()
{
int a=0,b=1,c,n;
scanf("%d",&n);
while(1)
{
c=a+b;
if(c>=n)
goto END;
a=b;
b=c;
}
END:printf("%d %d",a,b);
return 0;
}
12.10
#include<stdio.h>
#include<string.h>
int main()
{
char pla[50],key[50],cip[50];
int i,l1,l2;
gets(pla);
gets(key);
l1=strlen(pla);
l2=strlen(key);
for(i=0;i<l1;i++)
{
pla[i]=pla[i]-64;
}
for(i=0;i<l2;i++)
{
key[i]=key[i]-64;

}
if(l1==l2)
{
goto END;
}
else if(l1>l2)
{
int v=0;
for(i=l2-1;i<l1-1;i++)
{
key[i+1]=key[v];
v++;
}
goto END;
}
else if(l2>l1)
{
for(i=l1;i<=l2;i++)
key[l1+1]=' ';
goto END;
}
END:for(i=0;i<l1;i++)
{
cip[i]=pla[i]+key[i];
if(cip[i]>26)
{
cip[i]=cip[i]%26;
if(cip[i]==0)
cip[i]=26;
}
}
for(i=0;i<l1;i++)
{
cip[i]=cip[i]+64;
printf("%c",cip[i]);
}
return 0;
}

You might also like