In if…else statement, if the condition is true then the statements inside if statements are executed, and if the condition is false then statements inside else are executed. Syntax if(Condition or Expression) { //Statements } else { //Statements } Example #include <iostream> using namespace std; int main() { int number; cout << "Enter an integer value: "; cin >> number; if ( number >= 0) { cout << "You entered a positive integer: " << number << endl; } else { cout << "You entered a negative integer: " << number << endl; } cout << "This line is always…
-
-
In simple if statement the line of code inside if statement are executed if the condition or expression is true. Syntax if (Expression or Condition) { // statements } Example #include <iostream> using namespace std; int main() { int number; cout << "Enter an integer: "; cin >> number; //if number is positive then statements inside if statements are executed. if ( number > 0) { cout << "You entered a positive integer: " << number << endl; } cout << "This statement is always executed."; return 0; } OutPut Enter an integer: 5 You entered a positive number: 5…
-
# include <iostream> using namespace std; int main() { char operation; float no1, no2; cout << "Enter operationerator either + or - or * or /: "; cin >> operation; cout << "Enter two operationerands: "; cin >> no1 >> no2; switch(operation) { case '+': cout << no1+no2; break; case '-': cout << no1-no2; break; case '*': cout << no1*no2; break; case '/': cout << no1/no2; break; default: cout << "Error! operation is not correct"; break; } return 0; } OutPut Enter operator either + or - or * or divide : + Enter two operands: 3.4 8.4 3.4 -…
-
All years which are perfectly divisible by 4 are leap years except for century years. #include <iostream> using namespace std; int main() { int year; cout << "Enter a year: "; cin >> year; if (year % 4 == 0) { if (year % 100 == 0) { if (year % 400 == 0) cout << year << " is a leap year."; else cout << year << " is not a leap year."; } else cout << year << " is a leap year."; } else cout << year << " is not a leap year."; return 0; }…
-
A positive integer is called an Armstrong number if the sum of cubes of individual digit is equal to that number itself. #include <iostream> using namespace std; int main() { int origNum, num, rem, sum = 0; cout << "Enter a positive three digit integer value: "; cin >> origNum; num = origNum; while(num != 0) { rem = num % 10; sum += rem * rem * rem; num /= 10; } if(sum == origNum) cout << origNum << " is an Armstrong number."; else cout << origNum << " is not an Armstrong number."; return 0; } OutPut…
-
A positive integer value which is only divisible by 1 and itself is known as prime number. #include <iostream> using namespace std; int main() { int n, i, flag=0; cout << "Enter a positive integer value: "; cin >> n; for(i = 2; i <= n / 2; ++i) { if(n % i == 0) { flag = 1; break; } } if (flag==0) cout << "This is a prime number"; else cout << "This is not a prime number"; return 0; } OutPut Enter a positive integer value: 29 This is a prime number.
-
If the original value and its reversed value is same then it will called palindrome value. #include <iostream> using namespace std; int main() { int n, num, digit, rev = 0; cout << "Enter a positive integer number: "; cin >> num; n = num; do { digit = num % 10; rev = (rev * 10) + digit; num = num / 10; } while (num != 0); cout << " The reverse of the number is: " << rev << endl; if (n == rev) cout << " The number is a palindrome."; else cout << " The…
-
In this program, the user entered one integer value and it will find reverese of that number. #include <iostream> using namespace std; int main() { int no, rev = 0, rem; cout << "Enter an integer value: "; cin >> no; while(no != 0) { rem = no%10; rev = rev*10 + rem; no /= 10; } cout << "Reversed Number = " << rev; return 0; } OutPut Enter an integer value: 31122000 Reversed number = 00022113
-
A character variable holds ASCII value means an integer number between 0 and 127 rather than that character itself in C programming. That value is known as ASCII value. #include <iostream> using namespace std; int main() { char c; cout << "Enter a character: "; cin >> c; cout << "ASCII Value of " << c << " is " << int(c); return 0; } OutPut Enter a character: A ASCII Value of A is 65
-
The size of each variable is find using sizeof operator. #include <iostream> using namespace std; int main() { cout << "Size of char: " << sizeof(char) << " byte" << endl; cout << "Size of int: " << sizeof(int) << " bytes" << endl; cout << "Size of float: " << sizeof(float) << " bytes" << endl; cout << "Size of double: " << sizeof(double) << " bytes" << endl; return 0; } OutPut Size of char: 1 byte Size of int: 4 bytes Size of float: 4 bytes Size of double: 8 bytes