-->




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...

Cpp tutorial detecting a keypress and ASCII code

Leave a Comment
Write a simple cpp program which detects a key pressed and its ASCII value with out pressing enter key and using cin>> on press ESC key program should exit. When user press a key its ASCII value should be displayed instantly. You can use any Cpp compiler CodeBlocks Recommended.

Concept used
  • Char and int data types
  • while infinite loop
  • conio.h header file getch() function
Program Logic and Explanation
  •  Its a simple program which reads a character of char data type
  • A header file conio.h is included to use getch() function
  • An infinite while loop breaks on pressing ESC key
  • In while loop  when program runs getch() function calls which wait to get character when we enter a character it store is ASCII value in int variable then displays the pressed key and its ASCII value on screen and then wait to another input a character.
This kind of program help user in game programming and programmer can control the value of any keypress by using a simple if statement. e.g you are making a game and want to control your inpute on pressing up, down, right, left, arrow keys simply get their ASCII values, use a control structure and control your input.
                          Value
Up Key               72
Down Key          80
Right Key          77
Left Key             75


Cpp code: Compiler used CodeBlocks Cpp Compiler

  1. #include<iostream>
  2. #include<conio.h>
  3. using namespace std;
  4. int main()
  5. {
  6.     char key_press;
  7.     int ascii_value;
  8.     cout<<"\n\t\t\tPress Any Key To Check  Its ASCI Value\n\n\t\t\tPress ESC to EXIT\n\n\n";
  9.     while(1)
  10.     {
  11.     key_press=getch();
  12.     ascii_value=key_press;
  13.     if(ascii_value==27) // For ESC
  14.      break;
  15.    cout<<"\t\t\tKEY Pressed-> \" "<<key_press<<" \" Ascii Value =  "<<ascii_value<<"\n\n";
  16.     }
  17.     return 0;
  18. }
Sample input outputs
Detecting a keypress in Cpp code

Its a simple program which helps the beginner to control input keys.

Read More...

Cpp Tutorial to reverse an array code

Leave a Comment
Write a program which takes some elements as input in integer array then pass this array to a function which reverse its elements using for loop and swapping. 
 Display the reversed array with index number. Loop should execute less than array size. Use any c compiler (codeBlocks recommended).

This tutorial contains the following concepts

Program explanation
  • Program declare an integer array of size five, initialize it using for loop
  • Pass size and array name to function 
  • Function uses for loop and swap array elements with in it
  • A for loop is used to display the final result

Compiler used: CodeBlocks C Compiler
 code:
  1. #include <iostream>
  2. using namespace std;
  3. void Reverse_Array(int array[],int size)
  4. {   int temp;
  5.     size--;
  6.     int loop_count=0;
  7.     for(int i=0;size>=i;size--,i++)
  8.     {
  9.         loop_count++;// Counts the iterations
  10.         temp=array[i];
  11.         array[i]=array[size];
  12.         array[size]=temp;
  13.     }
  14.     cout<<"Number of Iterations: "<<loop_count<<endl;
  15. }
  16. int main()
  17. {
  18.     int array[5],i;
  19.     cout<<"\nEnter 5 Integer Values in Array\n"<<endl;
  20.     for(i=0;i<5;i++)
  21.     {
  22.     cout<<"Enter Value For Index Number array [ "<<i<<" ] -> ";
  23.     cin>>array[i];
  24.     }
  25.     // Calling Reverse Array Values Function
  26.     Reverse_Array(array,5);
  27.     cout << "\n\n\t\t\tReversed Array Values" << endl;
  28.     for(i=0;i<=4;i++)
  29.         {
  30.          cout<<"\t\t\tarray ["<<i<<"]"<<"= "<<array[i]<<endl;
  31.         }
  32.     return 0;
  33. }


Sample Input  output on C compiler
cpp tutorial to reverse an integer array using function and for loop



This tutorial is helpful to understand the array indexing and how to swap the index values. Its is recommended for beginners to edit and do experiment with code. Best of luck for learning cpp programming language.
 Find More Basic Examples here
C++ simple examples
Read More...