Posts

Showing posts with the label cppExample

CPP Program convert octal to binary

 In this example, you will learn about the conversion of octal to binary number in the cpp programming language .you must have knowledge about the following topics to understand the working of the program. Cpp while loop Cpp input method Cpp output method Following is the code of conversion. Code: #include <iostream> #include <cmath> using namespace std; int main() {     int octalNumber,original;     cout << "Enter an octal number: ";     cin >> octalNumber; original=octalNumber;     int decimalNumber = 0, i = 0;     long long binaryNumber = 0;     while(octalNumber != 0)     {         decimalNumber += (octalNumber%10) * pow(8,i);         ++i;         octalNumber/=10;     }     i = 1;     while (decimalNumber != 0)     {         binaryNumber += (decimalNumber % 2) * i; ...

CPP Program convert binary to octal

In this program, you will learn about the conversion of binary numbers to octal numbers in the cpp programming language. Code: #include <iostream> #include <cmath> using namespace std; int main() {     long long binaryNumber,original;     cout << " Enter a binary number  ";     cin >> binaryNumber; original=binaryNumber;        int octalNumber = 0, decimalNumber = 0, i = 0;     while(binaryNumber != 0)     {         decimalNumber += (binaryNumber%10) * pow(2,i);         ++i;         binaryNumber/=10;     }     i = 1;     while (decimalNumber != 0)     {         octalNumber += (decimalNumber % 8) * i;         decimalNumber /= 8;         i *= 10;     }       cout << original << "   in bin...

CPP Program convert decimal to octal

 In this example, you will learn about the conversion of decimal to octal number in the CPP programming language. For understanding the program you have the knowledge about the following topic. while loop input method output method Code: #include <iostream> #include <cmath> using namespace std; int main() {    int decimalNumber,original;    cout << "Enter a decimal number ";    cin >> decimalNumber;   original=decimalNumber;     int remainder, i = 1, octalNumber = 0;     while (decimalNumber != 0)     {         remainder = decimalNumber % 8;         decimalNumber /= 8;         octalNumber += remainder * i;         i *= 10;     }            cout << original << " in decimal  =  " << octalNumber << "  in octal "<<endl;    ret...

CPP program convert octal to decimal

 In this program, you will learn about the conversion of octal to decimal number in the cpp programming language. In order to understand the following code must have the knowledge of the following topics: cpp while loop cpp input method cpp output method Code: #include <iostream> #include <cmath> using namespace std; int main() {    int octalNumber,originalnumber;    cout << "Enter an octal number "<<endl;    cin >> octalNumber;   originalnumber=octalNumber;     int decimalNumber = 0, i = 0, remainder;     while (octalNumber != 0)      {         remainder = octalNumber % 10;         octalNumber /= 10;         decimalNumber += remainder * pow(8, i);         i++;     }      cout << originalnumber << " in octal = " << decimalNumber << " in decimal"<<endl...

cpp program decimal to binary

 conversion binary to decimal In this program, you learn how to convert the binary numbers into decimal numbers in the cpp programming language. For understanding this conversion you must have the knowledge of the following topics. For loop cin if-else Following is the conversion code code: #include <iostream> #include <cmath> using namespace std; int main() {     int num, binaryNumber,decimal;     cout << "Enter a decimal number: ";     cin >> num;         decimal=num;          int remainder, i = 1;     while (num!=0)     {         remainder = num%2;         num /= 2;         binaryNumber += remainder*i;         i *= 10;     }           cout << decimal << " in decimal = " << binaryNumber << " in binary" << endl ;   ...

cpp program to convert binary to decimal

In this program, you will learn to convert the binary number into a decimal number in cpp programming langauge. Following is the conversion binary to decimal number code: #include <iostream> #include <cmath> using namespace std; int main() {     long  num;     cout << "Enter a binary number: ";     cin >> num;       int decimalNumber = 0, i = 0, rem;     while (num!=0)     {         rem = num%10;         num /= 10;         decimalNumber += rem*pow(2,i);         ++i;     }         cout  << decimalNumber << " in decimal"; return 0; } Output: Enter a binary number: 101 5 in decimal

CPP program to check character is vowel or consonant

 In this program, you will learn how to check the character is a vowel or not in the cpp programming language. There are five alphabets a,I,o,u, and e are vowels other are consonant. For understanding, you should have knowledge about the following topics. cpp cin cpp cout cpp if-else Following is the code. code: #include <iostream> using namespace std; int main() {     char ch;          cout << "Enter an alphabet: ";     cin >> ch;          if(ch == 'a' || ch == 'e' || ch== 'i' || ch == 'o' || ch == 'u')     {     cout << ch << " is a vowel."; }          else if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') { cout << ch << " is a vowel."; }          else         cout << ch << " is a consonant.";     return 0; } Output: Enter an alphabet: ...

c++ program to Find HCF or GCD

FInding GCD: HCF is the highest common factor of the two numbers,how to find in the cpp programming language. For the calculation of the HCF or GCD, you must have knowledge of the following topics. for loop while loop if else and nested if else Here is the code for finding GCD  Code: #include <iostream> using namespace std; int main() {     int num1, num2;     cout << "Enter two numbers: ";     cin >> num1 >> num2;          while(num1 != num2)     {         if(num1 > num2)             num1 -= num2;         else             num2 -= num1;     }     cout << "HCF = " << num1;     return 0; } Output: Enter two numbers: 13 26 HCF = 13 Here is another to find the GCD of the two number in cpp . Code: #include <iostream> using namespace std; int main() { ...

cpp program to find LCM

In this program, find the LCM (lowest common multiple)   of the two numbers in the cpp programming language . For understanding this program you should have the knowledge about   if-else while loop code: #include <iostream> using namespace std; int main() {     int num1, num2, maximum;     cout << "Enter two numbers: ";     cin >> num1 >> num2;          if(num1>num2)     {     maximum=num1; } else { maximum=num2; }           while (true)     {         if (maximum % num1 == 0 && maximum % num2 == 0)         {             cout << "LCM = " << maximum;             break;         }         else             maximum++;     }...

Cpp program to check leap year

 What is a leap year The year which contains one day extra for the synchronization of the calendar year with the  astronomical year. The year divisible by 4 and divisible by 100 is not but divisible by 400 is known as a leap year. For understanding the following code you must learn first if-else . Code: #include <iostream> using namespace std; int main() {     int years;     cout << "Enter a year: ";     cin >> years;     if (years % 4 == 0)     {         if (years % 100 == 0)         {               if (years % 400 == 0)          cout << years << " is a leap year";          else          cout << years << " is not a leap year";                               ...

C++ Program to Find Largest Number Among Three Numbers

In this program, you will learn about how to find the largest number among the three numbers in the cpp programming language. Code: #include <iostream> using namespace std; int main() { int var1,var2,var3; cout<<" Enter the first number "<<endl; cin>>var1; cout<<" Enter the second number "<<endl; cin>>var2; cout<<" Enter the third number "<<endl; cin>>var3; if(var1>=var2 && var1>=var3) { cout<<" this is largest number = "<<var1<<endl; } if(var2>=var1 && var2>=var3) { cout<<" this is largest number = "<<var2<<endl; } if(var3>=var2 && var3>=var1) { cout<<" this is largest number = "<<var3<<endl; }     return 0; } Output:  Enter the first number 5  Enter the second number 6  Enter the third number 7  this is largest number = 7 Here, compare the first variable by other ...

CPP PROGRAM TO CHECK NUMBER IS EVEN OR ODD

In this program, you will learn about how to check the number is even or odd in the cpp programming language. For understanding ,you must learn following topics output method input method if else Code: #include <iostream> using namespace std; int main() { int var; cout<<" enter any number to check even or odd "<<endl; cin>>var; if(var%2==0) { cout<<" It is even number "<<var; } else { cout<<" it is odd number "<<var; }     return 0; } Output:  enter any number to check even or odd 5  it is odd number 5

Cpp Program Find ASCII Value of a Character

 In this program, you will learn about printing the value of ASCII  of the char variables in the cpp programming language. Every character on the keyboard of the computer has its own integer number which is called the ASCII value of the character . The range is (between 0 and 127). For example: ASCII value of the A is 65. code: #include <iostream> using namespace std; int main() { char ch; cout<<" enter any one character "<<endl; cin>>ch; cout<<" ASCII value of "<<ch<<"  is "<<int(ch)<<endl;     return 0; } Output:  enter any one character A  ASCII value of A  is 65 Here, printing the integer value of the character by writing int(ch), and print the ASCII value of the character.

Cpp Program to Find Size of int, float, double and char

  This cpp program finds the size of 6 variables of the different data types, By declaring the different data types and uses of sizeof the operator. The syntax of finding the size of the variable: sizeof(datatype); code: #include <iostream> using namespace std; int main() { cout<<" The size of char "<<sizeof(char)<<" bytes"<<endl; cout<<" The size of int "<<sizeof(int)<<" bytes"<<endl; cout<<" The size of float "<<sizeof(float)<<" bytes"<<endl; cout<<" The size of double "<<sizeof(double)<<" bytes"<<endl; cout<<" The size of short "<<sizeof(short)<<" bytes"<<endl; cout<<" The size of boolean "<<sizeof(bool)<<" bytes"<<endl;     return 0; } Output: The size of char 1 bytes  The size of int 4 bytes  The s...

cpp program to reverse the number

The program to reverse an integer number in the cpp programming language.For understanding the program you should learn the first following topic. while loop input method output method Code: #include <iostream> using namespace std; int main() { int num,reversednumber=0; cout<<" enter the number "<<endl; cin>>num; while(num!=0) { reversednumber=reversednumber*10+num%10; num=num/10; } cout<<" reversed number = "<<reversednumber<<endl;     return 0; } here, firstly enter the number through the input method then use while loop.In the loop, the terminal condition is num!=0 , modulus the num separate the one digit and add in the reverse number at its unit place by multiple by 10. At the end of the loop,(num/10)divide the number by 10 to reduce the one-digit till the no digit in the num. In the last of the program, display the reversednumber on the screen. If you face any problem in the understanding of the ab...

cpp program to check palindrome or not

 This program tells about the number is the palindrome or not by using while loop.Reverse the number and match with the original number . For understanding you should learn the while loop  first. code #include <iostream> using namespace std; int main() {      int originalnumber, num, digit, reversenumber = 0;      cout << "Enter a positive number: ";      cin >> num;      originalnumber = num;      while (num != 0)      {          digit = num % 10;          reversenumber = (reversenumber * 10) + digit;          num = num / 10;      }       cout << " The reverse of the number is: " <<reversenumber<< endl;      if (originalnumber == reversenumber)          cout << " The number is a palindrome.";  ...

cpp program swap two numbers by bitwise,multiple and addition

 cpp program swap two numbers  Swapping the two number is a tricky task in cpp programming language,In this example,swap two numbers with the help of bitwise operators, addition and subtraction, and multiplication.  Swap two number by addition and subtraction #include<iostream> using namespace std; int main() {     int a,b;     a=10,b=20;     a=a+b;     b=a-b;     a=a-b;     cout<<"a ="<<a<<endl<<"b ="<<b;     return 0; } Output: a=20 b=10 in the above example, a+b is equal to 30 and store in the a variable then a-b is assigned to the variable due to 30-20 is equal to 10 then a-b is assign to the a due to 30-10 is equal to 20. Swap two number by multiplication and division #include<iostream> using namespace std; int main() {     int a,b;     a=10,b=20;     a=a*b;     b=a/b;     a=a/b;     cout<<...

cpp check prime number is or not

  In this example, the solution to finding the prime number in the cpp programming language.for doing this you should learn the following topics. for loop if-else input and output in the cpp language. Code of prime number: #include <iostream> using namespace std; int main() {     int var, n;     bool Prime = true;     cout << "Enter a positive integer: ";     cin >> n;          if (n == 0 || n == 1) {// 0 and 1 is already prime numbers         Prime = false;     }     else {         for (var = 2; var <= n -1; ++var) {             if (n % var == 0) {                 Prime = false;                 break;             }         }     }     if (Prime)     ...

cpp program of pyramid pattern

Image
In this example, how to print the pyramid pattern in the cpp programming language. Following is the code of the problem and the dry run description is in the last. Code of pyramid pattern:   #include<iostream> using namespace std; int main() { int num; cout<<" enter the length of  pyramid "<<endl; cin>>num; for(int i=0;i<num;i++) { for(int k=i;k>0;k--) { cout<<" "; } for(int j=i;j<num;j++) { cout<<"* "; } for(int k=num;k>0;k--) { cout<<" "; } cout<<endl; } return 0; } In the above example, divide the problem into three portions, one is the print space, the second is the asterisk, and the third is the again spaces. The first loop is used to printing the spaces which is terminate in half of the pyramid length, the second loop is used to printing the asterisk, and the third one is for again printing the spaces. In this way, the problem...

convert lower to upper case

Image
 In this example, you will learn about the conversion of the lower case to the upper case of the string. code: #include<iostream> #include<cstring> using namespace std; void lowertoupper(char *); int main() { char stringtake[100]; cout<<" Enter the string to convert into upper case"; cin.getline(stringtake,100); cout<<endl; lowertoupper(stringtake); return 0; } void lowertoupper(char *str) { for(int i=0;str[i]!='\0';i++) { if(str[i]>='a' && str[i]<='z') { str[i]-=32; } cout<<str[i]; } } Output: In this example, first of all, declare the function which accepts the char pointer which is used to convert the character array from lower to upper case.  In this function, one for loop is used which is terminated when null is reached, and if check check the char which are in between the lower case and sucstract 32 in the ascii of the character to convert into upper case.In the end,...