Ex:B1 Fibonacci Series Aim : To Write a C++ Program that prints a Fibonacci series #include <iostream.h> int f1 = -1, f2=1, f3, n, i; cout < < "\n\nenter the value of N : "; cin >> n ; cout < < "\n\nfibonacci Series\n"; for(i=1; i <= n; i++) f3=f1+f2; cout < < "\t" < < f3; f1=f2; f2=f3; Input : Enter the value of N : 5 Ex:B2 Fibonacci Series 0 1 1 2 3 Factorial Value Aim : To Write a C++ Program that finds Factorial Value #include <iostream.h> long factorial(int n) long f=1; int i; for(i=1; i <= n; i++) f = f*i; return f;
int m; cout < < "\nenter the value of M : "; cin >> m ; cout < < "\n\nm = " < < m; cout < < "\n\nm! = " < < factorial(m); Input : Enter the value of M : 5 M = 5 M! = 120 Ex:B3 Number to Words Aim : To write a C++ Program that displays the given number into words #include <iostream.h> int n; cout < < "\nenter the value of N : "; cin >> n ; cout < < "\n\nn = " < < n < < "\t" ; switch(n) case 1 : cout < < "One"; break; case 2 : cout < < "Two"; break; case 3 : cout < < "Three"; break; case 4 : cout < < "Four"; break; case 5 : cout < < "Five"; break; default: cout < < "Enter a number between 1-5";
Input : Enter the value of N : 5 N = 5 Five Ex:B4 Palindrome Checking Aim : to Write a program that tests Palindrome word #include < string.h> #include <iostream.h> char str1[100], str2[100]; cout << "\nenter a string : " ; cin.getline(str1, 100, '\n'); strcpy(str2, str1); strrev(str2); cout << "\ngiven String : " << str1; cout << "\nreversed String : " << str2; if(strcmp(str1, str2)==0) cout << "\npalindrome" ; else cout << "\nnot a Palindrome" ; Input : Ex:B5 Enter a string : madam Given String : madam Reversed String : madam Palindrome Odd/Even Number Checking Aim : To write a C++ program that counts odd/even numbers
int a[20], n, i, odd=0, even=0; cout < < "\nenter the Value of N : "; cin >> n; cout < < "\nenter array values : "; for(i=0; i< n; i++) cin >> a[i]; if(a[i]%2==0) even++; else odd++; cout < < "\ngiven Numbers are : "; for(i=0; i< n; i++) cout < < a[i] < < "\t"; cout < < "\nodd Numbers : " < < odd; cout < < "\neven Numbers : " < < even; Input : Enter the Value of N : 5 11 12 13 14 15 Given Numbers are : 11 12 13 14 15 Odd Numbers : 3 Even Numbers : 2 Ex:B6 Matrix Transpose Aim : To Transpose a given matrix int a[3][3], i, j;
cout < < "\nenter A(3X3) Matrix : "; cin >> a[i][j]; cout < < "\n\ngiven Matrix\n"; cout < < a[i][j] < < "\t"; cout < < "\n"; cout < < "\n\ntransposed Matrix\n"; cout < < a[j][i] < < "\t"; cout < < "\n"; Input : Enter A(3X3) Matrix : 1 2 3 4 5 6 7 8 9 Ex:B7 Given Matrix 1 2 3 4 5 6 7 8 9 Transposed Matrix 1 4 7 2 5 8 3 6 9 Matrix Addition Aim : to Add two 3 X 3 matrices
int a[3][3], b[3][3], c[3][3], i, j; cout < < "\nenter A(3X3) Matrix : "; cin >> a[i][j]; cout < < "\nenter B(3X3) Matrix : "; cin >> b[i][j]; c[i][j] = a[i][j] + b[i][j]; cout < < "\n\nmatrix A\n"; cout < < a[i][j] < < "\t"; cout < < "\n"; cout < < "\n\nmatrix B\n"; cout < < b[i][j] < < "\t"; cout < < "\n"; cout < < "\n\nmatrix C\n"; cout < < c[i][j] < < "\t"; cout < < "\n";
Input : Enter A(3X3) Matrix : 1 2 3 4 5 6 7 8 9 Enter B(3X3) Matrix : 9 8 7 6 5 4 3 2 1 Matrix A 1 2 3 4 5 6 7 8 9 Matrix B 9 8 7 6 5 4 3 2 1 Ex:B8 Matrix C 10 10 10 10 10 10 10 10 10 Prime Number Checking Aim : To write a C++ program that tests Prime number. int n, i; cout < < "\nenter a Number : "; cin >> n; for(i=2; i< n; i++) if(n%i==0)
cout < < n < < " is not a prime number"; return; cout < < n < < " is a prime number"; Input : Enter a Number : 7 Ex:B9 7 is a prime number Class Example Aim : To illustrate Class concept in C++ #include < stdio.h> class Employee int empno; char ename[35]; float basic, hra, da, netpay; float calculate(void) return basic+da+hra; public : void havedata(void) cout < < "\nenter employee number : "; cin >> empno; cout < < "\nenter employee name : "; gets(ename); cout < < "\nenter basic pay, hra, da : "; cin >> basic >> hra >> da;
netpay = calculate( ); void dispdata(void) cout < < "\nname : " < < ename; cout < < "\nemployee Number : " < < empno; cout < < "\nbasic Pay : " < < basic; cout < < "\nhra : " < < hra; cout < < "\nda : " < < da; cout < < "\nnet Pay : " < < netpay; ; Employee obj; obj.havedata( ); obj.dispdata( ); Input : Enter employee number : 12501 Enter employee name : Tamizh S Enter basic pay, hra, da : 12000 700 3000 Ex:B10 Name : Tamizh S Employee Number : 12501 Basic Pay : 12000 HRA : 700 DA : 3000 Net Pay : 15700 Function Overloading Aim : To write a C++ program for function overloading void max(int a, int b)
cout < < "\na = " < < a < < "\tb = " < < b; if(a>b) cout < < "\tmax = " < < a; else cout < < "\tmax = " < < b; void max(int a, int b, int c) cout < < "\na = " < < a < < "\tb = " < < b < < "\tc = " < < c; if(a>b) if(a>c) cout < < "\tmax = " < < a; else cout < < "\tmax = " < < c; else if(b>c) cout < < "\tmax = " < < b; else cout < < "\tmax = " < < c; max(10, 58); max(78, 125, -7); A = 10 B = 58 Max = 58 A = 78 B = 125 C = -7 Max = 125 Ex:B11 Inheritance Aim : To explain Inheritance concept in C++
class Add private: int sum ; protected: int num1, num2; public: Add( ) num1=num2=sum=0; void accept(void) cout < < "\nenter 2 numbers : "; cin >> num1 >> num2; void plus(void) sum=num1+num2; cout < < "\n\naddition"; cout < < "\nnum1 = " < < num1; cout < < "\nnum2 = " < < num2; cout < < "\nsum = " < < sum; ; class Subtract : public Add private : int sub; public : Subtract( ) num1=num2=sub=0; void minus(void) sub=num1-num2; cout < < "\n\nsubtraction"; cout < < "\nnum1 = " < < num1; cout < < "\nnum2 = " < < num2;
; cout < < "\nsub = " < < sub; Subtract obj; obj.accept( ); obj.plus( ); obj.minus( ); Input : Enter 2 numbers : 10 5 Ex:B12 Addition num1 = 10 num2 = 5 Sum = 15 Subtraction num1 = 10 num2 = 5 Sub = 5 Pattern Display Aim : To display the given pattern char str[10] = "COMPUTER"; int i; for(i=1; i< =8; i++) cout.write(str, i); cout < < "\n";
C CO COM COMP COMPU COMPUT COMPUTE COMPUTER