1.
Write a C++ Program to Multiply two Numbers, the product of num1 and num2 is
evaluated and the result is stored in variable product. Finally, the product is displayed
on the screen.
#include <iostream>
using namespace std;
int main() {
double num1, num2, product;
cout << "Enter two numbers: ";
// stores two floating point numbers in num1 and num2 respectively
cin >> num1 >> num2;
// performs multiplication and stores the result in product variable
product = num1 * num2;
cout << "Product = " << product;
return 0;
}
2. Write a C++ program to Check Whether Number is Even or Odd 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;
}
Or 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.";
return 0;
}
3. Write a C++ program to Find Sum of Natural Numbers using Recursive function
(Suppose, 10 is entered by the user. Now, 10 is passed to the add () function).
#include<iostream>
using namespace std;
int add(int n);
int main() {
int n;
cout << "Enter a positive integer: ";
cin >> n;
cout << "Sum = " << add(n);
return 0;
}
int add(int n) {
if(n != 0)
return n + add(n - 1);
return 0;
}
4. Write a C++ Program to Store Information of a Student in a Structure (In this
program, student (structure) is created. This structure has three
members: name (string), roll (integer) and marks (float). Then, a structure variable s is
created to store information and display it on the screen).
#include <iostream>
using namespace std;
struct student
{
char name[50];
int roll;
float marks;
};
int main()
{
student s;
cout << "Enter information," << endl;
cout << "Enter name: ";
cin >> s.name;
cout << "Enter roll number: ";
cin >> s.roll;
cout << "Enter marks: ";
cin >> s.marks;
cout << "\nDisplaying Information," << endl;
cout << "Name: " << s.name << endl;
cout << "Roll: " << s.roll << endl;
cout << "Marks: " << s.marks << endl;
return 0;
}
5. Write a C++ program to display Student details using class
#include<iostream>
using namespace std;
class student
{
private:
char name[20],regd[10],branch[10];
int sem;
public:
void input();
void display();
};
void student::input()
{
cout<<"Enter Name:";
cin>>name;
cout<<"Enter Regdno.:";
cin>>regd;
cout<<"Enter Branch:";
cin>>branch;
cout<<"Enter Sem:";
cin>>sem;
}
void student::display()
{
cout<<"\nName:"<<name;
cout<<"\nRegdno.:"<<regd;
cout<<"\nBranch:"<<branch;
cout<<"\nSem:"<<sem;
}
int main()
{
student s;
s.input();
s.display();
}
6. Write a C++ Program to Calculate Average of Numbers Using Arrays (The numbers
are stored in the float array num, which can store up to 100 floating-point numbers. If
the user enters a value of n above 100 or below 100, a while loop is executed which
asks the user to enter a value of n until it is between 1 and 100. Then, we use
a for loop to input the numbers from the user and store them in the num array. Every
time a number is entered by the user, its value is added to the sum variable. By the
end of the loop, the total sum of all the numbers is stored in sum. After storing all the
numbers, average is calculated and displayed).
#include <iostream>
using namespace std;
int main()
{
int n, i;
float num[100], sum=0.0, average;
cout << "Enter the numbers of data: ";
cin >> n;
while (n > 100 || n <= 0)
{
cout << "Error! number should in range of (1 to 100)." << endl;
cout << "Enter the number again: ";
cin >> n;
}
for(i = 0; i < n; ++i)
{
cout << i + 1 << ". Enter number: ";
cin >> num[i];
sum += num[i];
}
average = sum / n;
cout << "Average = " << average;
return 0;
}
7. C++ program to Find Sum of n Natural Numbers using Recursion
#include<iostream>
using namespace std;
int add(int n);
int main()
{
int n;
cout << "\nEnter any positive integer :: ";
cin >> n;
cout << "\nThe Sum of natural numbers upto [ "<<n<<" ] = " <<
add(n)<<"\n";
return 0;
}
int add(int n)
{
if(n != 0)
return n + add(n - 1);
return 0;
}
8. Write a C++ Program to print pyramid using numbers:
#include <iostream>
using namespace std;
int main()
{
int rows, count = 0, count1 = 0, k = 0;
cout << "Enter number of rows: ";
cin >> rows;
for(int i = 1; i <= rows; ++i)
{
for(int space = 1; space <= rows-i; ++space)
{
cout << " ";
++count;
}
while(k != 2*i-1)
{
if (count <= rows-1)
{
cout << i+k << " ";
++count;
}
else
{
++count1;
cout << i+k-2*count1 << " ";
}
++k;
}
count1 = count = k = 0;
cout << endl;
}
return 0;
}
9. Write a C++ Program to Display Multiplication table up to 10 only.
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter a positive integer: ";
cin >> n;
for (int i = 1; i <= 10; ++i) {
cout << n << " * " << i << " = " << n * i << endl;
}
return 0;
}
10. Write a C++ Program to Calculate Area of Circle and Area of Rectangle.
#include <iostream>
using namespace std;
int main(){
int length, breadth, radius, area_circle, area_rect;
cout << "Enter the length of the rectangle: ";
cin >> length;
cout << "Enter the breadth of the rectangle: ";
cin >> breadth;
area_rect = length * breadth;
cout << "Area of Rectangle: " << area_rect<<endl;
cout << "Enter the radius of the circle: ";
cin >> radius;
area_circle=3.14159*radius*radius;
cout<<"Area of Circle = "<<area_circle<<endl;
return 0;
}
Using Switch:
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
float length, breadth, radius, area_circle, area_rect;
int ch;
cout<<"1.Area Of Circle";
cout<<"\n2.Area Of Rectangle";
cout<<"\nEnter Your Choice :";
cin>>ch;
switch(ch)
{
case 1:
{
cout<<"\nEnter the Radius of Circle: ";
cin>>radius;
area_circle=3.14159*radius*radius;
cout<<"Area of Circle = "<<area_circle<<endl;
break;
}
case 2:
{
cout<<"\nEnter the Length and Breadth of Rectangle:";
cin>>length>>breadth;
area_rect=length*breadth;
cout<<"Area of Rectangle = "<<area_rect<<endl;
break;
}
default: cout<<"\n Invalid Choice Try Again...!!!";
break;
}
return 0;
}