Posts

Operator Overloading Example in C++

 #include<iostream> using namespace std ; class space { int x,y,z; public : void getData ( int m , int n , int o ) { x = m ; y = n ; z = o ; } void showData () { cout << "value of x =\t" << x << endl ; cout << "value of y =\t" << y << endl ; cout << "value of z =\t" << z << endl ; } void operator -(); }; void space :: operator -() { x =- x ; y =- y ; z =- z ; } int main () { space s ; cout << "Before Operator overloading :-" << endl ; s . getData ( 100 ,- 29 , 39 ); s . showData (); s . operator -(); cout << "\nAfter Operator overloading :-" << endl ; s . showData (); } Output: Before Operator overloading :- value of x =    100 value of y =    -29 value of z =    39 After Operator overloading :- value of x =    -100 value of y =    29 value of z =    -39

Switch case in C++

 #include<iostream> using namespace std ; int main () { int rollno ; char name ; int Class ; int ch ; cout << "enter your choice : " ; cin >> ch ; switch ( ch ){ case 1: cout << "enter your name :" ; cin >> name ; break ; case 2: cout << "enter your rollno: " ; cin >> rollno ; break ; case 3: cout << " enter your class : " ; cin >> Class ; break ; default : cout << "\n Invalid choice:" ; } return 0 ; } Output: enter your choice : 1 enter your name :ssss

How to create an array in C

 #include<stdio.h> int main() { int i,n,arr[20]; printf("Enter the number of element in the arry \n"); scanf("%d",&n); for(i=0;i<n;i++) { printf("\n arr[%d]=",i); scanf("%d",&arr[i]); } } output: Enter the number of element in the arry 5  arr[0]=1  arr[1]=2  arr[2]=3  arr[3]=4  arr[4]=5

Adding number at a specific position in an array in C

 #include<stdio.h> int main() { int i,n,arr[20],num,pos; printf("\nEnter size of array : "); scanf("%d",&n); for(i=0;i<n;i++) { printf("\n arr[%d]= ",i); scanf("%d",&arr[i]); } printf("\nEnter no want to add \n"); scanf("%d",&num); printf("\nEnter position \n "); scanf("%d",&pos); for(i=n-1;i>pos;i--) { arr[i+1]=arr[i]; } arr[pos]=num; n=n+1; printf("\n the after insertion is %d",num); for(i=0;i<n;i++) { printf("\n arr[%d]=%d",i,arr[i]); } return 0; } output: Enter size of array : 5  arr[0]= 1  arr[1]= 2  arr[2]= 3  arr[3]= 4  arr[4]= 5 Enter no want to add 7 Enter position  3  the after insertion is 7  arr[0]=1  arr[1]=2  arr[2]=3  arr[3]=7  arr[4]=4  arr[5]=5

Greatest number among three numbers in C

  #include<stdio.h> int main () { int num1 , num2 , num3 ; printf ( "Enter any three number :\t" ); scanf ( "%d\t%d\t%d" ,& num1 ,& num2 ,& num3 );   printf ( "\t Program to find greatest number among three number\n" );      if ( num1 > num2 )   {   if ( num1 > num3 )   {   printf ( "1st number is greater" );   }   else   {   printf ( "3rd number is greater" );   }   }   else if ( num2 > num1 )   {   if ( num2 > num3 )   {   printf ( "2nd number is greater" );   }   else   {   printf ( "3rd number is greater" );   }   } } output: Enter any three number :        12 22 33          Program to find greatest number among three number 3rd number is greater OR By using nested if in C: #include<stdio....

Program for smallest number among two numbers in C++

  #include<iostream> using namespace  std ; int main () { int  num1 , num2 ; cout << "Enter first number " ; cin >> num1 ; cout << "Enter second  number " ; cin >> num2 ;   cout << "\t Program to find smallest number among two numbers\n" ;    if ( num1 < num2 )   {    cout << "1st number is smallest" ;   }    else    cout << "2nd number is smallest" ; } Output: Enter first number 12 Enter second  number 22          Program to find smallest number among two numbers 1st number is smallest

Smallest number among three numbers in C++

 #include<iostream> using namespace std; int main () { int num1 , num2 , num3 ; cout << "enter first number : " ; cin >> num1 ; cout << "enter second number : " ; cin >> num2 ; cout << "enter third number : " ; cin >> num3 ;   cout << "\t Program to find smallest number among three number\n" ;     if ( num1 <= num2 )   {   if ( num1 < num3 )   {   cout << num1 << " is smallest number " ;   }   else   {   cout << num3 << " is smallest number " ;   }   }   else if ( num2 <= num1 )   {   if ( num2 < num3 )   {   cout << num2 << " is smallest number " ;   }   else   {   cout << num3 << " is smallest number " ;   }   } } Output: enter first number : 12 enter second n...

swapping of two numbers in C++

 #include <iostream> using namespace std; int main () { int a , b , c ; cout << "enter value for a" ; cin >> a ; cout << "enter value for b" ; cin >> b ; c = a ; a = b ; b = c ; cout << "after swapping\n" ; cout << "a=" <<a<< "b=" <<b; return 0 ; } Output: enter value for a 12 enter value for b 22 after swapping a=22 b=12 swapping of two numbers by reference #include<iostream> using namespace std ; void swap ( int & x , int & y ) { int temp ; temp = x ; x = y ; y = temp ; return ; } int main () { int a , b ; cout << "before swapping " ; cout << "value for a is " ; cin >> a ; cout << "value of b is " ; cin >> b ; swap ( a , b ); cout << " after swapping " ; cout << "\nthe value of a : " << a ; cout << ...

greatest number among two numbers in C++

  #include<iostream> using namespace std ; int main () { int num1 , num2 ; cout << "Enter first number " ; cin >> num1 ; cout << "Enter second  number " ; cin >> num2 ;   cout << "\t Program to find greatest number among two numbers\n" ;    if ( num1 > num2 )   {   cout << "1st number is greater" ;   }   else   cout << "2nd number is greater" ; } output Enter first number 3 Enter second  number 7          Program to find greatest number among two numbers 2nd number is greater

Greatest number of three numbers in C++

  #include<iostream> using namespace std ; int main () { int x,y,z,greatest ; cout << "enter first number " ; cin >> x ; cout << "enter second number " ; cin >> y ; cout << "enter third number " ; cin >> z ; greatest = x ; if ( y > greatest ) greatest = y ; if ( z > greatest ) greatest = z ; cout << "\nlargest number is : " << greatest ; return 0 ; } Output: enter first number 5 enter second number 6 enter third number 9 largest number is : 9 Or #include<iostream> using namespace std ; int main () { int x,y,z ; cout << "enter first number " ; cin >> x ; cout << "enter second number " ; cin >> y ; cout << "enter third number " ; cin >> z ; if ( x > y && x > z ) { cout << "x is greater than y" ; cout << "x is greater than z...