Posts

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 ;     return 0; } Output: Enter a decimal number: 54 54 in decimal = 110110 in binary

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: A A is a vowel. Here, in the above program if-else is

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() {     int num1, num2, hcf;          cout << "Enter first numbers "<<endl;     cin >> num1;          cout << "Enter second numbers "<&l

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++;     }           return 0; } Output: Enter two numbers: 4 3 LCM = 12 Here, take two numbers from the user, find the max number by using if-else statement, In the last apply while loop. In the loop, find the number which is divisible by both numbers, if not then increase the value of maximum until the

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";                                       }         else             cout << years << " is a leap year.";     }     else         cout << years << " is not a leap year.";     return 0; } Output Enter a year: 1700 1700 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