Beruflich Dokumente
Kultur Dokumente
#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;
}
#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;
}
#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;
}
int i=0;
while(i<5){
cout<<arr[i]<<endl;
i++;
}
}
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>
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 : 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>
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 ;
}