You are on page 1of 3

Implement a C++ program using a switch statement to perform basic calculator

operations (addition, subtraction, multiplication, and division).

#include<iostream>
using namespace std;
int main()
{
int num1,num2,c;
cout<<"Enter the two numbers: "<<endl;
cin>>num1>>num2;
cout<<"Enter the choice:"<<endl;
cout<<"1. addition"<<endl;
cout<<"2. subtraction"<<endl;
cout<<"3. multiplication"<<endl;
cout<<"4. division"<<endl;
cin>>c;
switch(c){
case 1:
{
int sum = num1+num2;
cout<<"addition: "<<sum;
break;
}
case 2:
{
int sub = num1-num2;
cout<<"subtraction: "<<sub;
break;
}
case 3:
{
int mul = num1*num2;
cout<<"multiplication: "<<mul;
break;
}
case 4:
{
int d = num1/num2;
cout<<"division: "<<d;
break;
}
default:
{
cout<<"Invalid input:";
}
}
}

Develop a C++ function to reverse a string.(like aman to nama)

#include <iostream>
#include<string>
using namespace std;

void revStr(string& str)


{
int n = str.length();
for (int i = 0; i < n / 2; i++)
swap(str[i], str[n - i - 1]);
}
int main()
{
string str = "aman";
revStr(str);
cout << str;
return 0;
}

Write a C++ program that uses a while loop to print the square of numbers from 1 to
5.

#include <iostream>
using namespace std;
int main()
{
int n=1;
while(n<6)
{
cout<<n*n<<endl;
n++;
}
return 0;
}

Create a C++ program with a function that checks if a given year is a leap year.

#include <iostream>
using namespace std;

int main() {

int year;
cout << "Enter a year: ";
cin >> year;
if (year % 400 == 0) {
cout << year << " is a leap year.";
}
else if (year % 100 == 0) {
cout << year << " is not a leap year.";
}
else if (year % 4 == 0) {
cout << year << " is a leap year.";
}
else {
cout << year << " is not a leap year.";
}

return 0;
}

Create a C++ program to print the following pattern:


1
22
333
4444
#include<iostream>
using namespace std;

int main(){

int rows=4;
for( int i = 0; i < rows; i++ ) {
for( int j = 0; j <= i; j++ ){
cout << i + 1;
}
cout<<endl;
}
}

You might also like