c and c programming
play

C and C++ Programming Practical Course M.T ech. 6-Year Integrated - PowerPoint PPT Presentation

C and C++ Programming Practical Course M.T ech. 6-Year Integrated T opics Discussed Introduction to Programming Principles Program Structure Input / Output Statements Computing Computing is a process of taking input, doing a


  1. C and C++ Programming Practical Course M.T ech. 6-Year Integrated

  2. T opics Discussed  Introduction to Programming Principles  Program Structure  Input / Output Statements

  3. Computing  Computing is a process of taking input, doing a process and producing the output

  4. Programming  A program is a set of instructions given to a computer to perform a specific operation  These computer programs are written in a programming language like C or C++  Syntax and Semantics

  5. Program Structure Header #include<stdio.h> Main void main() { Variable int b; Declaration Body printf (“%d”, b); }

  6. Variables and Data Types Name Values Type Representation a,b,c,x,y,z …. 12232323 integer %d average, area 93.0, 21.53, 0.334 float %f result, stud_name, P , R, Vijay, 19MTCS01 character %c rollnumber

  7. Input and Output Statements Input Statement Output Statement printf( ) scanf( ) printf(“Result=%d”,c); scanf(“%d”,&a);

  8. Constructs and Symbols

  9. Our First Programs #include<stdio.h> #include<stdio.h> void main( ) void main( ) { { int a,b,c; float r, area; printf (“Enter the Value of A:”); printf (“Enter the Radius of the Circle:”); scanf("%d", &a); scanf("%f", &r); printf (“Enter the Value of B:”); area=3.14*r*r; scanf("%d", &b); printf("The Area of the Circle is:%f",area); c=a+b; printf("The Sum of A and B is:%d",c); } }

  10. Control Structures - structu ctures es used to control ol the flow of a program. Branching (if-else) Iteration (while, do while & for) Switching (switch)

  11. Branching (if-else) Branching statements allow the flow of execution to jump to a different part of the program.

  12. Voting Eligibility Odd / Even Program #include<stdio.h> #include<stdio.h> void main( ) void main( ) { { int age; int a; printf (“Enter the Age:”); printf (“Enter the Value of A:”); scanf (“%d”, &age); scanf (“%d”, &a); if (age >= 18) if(a % 2 == 0) printf (“ Elibigle ”); printf (“The Number is Even”); else else printf (“Not Eligible”); printf (“The Number is Odd”); } }

  13. Ex. No. 1 – Leap Year Date: 22.02.2020 Objective: Write a C Program to find whether a given year is a leap year or not. 2020, 2016, 2012, 2008, 2004, 2000, 1996… Logic: Year % 4 == 0 Exceptions:1900, 1700 Step 1: Divide the Year by 100, if there is no remainder, then divide it by four hundred. Step 2: Divide the year by 4.

  14. #include<stdio.h> else #include<conio.h> { if(y%4==0) void main( ) { { printf("%d is a leap year",y); int y; } clrscr( ); else { printf("\n Enter a year:"); printf("%d is not a leap year",y); scanf("%d",&y); } } if(y%100==0) getch( ); { } if(y%400==0) { printf("%d is a leap year",y); } else { printf("%d is not a leap year",y); } }

  15. Output Enter a year: 2020 2020 is a leap year

  16. Switching (Switch-Case) A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.

  17. Ex. No. 2 – Choices Date: 22.02.2020 Objective: Write a C Program to illustrate switch statement. Creating a simple calculator with addition, subtraction, multiplication and division. Algorithm 1. Get input for a,b 2. Display choices (+, -, *, / ) 3. Get Choice Value 4. Initiate action based on choice value

  18. #include<stdio.h> #include<conio.h> switch(x) void main() { { case 1 : c = a+b; printf("Result=%f",c); break; float a,b,c; case 2: c = a - b; int x; printf("Result=%f",c); break; clrscr(); case 3: c = a * b; printf("\n Enter A value:"); printf("Result=%f",c); break; scanf("%f",&a); case 4: c = a / b; printf("\n Enter B value:"); printf("Result = %f",c);break; default: printf("Invalid option"); break; scanf("%f",&b); } printf (“Options”); getch(); printf("\n 1. Add \n 2. Subtract \n 3. Multiply \n 4. Divide \n"); } printf("Enter Your Choice:"); scanf("%d",&x);

  19. Output E nter A value: 5 Enter B value: 3 Options 1. Add 2. Subtract 3. Multiply 4. Divide Enter your choice: 3 Result = 15

  20. Iteration An iteration statement, otherwise called as loop, is a way of while repeating a statement untill some condition is satisfied. do-while for

  21. while & do-while int i; int i; i = 1; i = 1; while (i<=100) do { { printf (“%d”, i); printf (“%d”, i); i++; i++; } } while (i<=100);

  22. for for( initaial value ; condition ; increment or decrement) { } for( i=1 ; i<=5 ; i++) { printf("%d",i); }

  23. Ex. No. 3 3 – Fact ctorial l Date: : 29.02.2020 Objective: Write a C Program to find the factorial of a given number Factorial Algorithm 1. n, i, j 2. j=1, i=1; 3. Get input for n 4. If i value is less than or equal to n, j=j*i

  24. #include<stdio.h> #include<conio.h> void main() Iteration i j j { 1 1 1 1 int i, j, n; 2 2 1 2 clrscr(); 3 3 2 6 j=1; 4 4 6 24 printf (“Enter the value of n:”); 5 5 24 120 scanf (“%d”, &n); 6 6 - - for(i=1;i<=n;i++) { j=j*i; } printf (“The factorial of %d is : %d”, n,j); getch(); }

  25. Output Enter the No: 5 Factorial of 5 is 120

  26. Ex. No. 4 4 – Fi Fibo bonacc cci Series ies Date: : 29.02.2020 Objective: Write a C Program to print the Fibonacci series for the given number f 0 =0 Algorith Algo rithm f 1 =1 The series starts with 0,1 and goes on like 0, 1, 1, 2, 3, 5, 8 …. 1. i, j,k,n,x f n = f n-1 + f n-2 2. i=0 j=1 3. Get input for x f 2 = f 2-1 + f 2-2 = 1 + 0 = 1 k=i+j; f 3 = f 2 + f 1 = 1 + 1 = 2 i=j; j=k; f 4 = f 3 + f 2 = 2 + 1 = 3 print k f 5 = f 4 + f 3 = 3 + 2 = 5 f 6 = f 5 + f 4 = 5 + 3 = 8

  27. #include<stdio.h> #include<conio.h> void main() i=0; j=1; x=5; { int i,j,k,n,x; Iteration i j k i j clrscr(); n<=5 i+j ( j ) ( k ) i=0; 1 0 1 1 1 1 j=1; 2 1 1 2 1 2 printf("Enter the Number of Terms:"); 3 1 2 3 2 3 scanf("%d",&x); 4 2 3 5 3 5 printf("%d\t%d",i,j); 5 3 5 8 5 8 for(n=1;n<=x;n++) 6 - - - - - { k=i+j; i=j; j=k; printf("\t%d",k); } getch(); }

  28. Output Enter the Number of Terms: 5 0 1 1 2 3 5 8

  29. Ex. No. 5 5 – Matrix Add ddition n Date: : 07.03.2020 Objective: Write a C Program to add two matrices Matrix Addition – Arrays – Dimensional 1. M x N 2. 2 x 2

  30. #include<stdio.h> for(i=1;i<=3;i++) #include<conio.h> { void main() for(j=1;j<=3;j++) { { int a[3][3],b[3][3],c[3][3],i,j; c[i][j]=a[i][j]+b[i][j]; clrscr(); } } printf("Enter the First Matrix:"); for(i=1;i<=3;i++) printf("\n The Addition of two Matrices:"); { for(j=1;j<=3;j++) for(i=1;i<=3;i++) { { scanf("%d",&a[i][j]); printf("\n"); } for(j=1;j<=3;j++) } A[1,1] A[1,2], A[1,3] { printf("Enter the Second Matrix:"); printf("%d\t",c[i][j]); for(i=1;i<=3;i++) A[2,1] A[2,2], A[2,3] } { } for(j=1;j<=3;j++) A[3,1] A[3,2], A[3,3] { getch(); scanf("%d",&b[i][j]); } } }

  31. Output Enter the first matrix: The Addition of Two Matrices Enter the second matrix: 2 3 2 3 2 3 5 5 5 2 3 5 5 5 2 3 2 5 5 5 3 2 3 2 3 2 3

  32. Ex. No No. 6 6 – User Defi fined ed Functions ions Date: : 07.03.202 020 Objective: Write a C Program to illustrate user defined functions Functions Pre User defined Defined

  33. #include<stdio.h> #include<conio.h> int findmax(int x, int y) { if (x > y) return x; else return y; } void main() { int a,b; a = 10; b = 20; clrscr(); int m = findmax(a, b); printf("Maximum=%d", m); getch(); }

  34. Output Maximum=20

  35. Ex. No. 7 – String Length – User Defined Functions Objective: Write a C Program to find the length of a given string without using built-in functions 1.Create a charcter array. 2.Get a string as an input 3.Find the location of '\0' 4.Print the location.

  36. #include<stdio.h> #include<conio.h> int find(char sample[]) { int i; for(i=0;i<100;i++) if(sample[i]=='\0') return i; } void main() { int len; char a[100]; clrscr(); printf("\nEnter the String:"); gets(a); len=find(a); printf("\n The length of the String is:%d",len); getch(); }

  37. Output Enter the String: Virat The length of the String is: 5

  38. Ex. No. 8 – Palindrome Objective: Write a C Program to find whether a given string is a palindrome or not. 1. Create a charcter array. 2. Get a string as an input 3. Find the reverse of the string 4. Compare the string with its reverse 5. if the reverse and original are same, the string is a palindrome

  39. #include<stdio.h> #include<conio.h> #include<string.h> void main() { char a[40],b[40]; clrscr(); printf("\n Enter any String:"); gets(a); strcpy(b,a); strrev(b); if (strcmp (a,b)==0) printf ("\n %s is palindrome", a); else printf ("\n %s is a non-palindrome", a); getch(); }

Recommend


More recommend