learning c programming by rbm

34
Learning C programming By RBM EEE 2k11 0 7 / 0 5 / 2 0 2 2 1

Upload: raihan-bin-mofidul

Post on 14-Apr-2017

78 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Learning c programming by rbm

05/02/2023

1

Learning C programmingBy RBM EEE 2k11

Page 2: Learning c programming by rbm

05/02/2023

2What is programming? A method of doing a function or a process

Input:A command which is given by the programmer e.g.We are EEE family(We want to print out what we’ve written)

Three partsProcessing Unit: Analysing the command Converting input command in

binary data Process the binary command Converting to aspect resulte.g.i. We are EEE family = 0101…..ii. Process 0101……iii. 0101= We are EEE family

Output:Result of the process e.g.We are EEE family(Computer’ve printed out what we had wanted to write)

Page 3: Learning c programming by rbm

05/02/2023

3What is compiler?A compiler is such a complex program which converts different languages (eg. C,C++,JAVA,Mac,Linax) to binary languages (eg. exe,jar,apk,sis) through which a computer may understand the command

Compilers we specially require:

• Code-blocks (for c or c++ programming)• Net-Beans (for Java or Android programming)

Page 4: Learning c programming by rbm

05/02/2023

4I. &argument(Argument: a reference or value that is passed to a function, procedure, subroutine, command, or program e.g. int i,j:) means the memory location of the argument

II. == means logical equalIII.!= means not equalIV. || means orV. && means logical andVI.i++ means i=i+1 after recursion ; ++i means i=i+1 before recursionVII.i-- means i=i-1 after recursion ; --i means i=i-1 before recursionVIII.i-=y means i=i-y; i+=y means i=i+y ; i*=y means i=i*y ; i/=y means

i=i/y IX.>= greater than & equalX. <= less than & equalXI.P%Q=reminder of (P/Q)XII.pow(argument,power) or argument ^ powerXIII.\n means new lineXIV.// means not coding laguageXV.\”----\” means print “----”XVI.To scan scanf(“%datatype(d or f or c) ”,&argument);XVII.To print printf(“%datatype(d or f or c) ”,argument);

C Symbolic patterns

Page 5: Learning c programming by rbm

05/02/2023

5General knowledge of C coding:CODING:

#include <stdio.h>//include standard input & output numbers#include <stdlib.h>//include standard library numbers

int main()// main function , integer type ,not including any arguments{ printf("Hello world! \n");// \n means new line return 0;//main function doesn’t return any value}

Page 6: Learning c programming by rbm

05/02/2023

61.Print your name#include <stdio.h>#include <stdlib.h>

int main(){ printf("My name is ----\n"); return 0;}

Page 7: Learning c programming by rbm

05/02/2023

7Data types: Integer type (%d ,%i require) Float type (%f ; 5.2%f means ddd.dd [d means digit]) Character type (Two types ; one character [%c]or String[%s]) Graphical type Sound type Accelerator type etc….

Bytes Required:#include <stdio.h>#include <stdlib.h>int main(){ int a; float b; char c; int s1,s2,s3; s1=sizeof(a); s2=sizeof(b); s3=sizeof(c); printf ("%d %d %d ",s1,s2,s3); return 0;}

An integer requires 4 bytes of Memory

A float requires 4 bytes of Memory

A character requires 1 byte of Memory

A string requires (x + Null) bytes . Here x is its total bytes of characters

Page 8: Learning c programming by rbm

05/02/2023

8Some important Functions & Features:for : for(initialization; condition; increment or

decrement )Program :#include <stdio.h>#include <stdlib.h>

int main(){ int i; // i is an integer type argument for (i=0;i<10;i++) // for (initialization i=0; condition i<10 ;increment i=i+1 or i++ ) { printf("%d\n",i); // print the various values of i } printf("\n\n\n"); // print 3 new lines for (i=9;i>=0;i--) // for (initialization i=9; condition i>=0 ;decrement i=i-1 or i-- ) { printf("%d\n",i); // print the various values of i } return 0;}

Page 9: Learning c programming by rbm

05/02/2023

9 while: 1.Initialization 2.while (condition) 3.{function}Coding:#include <stdio.h>#include <stdlib.h>

int main(){ int i=0; while(i<10) { printf("%d\n",i); i++; } int y=1; while(y==1) { printf("while function is working \n"); printf("Press 1 to continue else any key to close:"); scanf("%d",&y); } return 0;}

Page 10: Learning c programming by rbm

05/02/2023

10 do…..while : 1. intialization 2. do{function}; 3.while (condition)Coding:#include <stdio.h>#include <stdlib.h>

int main(){ int i=0; do { printf("%d\n",i); i++; } while(i<10);

int y=1; do{ puts("do function is workimg"); printf ("Enter 1 to continue ;else any key to exit: "); scanf("%d",&y); } while(y==1); return 0;}

Page 11: Learning c programming by rbm

05/02/2023

11 Switch function: 1. switch (int type argument) 2. case (int variable) if (variable ~ argument) then case function worksCoding:#include <stdio.h>#include <stdlib.h>

int main(){ int argument; scanf("%d",&argument); switch(argument) { case(10): puts("Case 10"); break; case(9): puts("Case 9"); break; } return 0;}

//Integer type argument declareded//scan the value of the argument// switch (value of argument)

//analyzing for case (value 10)// if 10 is equivalent to argument value,do the case function,then break//analyzing for case (value 9)// if 9 is equivalent to argument value,do the case function,then break

Page 12: Learning c programming by rbm

05/02/2023

12ArrayArray is a list of elements in a static memory locationo Elements inserted in pre-determined static memory location by means of

sequential indexo All elements must be same data-typeo If any element is deleted or inserted then locations must be resetCoding:#include <stdio.h>#include <stdlib.h>

int main(){ int a[3]={1,2,3}; float b[2]={2.34,4.56}; char c[2]={'v','r'}; int i; for (i=0;i<3;i++) { printf("a%d:%d ; b%d:%.2f ; c%d:%c ;\n",i,a[i],i,b[i],i,c[i]); } return 0;}

Page 13: Learning c programming by rbm

05/02/2023

13User define function: Manually defined a function Which can only be called by main function Must get input element & return aspect element It can never call the main function The arguments used in User define function are called Global variable The arguments used in main function are called Local VariableCoding-1:#include <stdio.h>#include <stdlib.h>

int userdefinefunction(int i){ printf("User define function is called\n"); printf("%d is scaned from main function",i);}

int main(){ userdefinefunction(4); return 0;}

Page 14: Learning c programming by rbm

05/02/2023

14User define function:Coding-2(Using Protype):#include <stdio.h>#include <stdlib.h>

int userdefinefunction(int i);//function prototype

int main(){ userdefinefunction(4); return 0;}

int userdefinefunction(int i){ printf("User define function is called\n"); printf("%d is scaned from main function",i);}

Page 15: Learning c programming by rbm

05/02/2023

15Pointer variable: A special variable including 1.Value of the variable (e.g. *v ~ argument) 2.Location of the variable (e.g. v ~ &argument)Coding:#include <stdio.h>#include <stdlib.h>

int main(){ int quantity,*p,n; quantity=179; p=&quantity; n=*p; printf("Value of n=%d",n); printf("\n&quantity=%d(memory location of the Quantity)\np=%d(memory location of the Quantity)\n*p=%d(Value of the Quantity)",&quantity,p,*p); return 0;}

Page 16: Learning c programming by rbm

05/02/2023

16C programs1-1.Adding two numbers#include <stdio.h>#include <stdlib.h>

int main(){ int a,b,sum; scanf("%d%d",&a,&b); sum=a+b; printf("Sum of %d & %d is %d",a,b,sum); return 0;}

1 Adding , Multiplying of two & n Numbers:

Page 17: Learning c programming by rbm

05/02/2023

171-2.Adding n Sequential numbers & average of themCoding:#include <stdio.h>#include <stdlib.h>

int main(){ int n,i; float sum=0; scanf("%d",&n); for (i=1;i<=n;i++) { sum=i+sum; } printf("SUM:%.2f\n",sum);

float avg; avg=sum/n; printf("Average:%.2f",avg); return 0;}

1 Adding , Multiplying of two & n Numbers:

Page 18: Learning c programming by rbm

05/02/2023

181-3. Adding n numbers & average of themCoding:#include <stdio.h>#include <stdlib.h>

int main(){ int n,i; scanf("%d",&n); int a[n]; float sum=0; for (i=0;i<n;i++) { printf("Scan Num%d =",i+1); scanf("%d",&a[i]); } for (i=0;i<n;i++) { sum=a[i]+sum; } printf("SUM: %.2f\n",sum); float avg; avg=sum/n; printf("Average: %.2f",avg); return 0;}

1 Adding , Multiplying of two & n Numbers:

Page 19: Learning c programming by rbm

05/02/2023

191-4. Multiplying of n numbers:Coding:#include <stdio.h>#include <stdlib.h>

int main(){ int n,i; scanf("%d",&n); int a[n]; float mul=1; for (i=0;i<n;i++) { printf("Scan Num%d =",i+1); scanf("%d",&a[i]); } for (i=0;i<n;i++) { mul=a[i]*mul; } printf("Multiple result: %.2f\n",mul); return 0;}

1 Adding , Multiplying of two & n Numbers:

Page 20: Learning c programming by rbm

05/02/2023

201-5. Sum of four digits of a single number:Coding:#include <stdio.h>#include <stdlib.h>

int main(){ int num,fd,sd,td,fourthd,restnum; printf("Enter a four digit Number: "); scanf("%d",&num); fourthd=num/1000; restnum=num%1000; td=restnum/100; restnum=restnum%100; sd=restnum/10; restnum=restnum%10; fd=restnum; printf("First digit: %d\nSecond digit: %d\nThird digit: %d\nFourth digit: %d\n",fd,sd,td,fourthd); int sum=fd+sd+td+fourthd; printf("Sum: %d+%d+%d+%d=%d",fourthd,td,sd,fd,sum); return 0;}

1 Adding , Multiplying of two & n Numbers:

Page 21: Learning c programming by rbm

05/02/2023

211-6.Multiplication of four digits of a single number inputCoding:#include <stdio.h>#include <stdlib.h>

int main(){ int num,fd,sd,td,fourthd,restnum; printf("Enter a four digit Number: "); scanf("%d",&num); fourthd=num/1000; restnum=num%1000; td=restnum/100; restnum=restnum%100; sd=restnum/10; restnum=restnum%10; fd=restnum; printf("First digit: %d\nSecond digit: %d\nThird digit: %d\nFourth digit: %d\n",fd,sd,td,fourthd); int mul=fd*sd*td*fourthd; printf(“Multiplication: %d*%d*%d*%d=%d",fourthd,td,sd,fd,mul); return 0;}

1 Adding , Multiplying of two & n Numbers:

Page 22: Learning c programming by rbm

05/02/2023

222 Finding Leap yearCoding:#include <stdio.h>#include <stdlib.h>int main(){ int year; printf("Enter a year:"); scanf("%d",&year); while (year!=0) { if (year%4==0) { printf("%d is a leap year",year); } else { printf("%d is not a leap year",year); } printf("\n\nEnter a year:"); scanf("%d",&year); } return 0;}

Page 23: Learning c programming by rbm

05/02/2023

233 Pyramid 3-1. Pyramid Ascending with increasing numCoding:#include <stdio.h>#include <stdlib.h>int main(){ int i,j,d,n; printf("Enter the loop number:"); scanf("%d",&n); for (i=0; i<n; i++) { for (j=(n-1)-i ;j >=0 ; j--) { printf(" "); } for (d=1; d<=i+1; d++) { printf("%d ",d); //digit + 1space } printf("\n"); } return 0;}

Page 24: Learning c programming by rbm

05/02/2023

243-2.Pyramd Descending with decreasing number:Coding:#include <stdio.h>#include <stdlib.h>int main(){ int i,j,d,n; printf("Enter the loop number:"); scanf("%d",&n); for (i=0; i<n; i++) { for (j=0 ; j<=i ; j++) { printf(" "); } for (d=n-i; d>0; d--) { printf("%d ",d); } printf("\n"); } return 0;}

3 Pyramid

Page 25: Learning c programming by rbm

05/02/2023

25#include <stdio.h>#include <stdlib.h>int factorial (int a){ int i,b=1; for (i=1;i<=a;i++) { b=b*i; } return(b);}int main(){ int n,m,r,loop,pasclenum; printf("Enter the loop number:"); scanf("%d",&loop); for (n=0; n<loop; n++) { for (m=(loop-1)-n ;m >=0 ; m--) { printf(" "); } for (r=0; r<=n; r++) { pasclenum=factorial(n)/(factorial(r)*factorial(n-r)); printf("%d ",pasclenum); } printf("\n"); } return 0;}

3-3.Pascle Pyramid:

Coding->

3 Pyramid

Page 26: Learning c programming by rbm

05/02/2023

264.Finding Roots of Two Degree EquationCoding:#include <stdio.h>#include <stdlib.h>int main(){ float a,b,c,D,X1,X2; printf ("Input the values of a,b,andc\n") ; scanf ("%f %f %f" ,&a,&b,&c); D = (b*b-4*a*c); if (D<0) printf ("Determinant = %.2f <0 \nTherefore Roots are IMAGINERY\n",D); else if (D==0) { X1=(-b+sqrt(D))/(2*a); X2=(-b-sqrt(D))/(2*a); printf("Determinant = %.2f \nTherefore Root1 =%.2f is exactly equal to Root2 =%.2f",D,X1,X2); } else if (D>0) { X1=(-b+sqrt(D))/(2*a); X2=(-b-sqrt(D))/(2*a); printf("Determinant = %.2f >0 \nTherefore Root1 =%.2f & Root2 =%.2f",D,X1,X2); } return 0;}

Page 27: Learning c programming by rbm

05/02/2023

275.Permutation & CombinationCoding:#include <stdio.h>#include <stdlib.h>int factorial (int a){ int i,b=1; for (i=1; i<=a; i++) { b=b*i; } return(b);}int main(){ int n,r; float perm,comb; printf("n="); scanf("%d",&n); printf("r="); scanf("%d",&r); perm=factorial(n)/factorial(n-r); comb=factorial(n)/(factorial(r)*factorial(n-r)); printf("\nnPr=%.2f\nnCr=%.2f",perm,comb); return 0;}

Page 28: Learning c programming by rbm

05/02/2023

286.e^x & e^-xCoding:#include <stdio.h>#include <stdlib.h>int f(int a){ int i,b=1; for (i=1; i<=a; i++) { b=b*i; } return(b);}int main(){ int n,x; float t,s; printf("x="); scanf("%d",&x); s=1;

for (n=1;; n++) { t=pow(x,n)/f(n); if (t<.001) { break; } else { s=s+t; } } printf ("e^%d=%.4f\n",x,s); printf ("e^-%d=%.4f",x,1/s); return 0;}

Page 29: Learning c programming by rbm

05/02/2023

297.CGPA grading7-1. CGPA grading( By switch function )Coding:#include <stdio.h>#include <stdlib.h>int main(){ int number; scanf("%d",&number); switch (number/5) { case 20: //case=int(number/5) case 19: case 18: case 17: case 16: printf ("The Grade is A+\n"); break; case 15: printf ("The Grade is A\n"); break; case 14: printf ("The Grade is A-\n"); break; case 13: printf ("The Grade is B+\n"); break; case 12: printf ("The Grade is B\n"); break;

case 11: printf ("The Grade is B-\n"); break; case 10: printf ("The Grade is C+\n"); break; case 9: printf ("The Grade is C\n"); break; case 8: printf ("The Grade is C-\n"); break; case 7: printf ("The Grade is D\n"); break; case 6: if (number>=33) { printf ("The Grade is E\n"); } else { printf ("The Grade is F\n"); } break;

case 5: case 4: case 3: case 2: case 1: case 0: printf ("The Grade is F\n"); break; } return 0;}

Page 30: Learning c programming by rbm

05/02/2023

307.CGPA grading7-2. CGPA grading( By if----else function )Coding:#include <stdio.h>#include <stdlib.h>int main(){ int number; printf("Enter your number (over 100):"); scanf("%d",&number); while (!(number<0 || number>100)) { if(number<=100 && number>=80) { printf("Your marks %d holds \"A+\" grade",number); } else if(number<80 && number>=75) { printf("Your marks %d holds \"A\" grade",number); } else if(number<75 && number>=70) { printf("Your marks %d holds \"A-\" grade",number); } else if(number<70 && number>=65) { printf("Your marks %d holds \"B+\" grade",number); } else if(number<65 && number>=60) { printf("Your marks %d holds \"B\" grade",number); }

else if(number<55 && number>=50) { printf("Your marks %d holds \"C+\" grade",number); } else if(number<50 && number>=45) { printf("Your marks %d holds \"C\" grade",number); } else if(number<45 && number>=40) { printf("Your marks %d holds \"C-\" grade",number); } else if(number<40 && number>=35) { printf("Your marks %d holds \"D\" grade",number); } else if(number<35 && number>=33) { printf("Your marks %d holds \"E\" grade",number); } else if(number<33 && number>=0) { printf("Your marks %d holds \"F\" grade",number); printf("\nDon't worry;\nFailure is the pillar of success"); } printf("\nEnter your number (over 100):"); scanf("%d",&number); } return 0;}

Page 31: Learning c programming by rbm

05/02/2023

318.Dollar to Taka convertionCoding:#include <stdio.h>#include <stdlib.h>

int main(){ float x,y; printf("Enter the amount of dollar="); scanf("%f",&x); y=(x*(80.75)); printf("Enter the amount of taka=%5.2f",y); return 0;}

Page 32: Learning c programming by rbm

05/02/2023

329.Solve: 1+1/5+1/10+……….+1/nCoding:#include <stdio.h>#include <stdlib.h>

int main(){ float n,x,s,t; printf("n="); scanf("%f",&n); s=1; for(x=5; x<=n; x+=5) // x+=5 means x=x+5 { printf("x=%.0f\n",x); t=1/x; s=s+t; } printf("1+1/5+1/10+...+1/%.0f=%.2f",n,s); return 0;}

Page 33: Learning c programming by rbm

05/02/2023

3310.Matrix10.1 Adding of [3x3] matrixCoding:#include <stdio.h>#include <stdlib.h>int main(){ int r,c,m1[3][3],m2[3][3],m[3][3]; printf ("Matrix A:\n"); for(r=0; r<3; r++) { for (c=0; c<3; c++) { printf ("\nRow=%d,Column=%d:",r+1,c+1); scanf ("%d",&m1[r][c]); } } printf ("\n\nMatrix B:\n"); for(r=0; r<3; r++) { for (c=0; c<3; c++) { printf ("\nRow=%d,Column=%d:",r+1,c+1); scanf ("%d",&m2[r][c]); } }

for(r=0; r<3; r++) { for (c=0; c<3; c++) { m[r][c]=m1[r][c]+m2[r][c] ; } } printf ("\nMatrix Sum:\n"); for(r=0; r<3; r++) { for (c=0; c<3; c++) { printf ("%5d",m[r][c]);

} printf ("\n"); } return 0;}

Page 34: Learning c programming by rbm

05/02/2023

3410.Matrix10.2 Multiplying of [3x3] matrixCoding:#include <stdio.h>#include <stdlib.h>int main(){ int r,c,m1[3][3],m2[3][3],m[3][3]; printf("Matrix1\n"); for(r=0; r<3; r++) for(c=0; c<3; c++) { printf("row=%d column=%d: ",r+1,c+1); scanf ("%d",&m1[r][c]); } printf("Matrix2\n"); for(r=0; r<3; r++) for(c=0; c<3; c++) { printf("row=%d column=%d: ",r+1,c+1); scanf ("%d",&m2[r][c]); }

for(r=0; r<3; r++) for(c=0; c<3; c++) { m[r][c]= m1[r][0]*m2[0][c]+m1[r][1]*m2[1][c]+m1[r][2]*m2[2][c]; } printf("Multiplication:\n"); for(r=0; r<3; r++) { for(c=0; c<3; c++) printf ("%5d",m[r][c]); printf("\n"); } return 0;}