Translate

Showing posts with label find. Show all posts
Showing posts with label find. Show all posts

c++ program to find area of circle using functions

Leave a Comment
Cpp tutorial to calculate area of circle
  •  User enter radius or diameter of circle.
  •  Program should have two functions one which takes radius as parameter and second takes diameter as parameter.
  •  If user enters wrong option then tell users its valid selection and chose again the right option.
This Cpp Tutorial covers the concept of
  • if else if statement
  • for loop
  • function with parameter and return value
Cpp code compiler used: Codeblocks C++ Compiler

#include <iostream>
#define PI 3.14159
using namespace std;

float AreaOfCircle(float radius);
float AreaWithDiameter(float diameter);

int main()
{
    float radius,diameter,circleArea;
    char choice='0';
    cout<<"\n\t\t\tFind Area Of Circle:"<<endl;


    for(;choice!='1'&&choice!='2';)
        {
         cout<<"\nEnter 1 to Enter Radius OR
                 2 to Enter Diameter: ";
         cin>>choice;
         if(choice!='1'&&choice!='2')
         cout<<"\n\t\tEnter a VALID Option ";

        }

    if(choice=='1')
    {
    cout <<"\n\t\tEnter Radius To Find Area: ";
    cin>>radius;
    circleArea=AreaOfCircle(radius);
    }
    else if(choice=='2')
    {
    cout <<"\n\t\tEnter Diameter To Find Area: ";
    cin>>diameter;
    circleArea=AreaWithDiameter(diameter);

    }
    cout<<"\n\n\t\tArea of Circle is:->> "<<circleArea<<endl;
    return 0;
}

float AreaOfCircle(float radius)
{
    return (PI*(radius*radius));

}

float AreaWithDiameter(float diameter)
{

    return (AreaOfCircle(diameter/2));
}


output of program
calculate area of circle cpp tutorial

Program explanation
  • Program has two functions one for radius and one for diameter both called upon user choice
  • Both calculates the radius and diameter according to formula and return result value from where they have called
  • A for loop is used to prevent the wrong choice selection it will break only if user enters valid option 1 or 2 else it will continue to ask user to enter valid option
Read More...

c++ program to find prime numbers in a given range

Leave a Comment
Cpp tutorial to find prime numbers between given range
  • Write a c++ program in which user will enter start and end of range in integer 
  • Program should display every prime number between range and at the end total number of prime numbers found in range.
  • Do the same program with separate function which receives two parameters for range calculate result and display output like first code
  • Use any C++ compiler codeBlocks Recommended
Cpp Tutorial covers the following concepts

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    int startNum,endNum;
    int found=0,count=0;
    cout<<"Enter Number START of Range:  ";
    cin>>startNum;
    cout<<"Enter Number END of Range:  ";
    cin>>endNum;
    for(int i=startNum;i<=endNum;i++)
       {
           for(int j=2;j<=sqrt(i);j++)
               {
               if(i%j==0)
                  count++;
               }
               if(count==0&&i!=1)
               { found++;
                 cout<<"Prime Number -> "<<i<<endl;
                 count=0;
               }
               count=0;
       }

 cout<<"Total Prime Number Between Range "<<startNum<<" to
 "<<endNum<<" = "<<found<<endl;
 return 1;
}

Output : range is 1 to 10
 cpp tutorial to find prime numbers in a given range




same Code with a function



#include<iostream>
#include<math.h>
using namespace std;
void calculatePrime(int startNum, int endNum);

int main()
{
    int startNum,endNum;
    cout<<"Enter Number START of Range:  ";
    cin>>startNum;
    cout<<"Enter Number END of Range:  ";
    cin>>endNum;

    calculatePrime(startNum,endNum);
 return 1;
}

void calculatePrime(int startNum, int endNum)
{      int found=0,count=0;


    for(int i=startNum;i<=endNum;i++)
       {
           for(int j=2;j<=sqrt(i);j++)
               {
               if(i%j==0)
                  count++;
               }
               if(count==0&&i!=1)
               { found++;
                 cout<<"Prime Number -> "<<i<<endl;
                 count=0;
               }
               count=0;
       }

 cout<<"Total Prime Number Between Range "<<startNum<<" to
        "<<endNum<<" = "<<found<<endl;

}

Another cpp Tutorial here To Find Prime Number 
Find more Example Here Cpp Tutorials
Read More...

Find maximum and minimum number in array c++ code

2 comments
This Tutorial contains:
  • C++ code to find maximum and minimum number in array 
  • Program basic working what is used in program and how 
  • Dry run of the program with respect to variable values for better under standing
Concept used:

Write a program to find maximum and minimum number in array using for loop and if statement.
To initialize array use random numbers

Program should display all random elements of array and minimum and maximum number in array
on the output console.

Program Explanation:
  • Array size is fixed to 100 to change the size just change the value of size integer
  • In the for loop array is initializing with a random number less than 100 to change the range just replace 100 by any desired number and it will produced numbers between 0 to a number you replaced
  • In another for loop which programs go through from start of the array to the end and with contains two if statements which checks for both minimum and maximum number in array
c++ code

#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;
int main()
{
const int size=50;
// array with size of 50
int array[size];
// for random numbers
srand(time(0));
    for(int i=0;i<size;i++){
    // Initializing Array number is less than 100
    array[i]=rand()%100;
    // Displaying array value
    cout<<array[i]<<endl;
    }
            // initializing max, min
                int max=array[0];
                int min=array[0];
/* scanning array to find
   minimum and maximum
   number  */
for(int i=0;i<size;i++){
    // finding minimum number in array
      if(min>array[i]){
         min=array[i];
      }
      //finding maximum number in array
      if(max<array[i]){
      max=array[i];
      }
}
       // displaying output
        cout<<"Maximum Number is :"<<max<<endl;
        cout<<"Minimum Number  is:"<<min<<endl;

return 0;
}


Dry running the code with a simple input so it will be easy to understand the working of program.

let there are five random values in array
55, 3, 2, 44, 100

we will run our main loop in which two if statements are used 
for(int i=0;i<size;i++){
    // finding minimum number in array
      if(min>array[i]){
         min=array[i];
      }
      //finding maximum number in array
      if(max<array[i]){
      max=array[i];
      }
}


before the start of the loop 
max=55; min=55;

i=0;  current array elements 55, 3, 2, 44, 100

min > array[i]
55   > 55    => condition false

max < array[i]
55   > 55    => condition false
------------------------------------------------------

i=1; current array elements 55, 3, 2, 44, 100

min > array[i]
55   > 3    => condition TRUE UPDATE  min =3;

max < array[i]
55    < 3    => condition false  max=55
------------------------------------------------------


i=2; current array elements 55, 3, 2, 44, 100

min > array[i]
3   >    2   => condition TRUE UPDATE  min =2;

max < array[i]
55   <  2    => condition false
------------------------------------------------------


i=3; current array elements 55, 3, 2, 44, 100

min > array[i]2   > 55    => condition false

max < array[i]
55   > 44    => condition false
------------------------------------------------------


i=4; current array elements 55, 3, 2, 44, 100

min > array[i]2   >  100    => condition false

max < array[i]
55   < 100   => condition TRUE UPDATE  max =100;
------------------------------------------------------


Hope this will be helpful.
find more codes here C++ Simple Examples
Read More...

Maximum or largest number in array c++ code

3 comments
Write a C++ program which find largest or maximum number in array using for loop and if statement

Concept used
  • Array in c++
  • for loop
  • if statement

Post contains c++ code and Dry run of code

How program works
  • Program first take size of array from user
  • Then input element or array one by one
  • then show the maximum number in array

c++ code: Using CodeBlocks Compiler

    #include<iostream>
    using namespace std;
    int main()
    {

    cout<<"Enter The Size Of Array:   ";
    int size;
    cin>>size;
    int array[size], key,i;

    // Taking Input In Array
    for(int j=0;j<size;j++){
    cout<<"Enter "<<j<<" Element : ";
    cin>>array[j];
    }
    //Your Entered Array Is
    for(int a=0;a<size;a++){
       cout<<"array[ "<<a<<" ]  =  ";
       cout<<array[a]<<endl;
    }

       int  maximum=array[0];
       for(int i=0;i<size;i++){

          if(array[i]>maximum){
              maximum=array[i];
          }
    }

   cout<<"\n\nMaximum Number is in array is :

   "<<maximum;
   return 0;
    
    }


Sample Output
Maximum or largest number in array c++ code
Sample input output


Dry Run Of Program:
Let size of array is equal to 5
and user enters following numbers
5
6
-7
99
78






Lets dry run main for loop which contains an if statement

which is
     int maximum=array[0];
         for(int i=0;i<size;i++){

          if(array[i]>maximum){
              maximum=array[i];
          }

we have an integer which is maximum = array[0];
so value of maximum= 5;

5, 6, -7, 99, 78

at i=0;
array[i]=5; maximum=5
if(array[i]>maximum)
     5         >    5

condition false


5, 6, -7, 99, 78
at i=1
array[i]=6; maximum=5
if(array[i]>maximum)
     6         >    5

condition True    then maximum=6


5, 6, -7, 99, 78
at i=2
array[i]=-7;  maximum=6
if(array[i]>maximum)
     -7         >    5

condition false



5, 6, -7, 99, 78
at i=3
array[i]= 99;  maximum=6
if(array[i]>maximum)
     99        >   6

condition True  maximum= 99


5, 6, -7, 99, 78
at i=4
array[i]=78;  maximum=99
if(array[i]>maximum)
     78         >    99

condition false



final result is  maximum= 99

so 99 is the maximum number in array


Recommended
There always more than one way to code a program for fast learning change the logic of the program do experiment with code one of the most important is dry run the program on paper it will be very helpful for understand it also improves the logic building

This program will help to understand the working of array, for loop, if statement and assignment operator

More Programs here C++ Simple Examples
Read More...