0% found this document useful (0 votes)
10 views8 pages

Assignment

Uploaded by

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

Assignment

Uploaded by

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

Name: Zain

Department: Artificial Intelligence


Roll Number: 21F-BsAI-13
Subject: Programming Fundamentals
Q1. Write a C/C++ program that takes a number from the user and prints whether it is even or
odd using if-else statement.

Answer.

#include <iostream>

using namespace std;

int main() {

int number;

cout << "Enter an integer: ";

cin >> number;

if (number % 2 == 0) {

cout << number << " is even." << en


dl;

} else {

cout << number << " is odd." << endl;

return 0;

Output:
Q2. Write a program to find the largest of three numbers using if-else-if ladder.

Answer:

#include <iostream>

using namespace std;

int main() {

int a, b, c;

cout << "Enter three integers: ";

cin >> a >> b >> c;

if (a >= b && a >= c) {

cout << "The largest number is: " << a << endl;

} else if (b >= a && b >= c) {

cout << "The largest number is: " << b << endl;

} else {

cout << "The largest number is: " << c << endl;

return 0;

Output:
Q.3 Use switch statement to create a simple calculator (addition, subtraction, multiplication,
division).

Answer:

#include <iostream>

using namespace std;

int main() {

double num1, num2;

char op;

cout << "Enter first number: ";

cin >> num1;

cout << "Enter an operator (+, -, *, /): ";

cin >> op;

cout << "Enter second number: ";

cin >> num2;

switch(op) {

case '+':

cout << "Result: " << num1 + num2 << endl;

break;

case '-':

cout << "Result: " << num1 - num2 << endl;

break;
case '*':

cout << "Result: " << num1 * num2 << endl;

break;

case '/':

if (num2 != 0)

cout << "Result: " << num1 / num2 << endl;

else

cout << "Error: Division by zero!" << endl;

break;

default:

cout << "Invalid operator!" << endl;

return 0;

Output:
Q.4 Write a program that prints numbers from 1 to 100 using a for loop.

Answer:

#include <iostream>

using namespace std;

int main() {

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

cout << i << " ";

return 0;

Output:
Q.5 Write a program using while loop that calculates the sum of first 10 natural numbers.

Answer:

#include <iostream>

using namespace std;

int main() {

int i = 1, sum = 0;

while (i <= 10) {

sum += i;

i++;

cout << "The sum of the first 10 natural numbers is: " << sum << endl;

return 0;

Output:
Q.6 Write a program that takes a number and prints its multiplication table using dowhile
loop.

Answer:

#include <iostream>

using namespace std;

int main() {

int num, i = 1;

cout << "Enter a number to print its multiplication table: ";

cin >> num;

do {

cout << num << " x " << i << " = " << num * i << endl;

i++;

} while (i <= 10);

return 0;

Output:

You might also like