Upload
shariful-haque-robin
View
263
Download
6
Embed Size (px)
DESCRIPTION
solution manual of programming in ansi c by balagurusamy 1-6 Chapters.
Citation preview
1
2
Special thanks to:
1. Safiqul Islam sir
2. Saddam
3. Arun
4. Oli
5. Nazmul
6. Basri
7. Naser
8. Rashed
9. Suzabat Vai
10. My dear Google &
11. Others websites
“It’s hard enough to find an error in your code when you're looking for it; it's even harder
when you've assumed your code is error-free.” - Steve McConnell
3
Chapter 1 1.1
//Program about mailing address 1.1
#include<stdio.h>
void main()
{
printf("Name: Md. Shariful Haque Robin");
printf("\nDoor No: 7, Street no: 10");
printf("\nCity: Uttara 10 no. sector, Pin code: 1230");
}
1.2
//Program about mailing address With Border line 1.2
#include<stdio.h>
void main()
{
printf("||===========================================||");
printf("\n||Name: Md. Shariful Haque Robin ||");
printf("\n||Door No: 7, Street no: 10 ||");
printf("\n||City: Uttara 10 no. sector, Pin code: 1230 ||");
printf("\n||===========================================||");
}
1.3
//print the pattern
#include<stdio.h>
void main()
{
printf("*\n* *\n* * *\n* * * *");
}
“When you don't create things, you become defined by your tastes rather than ability. Your tastes only
narrow & exclude people. So create.”
― Why the Lucky Stiff
4
1.4
//print the figure 1.4
#include<stdio.h>
void main()
{
printf("|----------| |----------|");
printf("\n| | | |");
printf("\n| | >>------>> | |");
printf("\n| | | |");
printf("\n| | | |");
printf("\n|----------| |----------|");
}
1.5
//calculate area of a circle
#include <stdio.h>
#define PI 3.14
void main()
{
int r;
float a;
scanf("%d",&r);
a=PI*(r*r);
printf("Ans is: %f",a);
}
1.6
//Multiplication table
#include <stdio.h>
void main()
{
printf("5x1=1\n5x2=10\n5x3=15\n5x4=20\n5x5=25\n5x6=30\n5x7=35\n5x8=40\n5x9=45\n5x10=50");
}
“Talk is cheap. Show me the code.”
― Linus Torvalds
5
1.7
//sum and difference 1.7
#include<stdio.h>
void main()
{
int a,b;
a=20+10;
b=20-10;
printf("20+10= %d",a);
printf("\n20-10= %d",b);
}
1.8
//Division 1.8
#include<stdio.h>
void main()
{
int a,b,c;
float x;
scanf("%d%d%d",&a,&b,&c);
x=a/(b-c);
printf("Ans is: %f",x);
}
1.9(a)
//c to f 1.9
#include <stdio.h>
void main ()
{
float C, F;
scanf ("%f", &F);
C= 5*(F-32) /9;
printf ("The temparature in Celcius is : %f", C);
}
6
1.9(b)
//Relation Between C and F 1.9
#include <stdio.h>
void main ()
{
float C, F;
scanf ("%f", &C);
F= ((9*C)/5)+32;
printf ("The temparature in Fahrenheit is : %2f", F);
}
1.10
//Area of Triangle
#include <stdio.h>
#include <math.h>
void main()
{
int S,a,b,c;
float A;
scanf("%d%d%d",&a,&b,&c);
S=(a+b+c)/2;
A=sqrt(S*(S-a)*(S-b)*(S-c));
printf("Ans is: %f",A);
}
1.11
//Distance Between Two point
#include <stdio.h>
#include <math.h>
void main()
{
int x1,x2,y1,y2;
float D;
scanf("%d%d%d%d",&x1,&x2,&y1,&y2);
D=sqrt(((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1)));
printf("Ans is: %f",D);}
7
1.12
//Area And Perimeter 1.12
#include <stdio.h>
#include <math.h>
void main()
{
float R,C,A;
R=sqrt(((4-0)*(4-0))+((5-0)*(5-0)));
C=2*3.14*R;
printf("Perimeter is: %f",C);
A=3.14*R*R;
printf("\nArea is: %f",A);
}
1.13
//Area 1.13
#include <stdio.h>
#include <math.h>
void main()
{
float R,A;
R=(sqrt(((5-2)*(5-2))+((6-2)*(6-2))))/2;
A=3.14*R*R;
printf("\nArea is: %f",A);
}
1.14
//1.14 display equation
#include<stdio.h>
void main()
{
int a,b,c;
a=5;
8
b=8;
c=18;
printf("%dx+%dy=%d",a,b,c);
}
1.15
//1.15 simple calculator
#include<stdio.h>
void main()
{
int a,b,sum,Difference,Product;
float Division;
printf("Enter a & b value: ");
scanf("%d%d",&a,&b);
printf("\nX= %d y=%d",a,b);
sum=a+b;
Difference=a-b;
printf("\nsum= %d Difference= %d",sum,Difference);
Product=a*b;
Division=a/b;
printf("\nProduct= %d Division= %.2f",Product,Division);
}
9
Chapter 2 2.1
//2.1:determine and print harmonic series
#include<stdio.h>
main()
{
float n,sum;
n=5; //consider given n value is 5
sum=1+(1/2)+(1/3)+(1/4)+(1/n);
printf("Ans is: %.3f",sum);
}
2.2
//2.2: convated taka in decimal from to paise
#include<stdio.h>
main()
{
int paise;
float price;
printf("Enter your price: ");
scanf("%f",&price);
paise=price*100;//we know that taka X 100 = paise
printf("\n%d Paise",paise);
}
2.3
//2.3: find all even number 1 to 100
#include<stdio.h>
void main()
{
int i=0;
for(i=2; i<=100; i=i+2)
{
printf("\t%d",i);}
}
10
2.4
//2.4
#include<stdio.h>
void main()
{
float num1,num2,div;
printf("Enter your 1st number: ");
scanf("%f",&num1);
printf("\nEnter your 2nd number: ");
scanf("%f",&num2);
div=num1/num2;
printf("\nnum1=%f, num2=%f, division=%f",num1,num2,div);
}
2.5
//2.5
#include<stdio.h>
void main()
{
float rp,sp;//Here sp=suger price & rp=rice price
scanf("%f%f",&rp,&sp);
printf("\n*** LIST OF ITEMS ***");
printf("\nItem Price");
printf("\nRice Rs %.2f",rp);
printf("\nSuger Rs %.2f",sp);
}
2.6
//2.6
#include<stdio.h>
void main()
{
int a,b,c;
printf("\n Input negative number= ");
scanf("%d",&a);
11
printf("\n Input positive number= ");
scanf("%d",&b);
printf("\n Set= ");
if(a<=0)
{
for(c=a;c<=b;c=c+1)
printf("\t%d",c);
}
}
2.7
//2.7:
#include<stdio.h>
void main()
{
int x,y;
short int z;
printf("Enter x & y values\n");
scanf("%d%d",&x,&y);
z=x+y;
printf("x=%d, y=%d, z=%d",x,y,z);
}
2.8
//2.8:
#include<stdio.h>
void main()
{
float x,y;
int z;
printf("Enter two values:\n");
scanf("%f%f",&x,&y);
z=x+y;
printf("x=%.3f,y=%.3f, z=%d",x,y,z);
}
12
Chapter 3 3.1
#include<stdio.h>
void main()
{
int x,y,z,Temp;
printf("Enter Three Values\n");
scanf("%d%d%d",&x,&y,&z);
Temp=x;
x=y;
y=z;
z=Temp;
printf(" x= %d \n y= %d \n z= %d",x,y,z);
}
3.2
#include<stdio.h>
void main()
{
float x;
int y,z;
printf("Enter floating point number : x= ");
scanf("%f",&x);
y=x;
z=y%10;
printf(" \nThe Right-most digit of the integral part of the number %f is %d",x,z);
}
3.3
#include<stdio.h>
void main()
{
float x;
int y,z;
13
printf("Enter floating point number : x= ");
scanf("%f",&x);
y=x;
z=y%100;
printf(" \nThe two Right-most digit of the integral part of the number %f is %d",x,z);
}
3.4
#include<stdio.h>
void main()
{
int Len,Wid,Area,Peri;
printf("Enter the length of the rectangle :\n");
scanf("%d",&Len);
printf("Enter width of the rectangle :\n");
scanf("%d",&Wid);
Peri= 2*(Len+Wid);
Area= Len*Wid;
printf("The perimeter of the rectangle is =%d \n",Peri);
printf("The area of the rectangle is =%d \n",Area);
}
3.5
#include<stdio.h>
void main()
{
int x,a,b,c;
printf("Enter a four digit number: ");
scanf("%d",&x);
a=x%1000;
b=a%100;
c=b%10;
printf("%d\n",x);
printf("%d\n",a);
printf("%d\n",b);
14
printf("%d\n",c);
}
3.6
#include<stdio.h>
void main()
{
float Dep,Year_Ser,Pur_Price,Sal_Price;
printf("Enter Deperaciation, Year of Service, Purchase price\n");
scanf("%f%f%f",&Dep,&Year_Ser,&Pur_Price);
Sal_Price = Pur_Price-(Dep*Year_Ser);
printf("The salvage value of an item = %f ",Sal_Price);
}
3.7
#include<stdio.h>
void main()
{
int SmallNo,LargeNo;
float RealNo;
printf("Enter the real no.");
scanf("%f",& RealNo);
SmallNo=RealNo;
LargeNo=RealNo;
printf("Smallest integer not ");
printf("The given no. ");
printf("Largest integer not \n");
printf("less than the number ");
printf(" greater than the no.\n");
printf("%d ", SmallNo);
printf("%f ", RealNo);
printf("%d ", LargeNo);
}
15
3.8
#include<stdio.h>
void main()
{
int u,t,a;
float Dis;
printf("Enter the value of u,a and t\n");
scanf("%d %d %d",&u,&a,&t);
Dis=(u*t)+(a*(t*t))/2;
printf("The distance is : %f \n",Dis);
}
3.9
#include<stdio.h>
#include<math.h>
void main()
{
float Dr,Sc,Hc;
float TBO,EOQ;
printf("Enter Demand Rate \n");
scanf("%f\n",&Dr);
printf("Enter Setup Cost \n");
scanf("%f\n",&Sc);
printf("Enter Holding Cost \n");
scanf("%f\n",&Hc);
EOQ=sqrt((2*Dr*Sc)/Hc);
TBO=sqrt((2*Sc)/(Dr*Hc));
printf("The Economic Order Quantity is : %f\n",EOQ);
printf("The time Between Order is : %f",TBO);
}
16
3.10
#include<stdio.h>
#include<math.h>
void main()
{
float L,R,C;
float Freq,Temp1,Temp2;
printf("Enter Inductance, Resistance, Capacitannce \n");
scanf("%f %f %f",&L,&R,&C);
Temp1= (1/(L*C));
Temp2= ((R*R)/(4*C*C));
Freq= sqrt(Temp1-Temp2);
printf("The Frequency is : %f\n",Freq);
}
3.11
#include<stdio.h>
void main()
{
int Num,Sum,Sum1,Sum2,Sum3,Sum4;
Sum1=Sum2=Sum3=Sum4=0;
Sum=0;
printf("Enter a Four Digits Number\n",&Num);
scanf("%d",&Num);
Sum1=Num%10;
Num=Num/10;
Sum2=Num%10;
Num=Num/10;
Sum3=Num%10;
Num=Num/10;
Sum4=Num%10;
Num=Num/10;
Sum=Sum1+Sum2+Sum3+Sum4;
printf("\nSum of Digits are :-- %d\n",Sum);}
17
3.12
#include<stdio.h>
void main()
{
printf("Size of Integer Data Type :-- %d \n",sizeof(int));
printf("Size of Character Data Type :-- %d \n",sizeof(char));
printf("Size of Float Data Type :-- %d \n",sizeof(float));
printf("Size of Double Data Type :-- %d \n",sizeof(double));
}
3.13
#include<stdio.h>
void main()
{
int x,y,z;
printf("Enter Three Numbers:--\n");
scanf("%d %d %d",&x,&y,&z);
((x>y)&&(x>z))?printf("Largest is x :-- %d",x):((y>x)&&(y>z))?printf("Largest is y :--%d",y):printf("Largest
is z :-- %d",z);
}
3.14
#include<stdio.h>
void main()
{
int m,n,x;
printf("Enter Two Numbers:--\n");
scanf("%d %d",&m,&n);
x=m%n;
(x==0)?printf("m is multiple of n\n"):printf("m is not multiple of n\n");
}
18
3.16
#include<stdio.h>
void main()
{
float Cus1,Cus2,Bill1,Bill2;
printf("Enter Numbers of Call of Customer 1:--\n");
scanf("%f",&Cus1);
printf("Enter Numbers of Call of Customer 2:--\n");
scanf("%f",&Cus2);
Cus1<=100?Bill1=250:Bill1=(250+Cus1*1.25);
Cus2<=100?Bill2=250:Bill2=(250+Cus1*1.25);
printf("Mobile Bill of Customer 1:-- %f\n",Bill1);
printf("Mobile Bill of Customer 2:-- %f",Bill2);
}
19
Chapter 5 5.1(a)
#include<stdio.h>
#include<stdlib.h>
void main()
{
int x;
printf("Enter Your Number: ");
scanf("%d",&x);
if(x%2==0)
{
printf("\nNUMBER IS EVEN");
exit(0);
}
printf("\nNUMBER IS ODD");
}
5.2(b)
#include<stdio.h>
void main()
{
int n;
printf("Enter Your Number: ");
scanf("%d",&n);
if(n%2==0)
{
printf("\nNUMBER IS EVEN");
}
else
{
printf("\nNUMBER IS ODD");
}
}
20
5.3
#include<stdio.h>
void main()
{
int a,b,c,d,m,n,R;
float x1,x2;
printf("Enter values of a,b,c,d,m,n\n");
scanf("%d%d%d%d%d%d",&a,&b,&c,&d,&m,&n);
R=(a*d)-(c*b);
if(R!=0)
{
x1=(m*d-b*n)/(a*d-c*b);
x2=(n*a-m*c)/(a*d-c*b);
printf("\nThe value of x1=%.3f\nThe Value of x2=%.3f",x1,x2);
}
else
{
printf("\nThe division is not possible");
}
}
5.4
#include<stdio.h>
void main()
{
float m;
printf("Enter your marks: ");
scanf("%f",&m);
if(m>80&&m<=100)
{
printf("\nYou have obtained more than 80 markes");
}
else if(m>=61&&m<=80)
{
21
printf("\nYou have obtained more than 60 markes");
}
else if(m>=41&&m<=60)
{
printf("\nYou have obtained more than 40 markes");
}
else
{
printf("\nYou have obtained less than 40 markes");
}
}
5.5
#include<stdio.h>
void main()
{
float mat,phy,che,total,total_mp;
printf("Enter marks of mathmatics: ");
scanf("%f",&mat);
printf("\nEnter marks of physics: ");
scanf("%f",&phy);
printf("\nEnter marks of chemistry: ");
scanf("%f",&che);
if(mat>=60&&phy>=50&&che>=40&&total>=200)
{
printf("\nThe candidate is eligible for the addmission");
}
else
{
if(total_mp>=150)
{
printf("\nThe candidate is eligible for the addmission");
}
else
{
22
printf("\nThe candidate is not eligible for the addmission");
}
}
}
5.7(a)
#include <stdio.h>
main()
{
int i,j,n,a=1;
printf("Enter n Value: ");
scanf("%d",&n);
for (i = 1; i <= n; i++)
{
for (j = 1; j <= i; j++)
{
printf("%d ",a);
a++;
}
printf("\n");
}
return 0;
}
5.7(b)
#include <stdio.h>
main()
{
int i,j,n,a=1;
printf("Enter n Value: ");
scanf("%d",&n);
for (i = 1; i <= n; i++)
{
23
for (j = 1; j <= i; j++)
{
printf("%d ",((i+j+1)%2));
a++;
}
printf("\n");
}
return 0;
}
5.8
#include<stdio.h>
#include<math.h>
main()
{
int am,mi,ha,tomi,toha;
printf("Enter your ammount:");
scanf ("%d",&am);
if (am>0 && am<=100)
{
mi=(0);
ha=((am*5)/100);
}
else if(am>101 && am<=200)
{
mi=((am*5)/100);
ha=((am*7.5)/100);
}
else if(am>201 && am<=300)
{
mi=((am*7.5)/100);
ha=((am*10.0)/100);
}
else if(am>301)
24
{
mi=((am*10.0)/100);
ha=((am*15.0)/100);
}
printf("Discount Mill cloth=%d",mi);
printf("\nDiscount Handloon items=%d",ha);
tomi=am-mi;
toha=am-ha;
printf("\nTotal Mill cloth=%d",tomi);
printf("\nTotal Handloon items=%d",toha);
}
5.9(a)
#include<stdio.h>
void main()
{
int x;
printf("Enter x value: ");
scanf("%d",&x);
if(x>0)
{
printf("y value is 1 for given x value");
}
if(x==0)
{
printf("y value is 0 for given x value");
}
if(x<0)
{
printf("y value is -1 for given x value");
}
}
25
5.9(c)
#include<stdio.h>
void main()
{
int x;
printf("Enter x value: ");
scanf("%d",&x);
if(x>0)
{
printf("y value is 1 for given x value");
}
else if(x==0)
{
printf("y value is 0 for given x value");
}
else
{
printf("y value is -1 for given x value");
}
}
5.9(c)
#include<stdio.h>
main()
{
int x,y;
printf("Enter value of x: ");
scanf("%d",&x);
(x>0?(y=1):(x==0)?(y=0):(y=-1));
printf("The value of y for the given value of x=%d is %d\n",x,y);
}
26
5.10
#include<stdio.h>
#include<math.h>
void main()
{
int a,b,c,d;
float x1,x2,x;
printf("Enter value of a,b,c\n");
scanf("%d%d%d",&a,&b,&c);
d=(b*b)-(4*a*c);
if(a==0&&b==0)
{
printf("No Solution");
}
else if(a==0)
{
x=-c/b;
printf("There is only one root and x=%.3f",x);
}
else if(d<0)
{
printf("There are no real root");
}
else
{
x1=-b+(sqrt(d)/(2*a));
x2=-b-(sqrt(d)/(2*a));
printf("Roots are real");
printf("x1=%.3f\nx2=%.3f",x1,x2);
}
}
27
5.11
#include<stdio.h>
void main()
{
int bes,hei,hyp,d,e;
scanf("%d%d%d",&bes,&hei,&hyp);
d=(bes*bes)+(hei*hei);
e=hyp*hyp;
if(d==e)
{
printf("It is right angled triangle");
}
else
{
printf("It is ont right angled triangle");
}
}
5.12
#include<stdio.h>
void main()
{
int units;
char names;
float charge;
printf("Enter your name: ");
scanf("%s",&names);
printf("Enter Total unit amount: ");
scanf("%d",&units);
if(units>=0&&units<=200)
charge=100+(units*0.80);
else if(units>200&&units<=300)
charge=100+(units*0.90);
28
else
charge=(100+units)+(100+units)*.15;
printf("Names Unites Charge\n");
printf("%s %d %.2f",names,units,charge);
}
5.13
#include<stdio.h>
void main()
{
int n,sum=0;
for(n=0;n<=100;n++)
{
if(n%6==0&&n%4!=0)
{
printf("%d, ",n);
sum=sum+n;
}
}
printf("\n\nSum= %d",sum);
}
5.14
#include<stdio.h>
Void main()
{
int n, c = 2;
printf("Enter a number to check if it is prime\n");
scanf("%d",&n);
29
for ( c = 2 ; c <= n - 1 ; c++ )
{
if ( n%c == 0 )
{
printf("%d is not prime.\n", n);
break;
}
}
if ( c == n )
printf("%d is prime.\n", n);
return 0;
}
30
Chapter 6 6.1
#include <stdio.h>
void main()
{
int n, reverse = 0;
printf("Enter a number to reverse: ");
scanf("%d",&n);
while (n != 0)
{
reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10;
}
printf("\nReverse of entered number is = %d\n", reverse);
}
6.1
#include<stdio.h>
void main()
{
int Num,Temp,RevNum,Dig;
printf("Enter any Number: ");
scanf("%d",&Num);
Temp=Num;
RevNum=0;
while(Temp!=0)
{
Dig=Temp%10;
Temp=Temp/10;
31
RevNum=(RevNum*10)+Dig;
}
printf("\nRverse of Number %d is %d\n",Num,RevNum);
}
6.2
#include <stdio.h>
void main()
{
int c, n, fact = 1;
printf("Enter a number: ");
scanf("%d", &n);
for (c = 1; c <= n; c++)
fact = fact * c;
printf("Factorial of %d! = %d\n", n, fact);
}
6.3
#include<stdio.h>
void main()
{
int Num,Temp,Sum,Dig;
printf("Enter any Number: ");
scanf("%d",&Num);
Temp=Num;
Sum=0;
while(Temp!=0)
{
32
Dig=Temp%10;
Temp=Temp/10;
Sum=Sum+Dig;
}
printf("Sum of Number %ld is %d\n",Num,Sum);
}
6.4
#include<stdio.h>
void main()
{
int n, first = 0, second = 1, next, c;
printf("Enter the number of terms: ");
scanf("%d",&n);
printf("First %d terms of Fibonacci series are :-\n",n);
for ( c = 0 ; c < n ; c++ )
{
if ( c <= 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;
}
printf("%d\n",next);
}
}
6.5
33
#include<stdio.h>
void main()
{
int a, s;
printf("Enter value of a: ");
scanf("%d",&a);
for(s=0;a>0;a=a/10)
{
s = s*10;
s = s + (a%10);
}
printf("Reverse number is: %d",s);
}
6.6
#include<stdio.h>
#include<conio.h>
void main()
{
int P,n;
float V,r,temp;
clrscr();
printf("Enter Principal Amount:--\n");
scanf("%d",&P);
printf("For P:-- %d\n",P);
for(r=0.1;r<=0.15;r+=0.01)
{
printf("For Rate %f\n",r);
printf("n V");
for(n=1;n<=5;n++)
{
printf("%d ",n);
temp=pow((1+r),n);
V=P*temp;
34
printf("%f",V);
}
}
printf("\nx = %f; n = %d; x to power n = %f\n",x,n,y);
getch();
}
6.7
#include<stdio.h>
void main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",i);
}
printf("\n");
}
}
6.7(b)
#include<stdio.h>
void main()
{
int i,j,k;
for(i=5;i>=1;i--)
{
for(k=5;k>i;k--)
printf(" ");
35
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}
}
6.8
#include<stdio.h>
void main()
{
int i,age,c=0;
for(i=1;i<=100;i++)
{
printf("Enter the age of the person%d:",i);
scanf("%d",&age);
if (age>=50 && age<=60)
c=c+1;
}
printf("The number of persons in the age group 50 to 60 are : %d",c);
}
6.9 callected from internet
#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
int i;
float a,x,y1,y2;
a=0.4;
36
printf(" Y-----> \n");
printf("0---------------------------------------\n");
for(x=0;x<5;x=x+0.25)
{
y1=(int) (50*exp(-a*x)+0.5);
y2=(int) (50*exp(-a*x*x/2)+0.5);
if(y1==y2)
{
if(x==2.5)
printf("X |");
else
printf("|");
for(i=1;i<=y1-1;++i)
printf(" ");
printf("#\n");
}
else
{
if(y1>y2)
{
if(x==2.5)
printf("X |");
else
printf(" |");
for(i=1;i<y2-1;i++)
printf(" ");
printf("*");
for(i=1;i<=(y1-y2-1);++i)
printf("-");
printf("0\n");
continue;
}
else
{
if(x==2.5)
printf("X |");
else printf(" |");
for(i=1;i<=(y1-1);++i)
37
printf(" ");
printf("0");
for(i=1;i<=(y2-y1-1);++i)
printf("-");
printf("*\n");
}
}
printf(" |\n");
}
}
6.10
#include<stdio.h>
#include<math.h>
void main()
{
float Ex,sum,i,j;
printf("X");
for(j=0.1;j<=0.5;j+=0.1)
printf(" %f",j);
printf("\n");
for(i=1;i<=5;i++)
{
printf("%f",i);
for(j=0.1;j<=0.5;j+=0.1)
{
sum=i+j;
Ex=exp(sum);
printf(" %f",Ex);
}
printf("\n");
}
}
6.11
38
#include<stdio.h>
void main()
{
int Num,Dig,Bin[10],i,Temp,Count;
printf("Enter any Number:--\n");
scanf("%d",&Num);
Temp=Num;
Count=0;
while(Temp!=0)
{
Dig=Temp%2;
Temp=Temp/2;
Bin[Count]=Dig;
Count++;
}
printf("Binary Number of Integer Number %d is \n",Num);
for(i=(Count-1);i>=0;i--)
printf("%d",Bin[i]);
}
6.12
#include<stdio.h>
void main()
{
int i,j,k;
j=1;
for(i=1;i<=3;i++)
for(j=1;j<=18;j++)
{
printf("*");
if(j==18)
printf("\n");
}
for(i=1;i<=3;i++)
39
for(j=1;j<=4;j++)
{
printf("*");
if(j==4)
printf("\n");
}
for(i=1;i<=3;i++)
for(j=1;j<=18;j++)
{
printf("*");
if(j==18)
printf("\n");
}
for(i=1;i<=3;i++)
{
for(k=1;k<=14;k++)
printf(" ");
for(j=15;j<=18;j++)
{
printf("*");
if(j==18)
printf("\n");
}
}
for(i=1;i<=3;i++)
for(j=1;j<=18;j++)
{
printf("*");
if(j==18)
printf("\n");
}
}
40
6.16(a)
#include<stdio.h>
void main()
{
int j,i;
for (i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
printf("S");
printf("\n");
}
}
6.16(b)
#include<stdio.h>
void main()
{
int j,i,k;
for (i=1;i<=5;i++)
printf("S");
for(j=2;j<=4;j++)
{
printf("\nS S");
}
printf("\n");
for (i=1;i<=5;i++)
printf("S");
}
41
6.17
#include<stdio.h>
#include<math.h>
void main()
{
float y;
int x,i;
printf("X Sin(X)\n");
for(i=0;i<=180;i+=15)
{
y=sin(x);
printf("%d %f\n",x,y);
}
}
6.18
#include<stdio.h>
void main()
{
int i,Count;
Count=0;
for(i=1;i<=100;i++)
{
if(i%2!=0 && i%3!=0)
{
Count=Count+1;
printf("%d",i);
}
printf("%d\n",Count);
}
}
42
Chapter 9 9.3
#include<stdio.h>
#include<math.h>
int Fact(int n)
{
if(n==0)
return 1;
else
return (n*Fact(n-1));
}
float Fx(int x)
{
float Result=0;
int i,j,Temp1,Temp2;
for(i=1,j=1;i<=5;i+=2,j++)
{
if(j%2!=0)
{
Temp1 = pow(x,i);
Temp2 = Fact(i);
Result += (float) Temp1/Temp2;
}
else
{
Temp1 = pow(x,i);
Temp2 = Fact(i);
Result -= (float) Temp1/Temp2;
}
}
return Result;
}
void main()
{
int No;
float F;
43
printf("Enter a Number-->\n");
scanf("%d",&No);
F = Fx(No);
printf("Result--> %f",F);
}
9.7
int prime(int x)
{
int i;
for(i=2;i<=x/2;i++)
{
if(x%i==0)
return 0;
}
return 1;
}
void main()
{
int n,c;
printf("Enter a Number-->\n");
scanf("%d",&n);
c = prime(n);
if(c==1)
printf("\nNumber %d is Prime",n);
else
printf("\nNumber %d is Not Prime",n);
}
44
9.10
#include<math.h>
#include<stdio.h>
int a,b,c;
void Read()
{
printf("Enter three sides of Triangle-->\n");
scanf("%d %d %d",&a,&b,&c);
}
void Area()
{
double S,Area,Temp;
S=(double) (a+b+c)/2;
Area=sqrt((S-a)*(S-b)*(S-c));
printf("Area of Triangle:--> %lf",Area);
}
void Peri()
{
int P;
P=a+b+c;
printf("Perimeter of Triangle:--> %d",P);
}
void main()
{
int ch;
Read();
while(1)
{
printf("1. Area \n2. Perimeter \n3. Exit\n");
printf("Enter UR Choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
Area();
break;
45
case 2:
Peri();
break;
default:
break;
}
}
}
9.11
#include<stdio.h>
#define MAX 10
int Largest(int a[][MAX],int m,int n)
{
int Large,i,j;
Large=a[0][0];
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(Large<a[i][j])
Large=a[i][j];
}
}
return Large;
}
void main()
{
int A[MAX][MAX];
int L,m,n,i,j;
printf("Enter Number of Rows\n");
scanf("%d",&m);
printf("Enter Number of Columns\n");
scanf("%d",&n);
printf("Enter Elements of Matrix:--\n");
46
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&A[i][j]);
}
}
printf("Matrix is:--\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",A[i][j]);
}
}
L = Largest(A,m,n);
printf("\nLargest Element :-- %d",L);
}
9.12
#include<stdio.h>
#include<conio.h>
#define MAX 10
void Multiplication(int a[][MAX],int b[][MAX],int m,int n1)
{
int c[MAX][MAX],i,j,k;
for(i=0;i<m;i++)
{
for(j=0;j<n1;j++)
{
c[i][j]=0;
for(k=0;k<m;k++)
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
}
47
printf("Multiplication of Matrices is:--\n");
for(i=0;i<m;i++)
{
for(j=0;j<n1;j++)
{
printf("%d ",c[i][j]);
}
printf("\n");
}
}
void main()
{
int A[MAX][MAX],B[MAX][MAX];
int L,m,n,m1,n1,i,j;
clrscr();
printf("Enter Number of Rows & Columns First Matrix\n");
scanf("%d %d",&m,&n);
printf("Enter Number of Rows & Columns of Second Matrix\n");
scanf("%d %d",&m1,&n1);
if(m==n1)
{
printf("Enter Elements of First Matrix:--\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&A[i][j]);
}
}
printf("Enter Elements of Second Matrix:--\n");
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
scanf("%d",&B[i][j]);
}
}
clrscr();
48
printf("First Matrix is:--\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",A[i][j]);
}
printf("\n");
}
printf("Second Matrix is:--\n");
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
printf("%d ",B[i][j]);
}
printf("\n");
}
Multiplication(A,B,m);
}
else
printf("Multiplication is not applicable\n");
getch();
}
“Good code is its own best documentation. As you're about to add a comment, ask yourself, "How can I
improve the code so that this comment isn't needed?" Improve the code and then document it to make
it even clearer. ” - Steve McConnell
“Programming can be fun, so can cryptography; however they should not be combined.” - Kreitzberg
and Shneiderman
“Copy and paste is a design error.” - David Parnas
49