C and C++ Programming Practical Course M.T
- ech. 6-Year Integrated
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
Introduction to Programming Principles Program Structure Input / Output Statements
Computing is a process of taking input, doing a process and producing the output
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
Header #include<stdio.h> Main void main() { Variable Declaration int b; Body printf(“%d”, b); }
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
Output Statement printf( ) printf(“Result=%d”,c);
#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); }
Control Structures - structu ctures es used to control
Branching statements allow the flow of execution to jump to a different part of the program.
#include<stdio.h> void main( ) { int age; printf (“Enter the Age:”); scanf (“%d”, &age); if (age >= 18) printf (“Elibigle”); else printf(“Not Eligible”); }
#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”); }
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.
#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( ); }
Enter a year: 2020 2020 is a leap year
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.
Objective: Write a C Program to illustrate switch statement.
Creating a simple calculator with addition, subtraction, multiplication and division. Algorithm
#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(); }
Enter A value: 5 Enter B value: 3 Options
Enter your choice: 3 Result = 15
An iteration statement, otherwise called as loop, is a way of repeating a statement untill some condition is satisfied.
int i; i = 1; while (i<=100) { printf(“%d”, i); i++; } int i; i = 1; do { printf(“%d”, i); i++; } while (i<=100);
for( initaial value ; condition ; increment or decrement) { } for( i=1 ; i<=5 ; i++) { printf("%d",i); }
Objective: Write a C Program to find the factorial of a given number
Factorial Algorithm
#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
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
#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
Objective: Write a C Program to add two matrices
Matrix Addition – Arrays – Dimensional
#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]
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
Objective: Write a C Program to illustrate user defined functions
#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(); }
Objective: Write a C Program to find the length of a given string without using built-in functions
#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(); }
string is a palindrome Objective: Write a C Program to find whether a given string is a palindrome or not.
#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(); }
Class : Human Properties: Physical Features, Mental Characteristics Functions: walk( ), talk( ), code ( ), teach ( ), see( ), laugh( ), cry( )
Instance: All of us Functions: Jimmy , Pug, Brown
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
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
#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;
getch(); }
#include<iostream.h> #include<conio.h> class banking { public: char cname[50]; char actype[30]; char acno[20]; int balance; int credit, debit;
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"; }
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”; } };
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(); }
Welcome to State Bank of India
Enter your choice: 1