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

c and c programming
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

C and C++ Programming Practical Course M.T

  • ech. 6-Year Integrated
slide-2
SLIDE 2

T

  • pics Discussed

Introduction to Programming Principles Program Structure Input / Output Statements

slide-3
SLIDE 3

Computing

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

slide-4
SLIDE 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

slide-5
SLIDE 5

Program Structure

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

slide-6
SLIDE 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, rollnumber P , R, Vijay, 19MTCS01 character %c

slide-7
SLIDE 7

Input and Output Statements

Input Statement scanf( ) scanf(“%d”,&a);

Output Statement printf( ) printf(“Result=%d”,c);

slide-8
SLIDE 8

Constructs and Symbols

slide-9
SLIDE 9

Our First Programs

#include<stdio.h> void main( ) { int a,b,c; printf (“Enter the Value of A:”); scanf("%d", &a); printf (“Enter the Value of B:”); scanf("%d", &b); c=a+b; printf("The Sum of A and B is:%d",c); }

#include<stdio.h> void main( ) { float r, area; printf (“Enter the Radius of the Circle:”); scanf("%f", &r); area=3.14*r*r; printf("The Area of the Circle is:%f",area); }

slide-10
SLIDE 10

Control Structures - structu ctures es used to control

  • l the flow of a program.

Branching (if-else) Iteration (while, do while & for) Switching (switch)

slide-11
SLIDE 11

Branching (if-else)

Branching statements allow the flow of execution to jump to a different part of the program.

slide-12
SLIDE 12

Voting Eligibility

#include<stdio.h> void main( ) { int age; printf (“Enter the Age:”); scanf (“%d”, &age); if (age >= 18) printf (“Elibigle”); else printf(“Not Eligible”); }

Odd / Even Program

#include<stdio.h> void main( ) { int a; printf (“Enter the Value of A:”); scanf (“%d”, &a); if(a % 2 == 0) printf (“The Number is Even”); else printf(“The Number is Odd”); }

slide-13
SLIDE 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.

slide-14
SLIDE 14

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

slide-15
SLIDE 15

Output

Enter a year: 2020 2020 is a leap year

slide-16
SLIDE 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.

slide-17
SLIDE 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
slide-18
SLIDE 18

#include<stdio.h> #include<conio.h> void main() { float a,b,c; int x; clrscr(); printf("\n Enter A value:"); scanf("%f",&a); printf("\n Enter B value:"); scanf("%f",&b); printf (“Options”); printf("\n 1. Add \n 2. Subtract \n 3. Multiply \n 4. Divide \n"); printf("Enter Your Choice:"); scanf("%d",&x);

switch(x) { case 1: c = a+b; printf("Result=%f",c); break; case 2: c = a - b; printf("Result=%f",c); break; case 3: c = a * b; printf("Result=%f",c); break; case 4: c = a / b; printf("Result = %f",c);break; default: printf("Invalid option"); break; } getch(); }

slide-19
SLIDE 19

Output

Enter A value: 5 Enter B value: 3 Options

  • 1. Add
  • 2. Subtract
  • 3. Multiply
  • 4. Divide

Enter your choice: 3 Result = 15

slide-20
SLIDE 20

Iteration

An iteration statement, otherwise called as loop, is a way of repeating a statement untill some condition is satisfied.

while do-while for

slide-21
SLIDE 21

while & do-while

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

slide-22
SLIDE 22

for

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

slide-23
SLIDE 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
slide-24
SLIDE 24

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

Iteration i j j 1 1 1 1 2 2 1 2 3 3 2 6 4 4 6 24 5 5 24 120 6 6

slide-25
SLIDE 25

Output

Enter the No: 5 Factorial of 5 is 120

slide-26
SLIDE 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

Algo Algorith rithm

The series starts with 0,1 and goes on like 0, 1, 1, 2, 3, 5, 8….

1. i, j,k,n,x 2. i=0 j=1 3. Get input for x k=i+j; i=j; j=k; print k

f0 =0 f1=1 fn = fn-1 + fn-2 f2 = f2-1 + f2-2 = 1 + 0 = 1 f3 = f2 + f1 = 1 + 1 = 2 f4 = f3 + f2 = 2 + 1 = 3 f5 = f4 + f3 = 3 + 2 = 5 f6 = f5 + f4 = 5 + 3 = 8

slide-27
SLIDE 27

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

Iteration n<=5 i j k i+j i ( j ) j ( k ) 1 1 1 1 1 2 1 1 2 1 2 3 1 2 3 2 3 4 2 3 5 3 5 5 3 5 8 5 8 6

  • i=0; j=1; x=5;
slide-28
SLIDE 28

Output

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

slide-29
SLIDE 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
slide-30
SLIDE 30

#include<stdio.h> #include<conio.h> void main() { int a[3][3],b[3][3],c[3][3],i,j; clrscr(); printf("Enter the First Matrix:"); for(i=1;i<=3;i++) { for(j=1;j<=3;j++) { scanf("%d",&a[i][j]); } }

printf("Enter the Second Matrix:"); for(i=1;i<=3;i++) { for(j=1;j<=3;j++) { scanf("%d",&b[i][j]); } } for(i=1;i<=3;i++) { for(j=1;j<=3;j++) { c[i][j]=a[i][j]+b[i][j]; } } printf("\n The Addition of two Matrices:"); for(i=1;i<=3;i++) { printf("\n"); for(j=1;j<=3;j++) { printf("%d\t",c[i][j]); } } getch(); }

A[1,1] A[1,2], A[1,3] A[2,1] A[2,2], A[2,3] A[3,1] A[3,2], A[3,3]

slide-31
SLIDE 31

Output

Enter the first matrix: 2 2 2 2 2 2 2 2 2

Enter the second matrix: 3 3 3 3 3 3 3 3 3

The Addition of Two Matrices 5 5 5 5 5 5 5 5 5

slide-32
SLIDE 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 defined User Defined

slide-33
SLIDE 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(); }

slide-34
SLIDE 34

Output

Maximum=20

slide-35
SLIDE 35
  • Ex. No. 7 – String Length – User Defined Functions

1.Create a charcter array. 2.Get a string as an input 3.Find the location of '\0' 4.Print the location.

Objective: Write a C Program to find the length of a given string without using built-in functions

slide-36
SLIDE 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(); }

slide-37
SLIDE 37

Output

Enter the String: Virat The length of the String is: 5

slide-38
SLIDE 38
  • Ex. No. 8 – Palindrome
  • 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 Objective: Write a C Program to find whether a given string is a palindrome or not.

slide-39
SLIDE 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(); }

slide-40
SLIDE 40

Output

Enter any String: madam madam is palindrome Enter any String: dumdum dumdum is a non-palindrome

slide-41
SLIDE 41

C+ C++ + Pr Prog

  • gramming

amming

slide-42
SLIDE 42

Software Development : Priorities

Quality

  • Improve

Reusability

  • Ensure

Cost

  • Decrease

Time

  • Manage
slide-43
SLIDE 43

Classes and Objects

  • Blueprint and houses built from that blueprint
slide-44
SLIDE 44

Object Oriented Programming

Class : Human Properties: Physical Features, Mental Characteristics Functions: walk( ), talk( ), code ( ), teach ( ), see( ), laugh( ), cry( )

Class : Dog Properties: Name, Breed, Color Functions: bark( ), run( ), bite( ), sleep( )

Instance: All of us Functions: Jimmy , Pug, Brown

slide-45
SLIDE 45
slide-46
SLIDE 46
slide-47
SLIDE 47

Class : Address Book

Person & Address

  • Name
  • Phone Number
  • Email
  • Door Number
  • Street Name
  • City
  • Zip / Postal Code
slide-48
SLIDE 48

Address Book – Instance and Method

Name: Kumar Phone Number: 0431-2407007 Email: kumar@gmail.com Door Number: 10 Street Name: Sir C.V. Raman Street City: Tiruchirappalli Zip: 620 024

Methods:

search ( ), add ( ), edit ( ), delete ( ), share ( ), sync ( ), migrate ( )

slide-49
SLIDE 49
  • Ex. No. : CPP01 – Banking

Objective: Define a class to represent a bank account. The account should include the following data members and member functions.

Data Members 1 Name of the depositor 2 Account Number 3 Type of account 4 Balance amount in the account Member Functions 1 To assign initial values 2 To deposit an amount 3 To withdraw an amount 4 To display the Name and Balance

slide-50
SLIDE 50

Comparing Two Programmes – C and C++

#include<stdio.h> #include<conio.h> void main( ) { float r, area; clrscr( ); printf (“Enter the Radius of the Circle:”); scanf("%f", &r); area=3.14*r*r; printf("The Area of the Circle is:%f",area); getch( ); } #include<iostream.h> #include<conio.h> class radcal { public: float r, area; void calculate( ) { cout<<“Enter the Radius of the Circle:”; cin>>r; area=3.14*r*r; cout<<"The Area of the Circle is:“<<area; } }; void main( ) { clrscr(); radcal ob;

  • b.calculate();

getch(); }

slide-51
SLIDE 51

#include<iostream.h> #include<conio.h> class banking { public: char cname[50]; char actype[30]; char acno[20]; int balance; int credit, debit;

slide-52
SLIDE 52

void deposit() { cout<<"\n\t\t Enter the amount to be deposited:"; cin>>credit; balance=balance+credit; cout<<"\n\t\t Current Balance:"<<balance; } void create() { cout<<"\n\t\t Enter the Customer Name: "; cin>>cname; cout<<"\n\t\t Enter the Account type:"; cin>>actype; cout<<"\n\t\t Enter the Account number:"; cin>>acno; cout<<"\n\t\t Enter the Opening Balance:"; cin>>balance; cout<<"\n\n Account Created"; }

slide-53
SLIDE 53

void withdraw() { cout<<"\n\t\tEnter the amount to be withdrawn:"; cin>>debit; balance=balance-debit; cout<<"\n\t\t Current Balance:"<<balance; } void display() { cout<<"\n\t\tName:"<<cname; cout<<"\n”; cout<<"\t\tAccount Number:"<<acno; cout<<"\n”; cout<<"\t\tType of account:"<<actype; cout<<"\n”; cout<<"\t\tBalance:"<<balance; cout<<"\n”; } };

slide-54
SLIDE 54

void main() { clrscr(); banking ob; int x; do { cout<<"\n”; cout<<"\n\n\t\t\t Welcome to State Bank of India"; cout<<"\n\t\t 1. Create Account"; cout<<"\n\t\t 2. Deposit"; cout<<"\n\t\t 3. Withdraw"; cout<<"\n\t\t 4. Balance Enquiry"; cout<<"\n\t\t 5. Exit"; cout<<"\n\n\t\t Enter your choice:"; cin>>x; switch(x) { case 1:ob.create();break; case 2:ob.deposit();break; case 3:ob.withdraw();break; case 4:ob.display();break; case 5:break; default: cout<<"Enter any value between 1 and 5"; } }while(x!=5); getch(); }

slide-55
SLIDE 55

Welcome to State Bank of India

  • 1. Create Account
  • 2. Deposit
  • 3. Withdraw
  • 4. Balance Enquiry
  • 5. Exit

Enter your choice: 1

slide-56
SLIDE 56