Friday, 23 November 2018

Program to find maximum of three number using user define function in C++

A function can be define by the user as per the requirement of the user or programmer . To implement
a user define function , a programmer must contain the following three statement .

1. Function declaration or function prototype 

2. Function call statement

3. Function definition

Here one new concept in C++ is INLINE function, Which we going to discuss in earlier post.

Program of find maximum of three number using user define function in C++

#include <iostream.h>
#include<conio.h>
int max ( int , int , int ) ;       
void main()                                                                                                               
{
int a , b , c ;
clrscr () ;
cout<<"\n Enter value of a:" ;
cin>>a;
cout<<"\n Enter value of b:" ;
cin>>b;
cout<<"\n Enter value of c:" ;
cin>>c;
d = max ( a,b,c ) ;
cout<<" maximum number is"=<<"d<<"\n ;
getch () ;
}
int max ( int a , int b , int c ) 
{
if ( (a>b) && (a>c) )
return a ;
else if ( (b>a) && (b>c) )
return b ;
else
return c ;
}

This is program for finding maximum of three number in C++ using user define function. Here first of all we have to input three number from the user and write function call statement and print the value of maximum and outside the main function the function definition is written where the execution of program is takes place and output goes again to the function call statement where it is print on the output screen.



Program and Algorithm to print n terms of fibonacci series in c++

/* program to print fibo series */
#include<iostream.h>#include <conio.h>
void main ()
{
int a=0,b=1,sum,counter=0,n ;
clrscr () ;
cout<<"a"<<\n;
cout<<"b"<<\n;
cout<<"\n how many terms you want to print:" ;
cin>>n;
while (counter<n-2)
{
sum=a+b ;
cout<<"sum="<<"sum"<<\n;
a=b ;
b=sum ;
counter++ ;
}
getch () ;
}

Output :

How many terms you want to print : 5
number are : 0 , 1 , 1 , 2 , 3.

Algorithm ;;

ALGORITHM : FIBONACCI SERIES ( A , B , N , COUNTER )

This algorithm is used to print n terms of fibonacci series 


STEP 1. START.
STEP 2. SET A = 0 , B = 1 , COUNTER = 0 .
STEP 3. INPUT N.
STEP 4. WRITE A.
STEP 5. WRITE B.
STEP 6. REPEAT STEP 7 TO 11.
                  COUNTER < N -2
               [ N - 2 IS WRITTEN BECAUSE OF PRINTING FIRST TWO TERMS OUT OF LOOP ]
STEP 7. SUM : = A + B.
STEP 8. PRINT SUM.
STEP 9. SET A : = B.
STEP 10. SET B : = SUM.
STEP 11. [ INCREMENT THE VALUE OF COUNTER BY 1 ]
                  COUNTER : = COUNTER + 1.
STEP 12. STOP.


Program in C++ to print digit pattern according to input number from user

(1.)  For printing this type structure in C++:

1
1 2
1 2 3 
1 2 3 4
1 2 3 4 5

Here as we seen the figure is in row and column form thus we have to declare two variable ( i and j ) for row and column respectively. Here we use for loop statement of c++.

/* program to print structure */
#include<iostream.h>
#include<conio.h>
void main()
{
int i,j,n ;
clrscr () ;
cout<<"\n Enter number of rows:" ;
cin>>n;
for (i=1;i<=n;i++)
{
    for (j=1;j<=i;j++)
    {
    cout<<"J":
    }
cout<<"\n" ;
}
getch () ;
}

Output screen :: 

Enter number of rows :: 5

output :: 

1
1 2
1 2 3 
1 2 3 4
1 2 3 4 5

 (2.)   For printing this type structure in C++:

1
2 2
3 3 3 
4 4 4 4
5 5 5 5 5

Here as we seen the figure is in row and column form thus we have to declare two variable ( i and j ) for row and column respectively. Here we use for loop statement of c++.

/* program to print structure */
#include<iostream.h>
#include<conio.h>
void main()
{
int i,j,n ;
clrscr () ;
cout<<"\n Enter number of rows:" ;
cin>>n;
for (i=1;i<=n;i++)
{
    for (j=1;j<=i;j++)
    {
    cout<<"i";
    }
cout<<"\n";
}
getch () ;
}

Output screen :: 

Enter number of rows :: 5

output :: 

1
2 2
3 3 3 
4 4 4 4
5 5 5 5 5

 (3.)  For printing this type structure in C++:

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Here as we seen the figure is in row and column form thus we have to declare two variable ( i and j ) for row and column respectively. Here we use for loop statement of c++.

/* program to print structure */
#include<iostream.h>
#include<conio.h>
void main()
{
int i,j,n,number=0;
clrscr () ;
cout<<"\n Enter number of rows:" ;
cin>>n;
for (i=1;i<=n;i++)
{
    for (j=1;j<=i;j++)
    {
    number++;
    cout<<"number" ;
    }
cout<<"\n" ;
}
getch () ;
}

Output screen :: 

Enter number of rows :: 5

output :

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15


Wednesday, 31 October 2018

Program to find cube first n number in c

/* program to find cube of a number */
#include <iostream.h>
# include <stdio.h>
void main ()
{
int n,a=1,b ;
int counter=0 ;
clrscr () ;
cout<<"/n how many no you want to cube";
cin>> n;
while (counter<n)
{
b =a*a*a ;
cin >>b;
counter++ ;
}
getch () ;
}


Monday, 1 October 2018

Difference between object oriented programming and procedural oriented programming

As we know that C++ is based on object oriented programming technique so first of all we have to know what are Difference between object oriented programming and procedural oriented programming.

object oriented programming :

        1. It deals with data.
        2. program is divided in object.
        3. It follow bottom down approach.
        4. It have access specifier.
        5. It requires more amount of memory.
        6. It is more secure.
        7. It follows overloading concept.

C++ , Java , Python , etc

procedural oriented programming

        1. It deals with Algorithm.
        2. program is divided in function.
        3. It follow Top down approach.
        4. It have no access specifier.
        5. It requires less amount of memory.
        6. It is less secure.
        7. It  not follows overloading concept.

Pascal , C , Fortron etc.


Monday, 24 September 2018

Program to calculate electricity bill of a user in different cases in C

#include <stdio.h>
#include <conio.h>
void main ()
{
char name[25];
int choice;
float cahrge1,charge2,charge3,charge4,charge5,unit1,unit2,unit3,unit4,unit5;
printf ("\n enter name of user:");
scanf ("%s", name);
printf ("\n you can perform following operation:");
printf ("\n 1. below 83.33:");
printf ("\n 2. above 83.34 to 100:");
printf ("\n 3. above 100 to 200:");
printf ("\n 4. above 200 to 300:");
printf ("\n 5. above 300:");
printf ("\n enter your choice:"):
scanf ("%d", &choice);
switch ( choice )
{
       case 1:
       printf ("\n enter value of units:");
       scanf ("%d", &unit1);
       printf ("\n Energy charge is 50:");
break;
       case 2:
       printf ("\n enter value of units:");
       scanf ("%d", &unit2);
       charge2= 0.6*unit2;
       printf ("\n Charge=%d",charge2);
break;
       case 3:
       printf ("\n enter value of units:");
       scanf ("%d", &unit3);
       charge3=( 0.8*(unit3-100)+60;
       printf ("\n Charge=%d",charge3);
break;
       case 4:
       printf ("\n enter value of units:");
       scanf ("%d", &unit4);
       charge4=( 0.9*(unit4-200)+140);
       printf ("\n Charge=%d",charge4);
break;
       case 5:
       printf ("\n enter value of units:");
       scanf ("%d", &unit5);
       charge5=( 0.9*(unit5-300)+230);
       charge5=charge5+0.15*(charge5);
       printf ("\n Charge=%d",charge5);
break;
default:
        printf ("\n Have a nice day:");
}
getch ();
}




Saturday, 15 September 2018

Program to do binary search of element in C

What is searching ?

It is defined as find data of a element. We use different types of searching techniques for searching an element.
               Here we use binary search techniques in C.

Program to do binary search of element in C 

#include<conio.h>
void main ()
{
int data[10],beg,mid,end,item,lb,ub,i;
clrscr ();
printf ("\n Enter 5 element in selected order:");
scanf ("%d", &item);
beg=0;
end=4;
mid=(beg+end)/2;
while(beg<=end && data[mid]!=item)
{
      if (item>data[mid])
     {
      beg=mid+1;
      }
      else
       {
      end=mid-1;
       }
      mid=(beg+end)/2;
}
if(beg<end)
{
   printf("\n location = %d",mid+1);
}
else
{
   printf("\n unsuccessful:");
}
getch ();
}

OUTPUT ::  Enter 5 element in selected order = 25,45,65,75,85

                 ITEM SEARCHED ; 45

Featured post

Program to find maximum of three number using user define function in C++

A function can be define by the user as per the requirement of the user or programmer . To implement a user define function , a programmer ...