Navigation Bar

Calculate Circumference With Function

#include<iostream.h>
#include<conio.h>
float area(int&);
float Circumference(int&);
void main()
{
 clrscr();
 int r;
 float a;
 cout<<"enter radius=";
 cin>>r;
 a=area(r);
 cout<<"area="<<a<<endl;
 a=Circumference(r);
 cout<<"Circumference ="<<a<<endl;
 getch();
}
//Area function
float area(int&r)
{
  float a;
  return a=r*r*3.14;
}
//Circumference function
float Circumference(int&r)
{
  float a;
  return a=2*3.14*r;

}

Prime or Not prime with while loop

#include<iostream.h>
#include<conio.h>
void main()
{
  char c = ‘y’;
  int n;
  while( c == ‘y’ || c ==’Y’ )
    {
clrscr();
cout<<” Enter a Number”<<endl;
cin>>n;
             int s = 0;

for(int i=1; i<=n; i++)
  {
            if(n%i==0)
                 s = s+1;
  }

if(s == 2)
cout<<” Prime Number”<<endl;
else
cout<<” Not Prime Number”<<endl;

cout<<” Do you want to check another number? Press ‘y’ for yes and ‘n’ for No ”<<endl;
cin>>c;
    }
getch();
}

Prime or Not prime with while loop

#include<iostream.h>
#include<conio.h>
void main()
{
  char = ‘y’;
  int n;
  while( c == ‘y’ || c ==’Y’ )
    {
clrscr();
cout<<” Enter a Number”<<endl;
cin>>n;
             int s = 0;
for(int i=0; i<=n; i++)
  {
            if(n%i==0)
                 s = s+1;
  }
if(s == 2)
cout<<” Prime Number”<<endl;
else
cout<<” Not Prime Number”<<endl;
cout<<” Do you want to check another number? Press ‘y’ for yes and ‘n’ for No ”<<endl;
cin>>c;
    }
getch();
}

Program to Convert Military Time to Standard time (12 OR 24 Hours)

  1. #include<iostream>
  2. #include<conio.h>
  3. using namespace std;
  4. int main()
  5. {
  6. int hours,mins,seconds;
  7. bool status = true;
  8. cout << "Enter Hours (00-23): "; cin >> hours;
  9. cout << "Enter Minutes (00-59): "; cin >> mins;
  10. cout << "Enter Seconds (00-59): "; cin >> seconds;
  11. if((hours >= 24 || hours < 0))
  12. {
  13. cout<<"\nInvalid Hours.."; status=false;
  14. }
  15. if((seconds >= 60 || seconds < 0))
  16. {
  17. cout<<"\nInvalid Seconds.."; status=false;
  18. }
  19. if((mins >= 60 || mins < 0))
  20. {
  21. cout<<"\nInvalid Minutes.."; status=false;
  22. }
  23. if(status)
  24. {
  25. cout<<"\n24 Hours Format\n";
  26. cout << "Hours: " << hours << endl;
  27. cout << "Minutes: " << mins << endl;
  28. cout << "Seconds: " << seconds << endl;
  29. if(hours > 12)
  30. {
  31. hours-=12;
  32. }
  33. cout<<"\n\n12 Hours Format\n";
  34. cout << "Hours: " << hours << endl;
  35. cout << "Minutes: " << mins << endl;
  36. cout << "Seconds: " << seconds << endl;
  37. }
  38. return 0;
  39. }

Output:

program to check entered character is small, capital, digit or a special character

C++ program which takes input a character and check it whether entered character is capital letter, small letter, Digit or Special character

All characters like small letterscapital lettersdigits or special character have ASCII codes when a key is pressed from keyboard there is always an ASCII value behind it like if small letter 'a' is pressed its ASCII is 97 if capital letter 'A' is pressed its ASCII value is 65 if a number digit '0' is pressed its ASCII value is 48.

ASCII value ranges 
 0 - 9      48 - 57
A - Z     65 - 90
a - z       97 - 122
Special Characters  0-47, 58-64, 91-96, 123-127

On the basis of ASCII values and using operators like and operator (&&) or Operator  (||) we can differentiate the letters.

Concept Used
C++ Code :
If you enter more that one character program will show the result on the basis of first charater



#include<iostream>
using namespace std;
int main()
{
  char character;
  cout<<"Enter a character =  ";
    cin>>character;

  int storeAscii=character;
  cout<<"The ASCII value  of "<<character<<
        " is "<<storeAscii;

  if (storeAscii>=65 && storeAscii<=90)
  {
    cout<<"\nYou have entered a capital letter";
  }

  else if (storeAscii>=97 && storeAscii<=122)
  {
    cout<<"\nYou have entered a small letter";
  }

  else if (storeAscii>=47 && storeAscii<=57)
  {
    cout<<"\nYou have entered a digit ";
  }

  else if (storeAscii>=&& storeAscii>=47
      || storeAscii>=54 && storeAscii<=64
      || storeAscii>=91 && storeAscii<=96 
      || storeAscii>=123 && storeAscii<=127)
  {
    cout<<"\nYou have entered a special character";
  }

return 0;
}

Sample Input:
C++ program to check the entered character is a capital letter, a small letter, a digit or a special character




This Program helps to understand the operators like OR operator(||) how if else works, and ASCII values of characters

Find Armstrong Number in C++ code with logic explanation and code dry run

What is Armstrong number?
 A number in which the sum of cube of its individual digits is equal to the number itself is called Armstrong number
For Example: 1^3 + 5^3 + 3^3 = 153
4 * 4 * 4 + 0 * 0 * 0 + 7 * 7 * 7 = 407 is an Armstrong number.


C++ program which takes input a number and check whether it is Armstrong Number or not



  #include<iostream>
  using namespace std;
  int main()
  {
  int armstrong=0,num=0,result=0,check;
  cout<<"Enter Number to find it is an Armstrong number?";
       cin>>num;
       check=num;
       for(int i=1;num!=0;i++){
           armstrong=num%10;
           num=num/10;
           armstrong=armstrong*armstrong*armstrong;
           result=result+armstrong;
       }
       if(result==check){
       cout<<check<<"  is an Armstrong Number";
       }
       else{
       cout<<check<<"  is NOT an Armstrong Number";
       }
       return 0;
    }



 Note: Program can be coded in more than one ways above program is very simple so any one can understand the logic of program.

Recommended: Change program logic and do experiment with it for fast learning.

Image view of the program click on image to view large
Armstrong number code in c++ programming
Find Armstrong number in c++ code


Logic Explanation:

Concept Used:  for loopif-else statement
  • To make logic firstly concept about Armstrong number should be veryclear
  • We took some variables in which we take input, make calculations and produce results
  • Program take input number in variable num and store it in checkvariable
  • In for loop we take mod of num with 10 and stores it in variable Armstrong then we divide it with 10(below dry running will make moreclear the working)
  • Then we take cubes of Armstrong variable and add it into result variable
  • When num=0; then for loop break after for loop using if else statement we test if our result equals to check variable then number is Armstrong else not.

Dry Running The Armstrong number Code:

Let input is equal to 153 

variable values before for loop
num=153; check=153; Armstrong=0; result=0;
variable values in for loop line by line
for i=1
Armstrong=3;
 num=15;
Armstrong=3*3*3=27 
result=0+27=27;

for loop condition check: num is not equal to zero loop will run again

for i=2
Armstrong=5;
num=1;
Armstrong=5*5*5=125 
result=27+125=152;

for loop condition: num is not equal to zero loop will run again

for i=3
Armstrong=1;
num=0;
Armstrong=1*1*1=1; 
result=152+1=153;


for loop condition: num is EQUAL TO ZERO loop will run again

Loop will break and if else condition will be checked as or result=153 and check=153 
if condition will true and program will show output
 153 is an Armstrong Number