0% found this document useful (0 votes)
34 views

If-Else Solution

The document provides code solutions to common programming problems using concepts like if/else statements, loops, functions. It includes 6 problems and solutions: 1) Checking if a number is even or odd. 2) Checking if a character is a vowel or consonant. 3) Finding the largest of three numbers. 4) Finding the roots of a quadratic equation. 5) Calculating the sum of the first N natural numbers. 6) Checking if a year is a leap year. The solutions demonstrate the use of control flow statements and basic math operations to solve each problem in 1-2 well commented code snippets.

Uploaded by

Aniket Pawar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

If-Else Solution

The document provides code solutions to common programming problems using concepts like if/else statements, loops, functions. It includes 6 problems and solutions: 1) Checking if a number is even or odd. 2) Checking if a character is a vowel or consonant. 3) Finding the largest of three numbers. 4) Finding the roots of a quadratic equation. 5) Calculating the sum of the first N natural numbers. 6) Checking if a year is a leap year. The solutions demonstrate the use of control flow statements and basic math operations to solve each problem in 1-2 well commented code snippets.

Uploaded by

Aniket Pawar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

SOLUTION BOOK

❤️ From SIDDHARTH SINGH

IF-ELSE|
1) Check Whether Number is Even or Odd
Method 1: Using if else
#include <iostream>
using namespace std;

int main() {
int n;

cout << "Enter an integer: ";


cin >> n;

if ( n % 2 == 0)
cout << n << " is even.";
else
cout << n << " is odd.";

return 0;
}
Output

Enter an integer: 23
23 is odd.

Method 2: Using ternary operators


#include <iostream>
using namespace std;

int main() {
int n;

cout << "Enter an integer: ";


cin >> n;

(n % 2 == 0) ? cout << n << " is even." : cout << n << " is odd.";
SOLUTION BOOK
❤️ From SIDDHARTH SINGH
return 0;
}
CONCEPT:
We used ternary operators ?: instead of if..else statement. The ternary operator is a
shorthand notation of if...else statement.

2) Check Whether a character is Vowel or Consonant.


CODE:
#include <iostream>
using namespace std;

int main() {
char c;
bool isLowercaseVowel, isUppercaseVowel;

cout << "Enter an alphabet: ";


cin >> c;

// evaluates to 1 (true) if c is a lowercase vowel


isLowercaseVowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');

// evaluates to 1 (true) if c is an uppercase vowel


isUppercaseVowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');

// show error message if c is not an alphabet


if (!isalpha(c))
printf("Error! Non-alphabetic character.");
else if (isLowercaseVowel || isUppercaseVowel)
cout << c << " is a vowel.";
else
cout << c << " is a consonant.";

return 0;
}
Output
Enter an alphabet: u
u is a vowel.
CONCEPT:
SOLUTION BOOK
❤️ From SIDDHARTH SINGH
The isalpha() function checks whether the character entered is an alphabet or not. If it is
not, it prints an error message.

3) Program to Find Largest Number Among Three Numbers

Method 1: Using if...else Statement


#include <iostream>
using namespace std;

int main() {
float n1, n2, n3;

cout << "Enter three numbers: ";


cin >> n1 >> n2 >> n3;

if((n1 >= n2) && (n1 >= n3))


cout << "Largest number: " << n1;
else if ((n2 >= n1) && (n2 >= n3))
cout << "Largest number: " << n2;
else
cout << "Largest number: " << n3;

return 0;
}
Output
Enter three numbers: 2.3
8.3
-4.2
Largest number: 8.3

Method 2: Using Nested if...else statement


#include <iostream>
using namespace std;

int main() {
float n1, n2, n3;

cout << "Enter three numbers: ";


cin >> n1 >> n2 >> n3;
SOLUTION BOOK
❤️ From SIDDHARTH SINGH
if (n1 >= n2) {
if (n1 >= n3)
cout << "Largest number: " << n1;
else
cout << "Largest number: " << n3;
}
else {
if (n2 >= n3)
cout << "Largest number: " << n2;
else
cout << "Largest number: " << n3;
}

return 0;
}

Output
Enter three numbers: 2.3
8.3
-4.2
Largest number: 8.3

4) Program to Find All Roots of a Quadratic Equation


CODE:
#include <iostream>
#include <cmath>
using namespace std;

int main() {

float a, b, c, x1, x2, discriminant, realPart, imaginaryPart;


cout << "Enter coefficients a, b and c: ";
cin >> a >> b >> c;
discriminant = b*b - 4*a*c;

if (discriminant > 0) {
x1 = (-b + sqrt(discriminant)) / (2*a);
x2 = (-b - sqrt(discriminant)) / (2*a);
cout << "Roots are real and different." << endl;
cout << "x1 = " << x1 << endl;
SOLUTION BOOK
❤️ From SIDDHARTH SINGH
cout << "x2 = " << x2 << endl;
}

else if (discriminant == 0) {
cout << "Roots are real and same." << endl;
x1 = -b/(2*a);
cout << "x1 = x2 =" << x1 << endl;
}

else {
realPart = -b/(2*a);
imaginaryPart =sqrt(-discriminant)/(2*a);
cout << "Roots are complex and different." << endl;
cout << "x1 = " << realPart << "+" << imaginaryPart << "i" << endl;
cout << "x2 = " << realPart << "-" << imaginaryPart << "i" << endl;
}

return 0;
}
Output

Enter coefficients a, b and c: 4


5
1
Roots are real and different.
x1 = -0.25
x2 = -1
CONCEPT:
In this program, sqrt() library function is used to find the square root of a number.
5) Program to Calculate Sum of first N Natural Numbers
We have to display the value of 1+2+3+....+N.

CODE:
#include <iostream>
using namespace std;

int main() {
int n, sum = 0;

cout << "Enter a positive integer: ";


SOLUTION BOOK
❤️ From SIDDHARTH SINGH
cin >> n;

for (int i = 1; i <= n; ++i) {


sum += i;
}

cout << "Sum = " << sum;


return 0;
}
Output

Enter a positive integer: 50


Sum = 1275

NOTE: If a user enters a negative number, Sum = 0 is displayed and program is


terminated.

6) Program to Check Leap Year


CODE:
#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.";
SOLUTION BOOK
❤️ From SIDDHARTH SINGH
return 0;
}
Output
Enter a year: 2014
2014 is not a leap year

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy