Sie sind auf Seite 1von 6

//Program to Add Two Integers

#include<iostream>
using namespace std;
int main()
{
int a,b,sum;
cout<<"Enter the 1st number\n";
cin>>a;
cout<<"Enter the 2nd number\n";
cin>>b;
sum=a+b;
cout<<"Sum of two number "<<sum;
return 0;
}

//Swap Numbers (Using Temporary Variable)

#include <iostream>
using namespace std;
int main()
{
int a = 5, b = 10, temp;
cout << "Before swapping." << endl;
cout << "a = " << a << ", b = " << b << endl;
temp = a;
a = b;
b = temp;
cout << "\nAfter swapping." << endl;
cout << "a = " << a << ", b = " << b << endl;
return 0;
}

//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;
}

//Sum of Natural Numbers using loop


#include <iostream>
using namespace std;
int main()
{
int n, sum = 0;
cout << "Enter a positive integer: ";
cin >> n;
for (int i = 1; i <= n; ++i) {
sum += i;
}
cout << "Sum = " << sum;
return 0;
}

//Programs to print triangles using *, numbers and characters

#include <iostream>
using namespace std;
int main()
{
int rows;
cout << "Enter number of rows: ";
cin >> rows;
for(int i = 1; i <= rows; ++i)
{
for(int j = 1; j <= i; ++j)
{
cout << "* ";
}
cout << "\n";
}
return 0;
}

//C++ Program to Make a Simple Calculator to Add, Subtract, Multiply or Divide


Using switch...case
# include <iostream>
using namespace std;
int main()
{
char op;
float num1, num2;
cout << "Enter operator either + or - or * or /: ";
cin >> op;
cout << "Enter two operands: ";
cin >> num1 >> num2;
switch(op)
{
case '+':
cout << num1+num2;
break;
case '-':
cout << num1-num2;
break;
case '*':
cout << num1*num2;
break;
case '/':
cout << num1/num2;
break;
default:
// If the operator is other than +, -, * or /, error message is
shown
cout << "Error! operator is not correct";
break;
}
return 0;
}

//Displaying the elements of array using while loop


#include <iostream>
using namespace std;
int main(){
int arr[]={21,87,15,99, -12};

int i=0;
while(i<5){
cout<<arr[i]<<endl;
i++;
}
}

//A simple function example


#include <iostream>
using namespace std;

sum(int num1, int num2){


int num3 = num1+num2;
return num3;
}

int main(){
//Calling the function
cout<<sum(1,99);
return 0;
}

//Program : A) Design an employee class for reading and displaying the employee
information, the getInfo() and displayInfo() methods will be used respectively.
Where getInfo() will be a private method.

# include <iostream>

using namespace std ;

class employee
{
char name[20];
int no,age,salary;
void getInfo()
{
cout<<"Enter the employee details:\nName:";
cin>>name;
cout<<"employee ID:";
cin>>no;
cout<<"age:";
cin>>age;
cout<<"salary:";
cin>>salary;
}

public:
void displayInfo()
{
getInfo();
cout<<"\n\nEmployee Details:\n";
cout<<"\nName:"<<name;
cout<<"\nID:"<<no;
cout<<"\nAge:"<<age;
cout<<"\nSalary:"<<salary;
}
};

int main()
{
employee e;
e.displayInfo();
return 0 ;
}

//Program : B) Design the class student containing getData() and displayData() as


two of its methods which will be used for reading and displaying the student
information respectively. Where getData() will be a private method.

//Program : C) Design the class Demo which will contain the following methods: read
No(), factorial() for calculating the factorial of a number, reverse No() will
reverse the given number, is Palindrome() will check the given number is
palindrome, is Armstrong() which will calculate the given number is Armstrong or
not. Where read No() will be private method.

# include <iostream>

using namespace std ;

class demo
{
int n;
void readNo()
{
cout<<"enter the no "<<endl;
cin>>n;
}
public:void factorial();
void reverseNo();
void isPalindrome();
void isArmstrong();
};

void demo::factorial()
{
readNo();
int fact=1;
for(int i=1;i<=n;i++)
{
fact=fact*i;
}
cout<<"factorial="<<fact<<endl;
}

void demo::isPalindrome()
{
readNo();
int n1,d,sum;
sum=0;
n1=n;

while(n1!=0)
{
d=n1%10;
sum=d+(sum*10);
n1=n1/10;
}

if(sum==n)
cout<<"number is palindrome"<<endl;
else
cout<<"number is not a palindrome"<<endl;
}

void demo::reverseNo()
{
readNo();
int n1,d,sum;
sum=0;
n1=n;
while(n1!=0)
{
d=n1%10;
sum=d+(sum*10);
n1=n1/10;
}
cout<<"reverse of number="<<sum<<endl;
}

void demo::isArmstrong()
{
readNo();
char str[10];

int num=n,d,sum=0;
while(num!=0)
{
d=num%10;
sum=sum+(d*d*d);
num=num/10;
}
if(sum==n)
cout<<"number is armsrtong"<<endl;
else
cout<<"number is not a armstrong"<<endl;
}

int main()
{

demo d;
int a;
cout<<"Select from the following:\n1:factorial\n2:reverse\n3:palindrome\
n4:armstrong\n\n";
cin>>a;
switch(a)
{
case 1:
{
d.factorial();
break;
}

case 2:
{
d.reverseNo();
break;
}

case 3:
{
d.isPalindrome();
break;
}
case 4:
{
d.isArmstrong();
break;
}
}
return 0 ;
}

Das könnte Ihnen auch gefallen