![]() |
krishna raj kharel |
[1]// Write a program to display string " C++ is an object oriented language".
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
cout<<"C++ is an object oriented language."<<endl;
getch();
return(0);
}
[2]// Write a program to read a temperature in Celsius and convert into Fahrenheit.
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
float celsius,fahrenheit;
cout<<"Enter the temperature in Celsius : ";
cin>>celsius;
cout<<endl<<endl<<endl;
fahrenheit=(celsius*9)/5+32;
cout<<"Temperature in Fahrenheit = "<<fahrenheit<<endl;
getch();
return(0);
}
[3]//Write a program to find area and circumference of circle. [use const qualifier for PI]
#include<iostream>
#include<conio.h>
using namespace std;
const float pi=3.1415;
int main()
{
float r,area,circum;
cout<<"Enter the ardius of the circle : ";
cin>>r;
area=pi*r*r;
circum=2*pi*r;
cout<<endl<<endl;
cout<<"Area : "<<area<<endl;
cout<<"Circumference : "<<circum<<endl;
getch();
return(0);
}
[4]/* Write a program to display the following:
Location Population
=======================
Kirtipur 12345
Patan 32452
Kathmandu 556342
Manag 3421 */
#include<iostream>
#include<iomanip>
#include<conio.h>
using namespace std;
int main()
{
cout<<setw(12)<<left<<"Location";
cout<<setw(10)<<right<<"Population"<<endl;
cout<<"======================"<<endl;
cout<<setw(12)<<left<<"Kritipur"<<setw(10)<<right<<12345<<endl;
cout<<setw(12)<<left<<"Patan"<<setw(10)<<right<<32452<<endl;
cout<<setw(12)<<left<<"Kathmandu"<<setw(10)<<right<<556342<<endl;
cout<<setw(12)<<left<<"Manag"<<setw(10)<<right<<3421<<endl;
getch();
return(0);
}
[5]/* Write a program to assign a=137. And display value of a as:
+####137 */
#include<iostream>
#include<iomanip>
#include<conio.h>
using namespace std;
int main()
{
int a=137;
cout<<"+"<<setw(7)<<setfill('#')<<right<<a;
getch();
return(0);
}
[6]/* Write a program to read a five digit number and separate the digit and print them separated by 3 spaces. */
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int n,i,m;
start:
cout<<"Enter a five digit number : ";
cin>>n;
if(n>99999)
goto start;
for(i=0;i<5;i++)
{
m=n%10;
n=n/10;
cout<<n<<" ";
}
getch();
return(0);
}
// 8. Write a program to read a character and print its integer equivalent. (use type cast)
#include<iostream>
using namespace std;
int main()
{
char a;
cout<<"Enter any character : ";
cin>>a;
cout<<endl<<endl;
cout<<"The equivalent integer value of "<<a<<" is "<<static_cast<int>(a)<<endl;
return(0);
}
// 9. Write a program to read two numbers and find sum,difference,product and division.
#include<iostream>
using namespace std;
int main()
{
float a,b,sum=0.00,diff=0.00,pro=0.00,div=0.00;
cout<<"Number a = ";
cin>>a;
cout<<endl;
cout<<"Number b= ";
cin>>b;
sum=a+b;
diff=a-b;
pro=a*b;
div=a/b;
cout<<endl<<endl;
cout<<a<<" + "<<b<<" = "<<sum<<endl;
cout<<a<<" - "<<b<<" = "<<diff<<endl;
cout<<a<<" * "<<b<<" = "<<pro<<endl;
cout<<a<<" / "<<b<<" = "<<div<<endl;
return(0);
}
// 10. Write a program to find cube of (a-b).
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
float a,b;
cout<<"a = ";
cin>>a;
cout<<endl;
cout<<"b= ";
cin>>b;
cout<<endl<<endl;
cout<<"The cube of ("<<a<<"-"<<b<<") is "<<pow((a-b),3);
return(0);
}
// 11. Write a program to find smallest among four numbers using conditional operator.
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a,b,c,d,l;
cout<<"a=";
cin>>a;
cout<<"b=";
cin>>b;
cout<<"c=";
cin>>c;
cout<<"d=";
cin>>d;
l=a<b?a:b;
l=l<c?l:c;
l=l<d?l:d;
cout<<"Smallest number="<<l<<endl;
getch();
return(0);
}
// 12. Write a program to check whether a year is leap or not.
#include<iostream>
using namespace std;
int main()
{
int year;
cout<<"Year = ";
cin>>year;
if(year%4==0)
cout<<year<<" is a leap year.";
else
cout<<year<<" is not a leap year.";
return(0);
}
//13. Write a program to check whether a number is odd or even.
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int n;
cout<<"Enter any number : ";
cin>>n;
if(n%2==0)
cout<<n<<" is even";
else
cout<<n<<" is odd";
getch();
return(0);
}
// 14. Write a program to read three different numbers and find the median number.
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a,b,c,m;
cout<<"Enter any three number (a,b,c) : " ;
cin>>a>>b>>c;
if(a<b)
{
if(a<c)
{
if(b<c)
m=b;
else
m=c;
}
else
m=a;
}
else
{
if(b<c)
{
if(a<c)
m=a;
else
m=c;
}
else
m=b;
}
cout<<"Median = "<<m;
getch();
return(0);
}
// 15. Write a program to read three integers and find sum, average, product, largest and smallest of these numbers.
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
float a,b,c,sum=0.00,avg=0.00,pro=0.00,large,small;
cout<<"Enter any three number (a,b,c) : ";
cin>>a>>b>>c;
sum=a+b+c;
avg=sum/3;
pro=a*b*c;
if(a>b&&a>c)
large=a;
else if(b>a&&b>c)
large=b;
else
large=c;
if(a<b&&a<c)
small=a;
else if(b<a&&b<c)
small=b;
else
small=c;
cout<<endl<<endl;
cout<<"Sum = "<<sum<<endl;
cout<<"Average = "<<avg<<endl;
cout<<"Product = "<<pro<<endl;
cout<<"Largest Number = "<<large<<endl;
cout<<"Smallest Number = "<<small<<endl;
getch();
return(0);
}
// 16. Write a program to read to numbers and checks whether first is multiple of second.
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a,b;
cout<<"Enter any two numbers (a,b) : ";
cin>>a>>b;
if(a%b==0)
cout<<a<<" is multiple of "<<b;
else
cout<<a<<" is not multiple of "<<b;
getch();
return(0);
}
/* 21. Write a menu base program for
a. Area of rectangle
b. Perimeter of rectangle
c. exit */
#include<iostream>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
using namespace std;
void menu();
void clear();
int main()
{
char ch;
float l,b;
do{
clear();
menu();
ch=getch();
switch(ch)
{
case 'a':
cout<<"Enter Length & Breadth sequently : ";
cin>>l>>b;
cout<<"\n\nParameter = "<<2*(l+b);
getch();
clear();
menu();
case 'b':
cout<<"Enter Length & Breadth sequently : ";
cin>>l>>b;
cout<<"\n\nArea = "<<l*b;
getch();
clear();
menu();
case 'c':
break;
}
}while(ch!='c');
getch();
return(0);
}
void clear()
{
system("cls");
}
void menu()
{
cout<<"================================================\n";
cout<<"Press a for Area of rectangle.\n";
cout<<"Press b for Perimeter of rectangle.\n";
cout<<"Press c for Exit.\n";
}
/* 22. write a menu base program to perform calculation, according to following choice:
+ for addition
- For subtraction
* For multiplication
/ For division */
#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>
using namespace std;
int main()
{
float x,y;
char ch;
cout<<"Enter any two number (x,y) : ";
cin>>x>>y;
system("cls");
do{
cout<<"\n==========================================\n";
cout<<"+ for addition\n";
cout<<"- for subtraction\n";
cout<<"* for multiplication\n";
cout<<"/ for divisoion\n";
cout<<"Press enter to exit\n";
cout<<"==========================================\n";
cout<<"Enter your choice : \n";
ch=getch();
switch(ch)
{
case'+':
cout<<x<<" + "<<y<<" = "<<x+y;
break;
case'-':
cout<<x<<" - "<<y<<" = "<<x-y;
break;
case'*':
cout<<x<<" * "<<y<<" = "<<x*y;
break;
case'/':
cout<<x<<" / "<<y<<" = "<<x/y;
break;
}
}while(ch!='\r');
getch();
return(0);
}
/* 23. Write a program to reverse a number.
*/
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int n,rev=0;
cout<<"Enter any number : ";
cin>>n;
while(n!=0)
{
int r=n%10;
rev=rev*10+r;
n=n/10;
}
cout<<"\n\nReversed number is : "<<rev;
getch();
return(0);
}
// 25. Write a program to display multiples of any entered number. (Result should displayed in 10 column and 20 lines.)
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int n,i,j,m=0;
cout<<"Enter any number to display it's multiple : ";
cin>>n;
cout<<"\nThe multiple of "<<n<<" is given below\n\n";
for(i=1,j=1;i<=20;i++,j++)
{
m=m+n;
cout<<m<<"\t";
if(j==10)
{
cout<<"\n";
j=0;
}
}
getch();
return(0);
}
// 26. Write a program to check whether given number is prime or not.
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int n;
bool flag=0;
cout<<"Enter a number to check if prime or not : ";
cin>>n;
for(int i=2;i<=n/2;i++)
{
if(n%i==0)
{
flag =1;
break;
}
}
if(flag==0)
cout<<"\nThe number "<<n<<" is a prime number.";
else
cout<<"\nThe number "<<n<<" is not a prime number.";
getch();
return(0);
}
// 27. Write a program to count total numbers of prime number between 1 to given number ‘n’.
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int m,prime=0;
cout<<"Enter a number to count total numbers of prime number between 1 to entered number : ";
cin>>m;
for(int j=2;j<=m;j++)
{
for(int i=2;i<=j/2;i++)
{
if(j%i==0)
{
prime++;
break;
}
}
}
cout<<"\nTotal number of prime number bewteen 1 to "<<m<<" is "<<prime;
getch();
return(0);
}
// 28. Write a program to find the factorial of number.
#include<iostream>
#include<conio.h>
using namespace std;
long int fact(int n);
int main()
{
int num;
cout<<"Enter any number to find out it's factorial : ";
cin>>num;
cout<<"\n\nThe factorial of "<<num<<" is "<<fact(num);
getch();
return(0);
}
long int fact(int n)
{
if(n==0)
return(1);
else
return(n*fact(n-1));
}
// 29. Write a program to display the first ‘n’ terms of Fibonacci series.
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int n;
cout<<"For Fibonacci Series\n\n";
cout<<"Enter any number : ";
cin>>n;
cout<<"\n\n";
int a=0,b=1,c=0;
for(int i=1;i<=n;i++)
{
c=a+b;
cout<<a<<" ";
a=b;
b=c;
}
getch();
return(0);
}
// 30. Write a program to convert binary to decimal.
#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
using namespace std;
int main()
{
int n,p,b,d=0,i=0;
cout<<"Enter the binary number : ";
cin>>n;
do
{
b=n%10;
if(b==1||b==0)
goto now;
else
{
cout<<"You haven't entered binary no.\n";
cout<<"Try again\n";
getch();
exit(0);
}
now:
p=b*pow(2,i);
d=d+p;
i++;
n/=10;
}
while(n!=0);
cout<<"The num is decimal is "<<d<<endl;
getch();
return(0);
}
#include<iostream>
using namespace std;
int main()
{
for(int i=1;i<=8;i++)
{
for(int j=1;j<=8;j++)
{
if((i+j)%2==0)
cout<<static_cast<char>(219);
else
cout<<static_cast<char>(255);
}
cout<<endl;
}
return 0;
}
//code to display chess-board
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int i,j,k;
cout<<"Chess board:\n";
cout<<"______________";
cout<<"\n\n";
for(k=0;k<4;k++)
{
cout<<"\t\t";
for(i=0;i<4;i++)
{
cout<<""<<static_cast<char>(219)<<""<<static_cast<char>(255);
}
cout<<endl;
cout<<"\t\t";
for(j=0;j<4;j++)
{
cout<<""<<static_cast<char>(255)<<""<<static_cast<char>(219);
}
cout<<endl;
}
cout<<"\n\n";
return 0;
}
No comments:
Post a Comment