C++ Program
C++ Program
#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:";
}
}
}
#include <iostream>
#include<string>
using namespace std;
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;
}
int main(){
int rows=4;
for( int i = 0; i < rows; i++ ) {
for( int j = 0; j <= i; j++ ){
cout << i + 1;
}
cout<<endl;
}
}