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

Practical in C++

c++ practicals

Uploaded by

maxicolyno
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)
42 views

Practical in C++

c++ practicals

Uploaded by

maxicolyno
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

PRACTICAL IN C++

NAME : CHANDRAKANT DASHARATH SONAWANE


Std : S.Y.BCA
PRACTICAL IN C++

➢ Demonstrate the use of if else and Control structure. (even or odd, sum and
average, prime or Composite, Sum difference, product and quotient of two
integer.)

• In C++, the if-else control statement is used for conditional execution of code blocks. It allows
you to control the flow of your program based on certain conditions. Here's the basic syntax of
the if-else statement.

if (condition) {

// Code to be executed if the condition is true

} else {

// Code to be executed if the condition is false

1. The ‘if’ keyword is followed by an expression enclosed in parentheses. This expression is the
condition that you want to evaluate. If the condition evaluates to ‘true’, the code block within
the curly braces immediately after the ‘if’ statement is executed. If the condition is ‘false’, the
code block is skipped.

2. Optionally, you can have an ‘else’ block that follows the ‘if’ block. The ‘else’ block contains code
that will be executed when the condition in the ‘if’ statement is false. It provides an alternative
path of execution.
PRACTICAL IN C++

1. Even or Odd:

#include <iostream>
using namespace std;

int main() {
int number;
cout << "Enter an integer: ";
cin >> number;

if (number % 2 == 0) {
cout << number << " is even." << endl;
} else {
cout << number << " is odd." << endl;
}

return 0;
}
PRACTICAL IN C++

2. Sum and Average:

#include <iostream>
using namespace std;

int main() {
int num1, num2;
cout << "Enter two integers: ";
cin >> num1 >> num2;

int sum = num1 + num2;


double average = static_cast<double>(sum) / 2;

cout << "Sum: " << sum << endl;


cout << "Average: " << average << endl;

return 0;
}
PRACTICAL IN C++

3. Prime or Composite :

#include <iostream>
using namespace std;

bool isPrime(int num) {


if (num <= 1)
return false;

for (int i = 2; i * i <= num; ++i) {


if (num % i == 0)
return false;
}

return true;
}

int main() {
int number;
cout << "Enter an integer: ";
cin >> number;

if (isPrime(number)) {
cout << number << " is a prime number." << endl;
} else {
cout << number << " is a composite number." << endl;
}

return 0;
}
PRACTICAL IN C++

4. Sum, Difference, Product, and Quotient :

#include <iostream>
using namespace std;

int main() {
int num1, num2;
cout << "Enter two integers: ";
cin >> num1 >> num2;

int sum = num1 + num2;


int difference = num1 - num2;
int product = num1 * num2;

if (num2 != 0) {
double quotient = static_cast<double>(num1) / num2;
cout << "Quotient: " << quotient << endl;
} else {
cout << "Cannot divide by zero." << endl;
}

cout << "Sum: " << sum << endl;


cout << "Difference: " << difference << endl;
cout << "Product: " << product << endl;

return 0;
}
PRACTICAL IN C++

5. Product, and Quotient Of two integer :

#include <iostream>
using namespace std;

int main() {
int num1, num2;
cout << "Enter two integers: ";
cin >> num1 >> num2;

int product = num1 * num2;

if (num2 != 0) {
double quotient = static_cast<double>(num1) / num2;
cout << "Product: " << product << endl;
cout << "Quotient: " << quotient << endl;
} else {
cout << "Cannot divide by zero." << endl;
}

return 0;
}

You might also like