Posts

Showing posts from January, 2021

program to check odd even number by using C++ programming langiage

 #include<iostream> using namespace std ; int main () { int i , sum ; cout << "enter any number : " ; cin >> i ; if ( i % 2 == 0 ) { cout << i << " is a even number " ; } else { cout << i << " is a odd number " ; } return 0 ; } Output: enter any number : 22 22 is a even number

Imagine a publishing company that markets both book and audiocassette versions of its works. Create a class publication that stores the title (a string) and price (type float) of a publication.

  Question : Imagine a publishing company that markets both book and audiocassette versions of its works. Create a class publication that stores the title (a string) and price (type float)  of a publication. From this class derive two classes: Book, which adds a page count (type int), and Tape, which adds a playing time in minutes (type float). Each of these three classes should have a getdata() function to get its data from the user at the keyboard, and a putdata() function to display its data. Write a main() program to test the book and tape classes by creating instances of the asking the user to fill in data with getdata(), and then displaying the data with put. Program:   #include<iostream> #include<string> using namespace std; class publication {     protected:     string title ;     float price ;      public:     publication ()     {         title =" ";         price = 0.0 ;                  }         publication ( string t , float p )         {        

Binary search in C++

 #include<iostream> using namespace std ; int main () { int i , found = 0 , num , n , arr [ 100 ], mid , low , high ;  // declaration and initialization of variables cout << "enter the size of array\n" ;    //prints the msg in double colon cin >> n ;    //stores the value in n for ( i = 0 ; i < n ; i ++)   //for loop { cout << "\n arr[" <<i<< "] = " ;      //for block cin >> arr [ i ];        //for block } cout << "Enter number to be search\n" ;   //prints the msg in double colon cin >> num ;    //stores the value in n low = 0 ;    //initialization of low  to 0 high = n - 1 ;     //initialization of high to n-1 while ( low <= high )    //while loop { mid =(l ow + high )/ 2 ; if ( arr [ mid ]== num ) { cout << "Enter element is found" ; break ; } else if ( num < arr [ mid ]) { high = mid - 1 ; } else

Selection sort in C++

 #include<iostream> using namespace std ; int main () { int min , i , j , temp , a [ 100 ], n ; cout << "\tSelection sort" ; cout << "\nEnter size of array=\t" ; cin >> n ; cout << "\nEnter actual array element:\n" ; for ( i = 0 ; i < n ; i ++) cin >> a [ i ]; for ( i = 0 ; i < n ; i ++) { min = i ; for ( j = i + i ; j < n ; j ++) { if ( a [ min ]> a [ j ]) min = j ; } temp = a [ i ]; a [ i ]= a [ min ]; a [ min ]= temp ; } cout << "\nResultant array=\n" ; for ( i = 0 ; i < n ; i ++) cout << a [ i ]<< " " ; } Output:  Selection sort Enter size of array=    5 Enter actual array element: 9 7 8 4 6 Resultant array= 4 6 7 8 9

Bubble sort in C++

  #include<iostream>  //header file using namespace std ;     //header file int main ()    //main function { int i , j , temp = 0 , n , a [ 20 ];      //declaration and initialization of variables cout << "Enter the array size : \n" ;    //prints the msg on output screen cin >> n ;  //stores value in variable n cout << "\nEnter the array element:\n" ;    //prints the msg on output screen for ( i = 0 ; i < n ; i ++)    //for loop runs from i=0 to i=n-1 cin >> a [ i ];       //stores value in a[i] for ( i = 0 ; i < n ; i ++)    //for loop runs from i=0 to i=n-1 { for ( j = 0 ; j < n - i ; j ++)      //for loop runs from j=0 to j=n-i { if ( a [ j ]> a [ j + 1 ])    //if block { temp = a [ j ];      //storing value of a[j] in temp a [ j ]= a [ j + i ];        //storing value of a[j +1] in a[j] a [ j + 1 ]= temp ;      //storing value of  temp in a[j+1] }  } } cout <&l

How to use pure virtual function in C++

 #include<iostream>    //header file using namespace std ;      // header file class shape    // creating class of name shape { protected :    // access specifier float dim ;    // protected variable of data type float of name dim is declared public :        // access specifier void getdim ()        // member function in public access specifier { cin >> dim ;        } virtual float caldata ()= 0 ;      // pure virtual function declaration };      //end of class shape  (end of class should be by the semicolon) class square : public shape    //class square derived from class shape   { public :        // access specifier float caldata ()        // function definition { return dim*dim ;      //function body                     } };     //end of class square class circle : public shape     //class circle derived from class shape  { public :      // access specifier float caldata ()          // function definition { return 3.1

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.h> int main () { int num1 , num2 , num3 ; printf ( "Enter any three number :\t" );

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 number : 22 enter third number : 23          

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 << &quo

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

String functions (strcat, strchr, strstr, strcmp,strlen,strupr,strlwr)

 Stringscan be manipulated in different ways. there is a set of predefined functions in C++, which are used to manipulated strings in different manners. the predefined functions which are declared in the string.h( or cstring header file in new C++ compilers) header file. Strings predefined functions are: strcat() strcat() function is used to concatenate two strings with each other. it appends the second string to the end of the other string. sytax: char *strcat(char *Destination const char *source); Program to use strcat() funstion: #include<iostream> using namespace std ; #include<cstring> int main () { char str1[10] = "HELLO" ; char str2[10] = "world" ; char str3[20] ; strcat(str1,str2) ; cout << "the string after concatenation is : " << str1 ; } output: the string after concatenation is : HELLOworld strchr() strchr() function is a function used to scan a string in forward direction and looking for the first occurren