2. 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. 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. 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=1n5x2=10n5x3=15n5x4=20n5x5=25n5x6=30n5x7=35n5x8=40n5x9=45n5x10=50"); } “Talk is cheap. Show me the code.” ― Linus Torvalds
5. 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. 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);}
9. 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);} }
11. 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 valuesn"); 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. 12
Chapter 3 3.1 #include<stdio.h> void main() { int x,y,z,Temp; printf("Enter Three Valuesn"); 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. 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("%dn",x); printf("%dn",a); printf("%dn",b);
14. 14
printf("%dn",c); } 3.6 #include<stdio.h> void main() { float Dep,Year_Ser,Pur_Price,Sal_Price; printf("Enter Deperaciation, Year of Service, Purchase pricen"); 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. 15
3.8 #include<stdio.h> void main() { int u,t,a; float Dis; printf("Enter the value of u,a and tn"); 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("%fn",&Dr); printf("Enter Setup Cost n"); scanf("%fn",&Sc); printf("Enter Holding Cost n"); scanf("%fn",&Hc); EOQ=sqrt((2*Dr*Sc)/Hc); TBO=sqrt((2*Sc)/(Dr*Hc)); printf("The Economic Order Quantity is : %fn",EOQ); printf("The time Between Order is : %f",TBO); }
16. 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 : %fn",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 Numbern",&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 :-- %dn",Sum);}
17. 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 nn"):printf("m is not multiple of nn"); }
18. 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:-- %fn",Bill1); printf("Mobile Bill of Customer 2:-- %f",Bill2); }
19. 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. 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,nn"); 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=%.3fnThe 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. 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. 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++) {
24. 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. 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 %dn",x,y); }
26. 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,cn"); 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=%.3fnx2=%.3f",x1,x2); } }
27. 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. 28
else charge=(100+units)+(100+units)*.15; printf("Names Unites Chargen"); 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("nnSum= %d",sum); } 5.14 #include<stdio.h> Void main() { int n, c = 2; printf("Enter a number to check if it is primen"); scanf("%d",&n);
29. 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. 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 = %dn", 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. 31
RevNum=(RevNum*10)+Dig; } printf("nRverse of Number %d is %dn",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! = %dn", 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. 32
Dig=Temp%10; Temp=Temp/10; Sum=Sum+Dig; } printf("Sum of Number %ld is %dn",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("%dn",next); } } 6.5
33. 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:-- %dn",P); for(r=0.1;r<=0.15;r+=0.01) { printf("For Rate %fn",r); printf("n V"); for(n=1;n<=5;n++) { printf("%d ",n); temp=pow((1+r),n); V=P*temp;
34. 34
printf("%f",V); } } printf("nx = %f; n = %d; x to power n = %fn",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. 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;
43. 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. 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. Exitn"); printf("Enter UR Choicen"); scanf("%d",&ch); switch(ch) { case 1: Area(); break;
45. 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 Rowsn"); scanf("%d",&m); printf("Enter Number of Columnsn"); scanf("%d",&n); printf("Enter Elements of Matrix:--n");
47. 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 Matrixn"); scanf("%d %d",&m,&n); printf("Enter Number of Rows & Columns of Second Matrixn"); 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. 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 applicablen"); 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