C++ programming BBIT MAKAUT
Program Name:-Write a c++
program to print Hello world.
Program source code:-
#include<iostream>
using namespace std;
int main(){
cout<<"Hello
world.";
}
OUTPUT:-
Hello world.
Program Name:-Write a c++
program to add two integer number.
Source code:-
#include<iostream>
int main()
{
int a,b,c;
std::cout<<"Enter
two numbers for addition\n";
std::cin>>a>>b;
c=a+b;
std::cout <<"Sum of
entered numbers = " << c;
}
OUTPUT:-
Enter two numbers for addition
23
45
Sum of entered numbers = 68
Program name:-write a c++
program to find area and circumference of circle.
Source code:-
#include<iostream>
using namespace std;
float area_circle(float
radius);
float
circumference_circle(float radius);
int main()
{
float
radius,area,circumference;
cout<<"Enter the
radius of the circle to find the area and circumference\n";
cin>>radius;
area=area_circle(radius);
circumference=circumference_circle(radius);
cout<<"Area of the
circle is "<<area<<endl;
cout<<"circumference
of circle is "<<circumference;
}
float area_circle (float
radius)
{
float area;
area=3.14*radius*radius;
return area;
}
float
circumference_circle(float radius){
float circumference;
circumference=2*3.14*radius;
return circumference;
}
OUTPUT:-
Enter the radius of the circle
to find the area and circumference
5
Area of the circle is 78.5
circumference of circle is
31.4
Program name:-write a c++
program to convert celsius to fahrenheit and vice versa.
Sourcee code -
#include<iostream>
using namespace std;
float
celsius_To_Fahrenheit(float celsius);
float
fahrenheit_To_celsius(float fahrenheit);
int main(){
float
celsius,fahrenheit,celsi,entered_fahrenheit;
cout<<"Enter
celcius to convert to fahrenheit ";
cin>>celsius;
fahrenheit=celsius_To_Fahrenheit(celsius);
cout<<fahrenheit;
cout<<"\nEnter
fahrenheit to convert to celsius ";
cin>>entered_fahrenheit;
celsi=fahrenheit_To_celsius(fahrenheit);
cout<<celsi;
}
float
celsius_To_Fahrenheit(float celsius){
float fahrenheit;
//T(°F) = 20°C × 9/5 + 32
= 68 °F
fahrenheit=((celsius*9)/5)+32;
return fahrenheit;
}
float
fahrenheit_To_celsius(float entered_fahrenheit){
float celsi;
//T(°C) = (68°F - 32) ×
5/9 = 20 °C
celsi=((entered_fahrenheit-32)*5)/9;
return celsi;
}
OUTPUT-
Enter celcius to convert to
fahrenheit 20
68
Enter fahrenheit to convert to
celsius 68
20
Program name:-write a c++
program to find factorial of a number using function.
Source code-
#include<iostream>
using namespace std;
int fact(int num);
int main()
{
int num,factorial;
cout<<"Enter a
number to find its factorial\n";
cin>>num;
factorial=fact(num);
cout<<"The
factorial of the entered number is \n"<<factorial;
}
int fact(int num)
{
int i,factorial=1;
for(i=1;i<=num;i++)
{
factorial=factorial*i;
}
return factorial;
}
OUTPUT-
Enter a number to find its
factorial
5
The factorial of the entered
number is
120
Program name:-write a c++
program to find sum of series of fibonacci series.
Source code-
#include<iostream>
using namespace std;
int main()
{
int
first=0,second=1,fibonacci,n;
cout<<"Enter the
number for fibbonacci serires ";
cin>>n;
for(int i=0;i<n;i++)
{
if(i<=1)
fibonacci=i;
else
{
fibonacci=first + second;
first=second;
second=fibonacci;
}
cout<<(fibonacci);
}
}
Output-
0 1 1 2 3
Program name:-write a c++
program to find maximum and minimum in an array.
Source code-
#include<iostream>
using namespace std;
int arr_max_min(int arr[],int
num);
int main()
{
int num,i,arr[100],result;
cout<<"Enter the
size of the array\n";
cin>>num;
cout<<"Enter
numbers in the array one by one \n";
for(i=0;i<num;i++)
{
cin>>arr[i];
}
result=arr_max_min(arr,num);
}
int arr_max_min(int arr[],int
num)
{
int max,min,i;
max=arr[0];
for(i=0;i<num;i++)
{
if(arr[i]>max)
max=arr[i];
}
cout<<"The maximuum
in the array is \t"<<max;
for(i=0;i<num;i++)
{
min=arr[0];
if(arr[i]<min)
min=arr[i];
//return min;
}
cout<<"\nThe
minimum in the array is\t"<<min;
}
OUTPUT-
Enter the size of the array
5
Enter numbers in the array one
by one
23
56
78
12
36
The maximuum in the array is
78
The minimum in the array is 23
Program name:-write a c++
program to find odd numbers in range.
Source code-
#include<iostream>
using namespace std;
int main(){
int min,max,num;
cout<<"Enter
minimum range ";
cin>>min;
cout<<"Enter
maximum range ";
cin>>max;
for(num=min;num<=max;num++)
if(num%2!=0)
cout<<num<<"\t";
}
OUTPUT-
Enter minimum range 1
Enter maximum range 10
1 3 5 7 9
Program name:-write a c++
program to find whether a number is positive or negative.
Source code-
#include<iostream>
using namespace std;
int main(){
int number;
cout<<"Enter a
number ";
cin>>number;
if(number>0)
cout<<"Negative";
else
cout<<"Positive";
}
OUTPUT-
Enter a number 5
Negative
Program name:-write a c++
program to print prime factor of a number.
Source code-
#include<stdio.h>
void prime_number(int num);
int main()
{
int num;
printf("Enter a number
for prime factor ");
scanf("%d",&num);
prime_number(num);
}
void prime_number(int num)
{
int i=2;
while(num!=1)
{
if(num%i==0)
{
while(num%i==0)
{
printf("%d\t",i);
num=num/i;
}
}
i++;
}
}
OUTPUT-
Enter a number for prime
factor 12
2 2 3
Program name:-write a c++
program to print pascal triangle using only star(*).
Source code-
#include<iostream>
using namespace std;
int main()
{
int row,col,num,n,space;
cout<<"Enter the
number of rows you want to print\n";
cin>>num;
for(row=0;row<num;row++)
{
for(space=0;space<=num-row;space++)
{
cout<<"
";
}
n=1;
for(col=0;col<=row;col++)
{
cout<<"
"<<"*";
n=n*(row-col)/(col+1);
}
cout<<"\n";
}
}
OUTPUT-
Enter the number of rows you
want to print
5
*
* *
* * *
* * * *
* * * * *
Program name:-write a c++
program to implement class of a person containing name and age.
Source code-
#include<iostream>
using namespace std;
class person
{
char name[50];
int age;
public:
void getdata();
void putdata();
};
void person::getdata()
{
cout<<"Enter the
name : ";
cin>>name;
cout<<"Enter the
age";
cin>>age;
}
void person::putdata()
{
cout<<"Name :
"<<name;
cout<<endl<<"age
: "<<age;
}
int main()
{
person m;
m.getdata();
m.putdata();
return 0;
}
OUTPUT-
Enter the name : Bum
Enter the age 21
Name : Bum
age : 21
Program name:-write a c++
program to implement class book and calculate the price of a book.
Source code-
#include<iostream>
using namespace std;
class Book{
int Book_num,Quantity_Book;
char Book_Title[20];
float price;
public:
void Input();
float Total_Cost()
{
float cost=Quantity_Book *
price;
return cost;
}
void Display();
};
void Book::Input(){
cout<<"Number of
books ";
cin>>Book_num;
cout<<"Title of
the Book ";
cin>>Book_Title;
cout<<"Quantity of
Book ";
cin>>Quantity_Book;
cout<<"Enter the
Price of Book ";
cin>>price;
}
void Book::Display(){
cout<<"Numbers of
Books "<<Book_num<<endl;
cout<<"Title of
Book "<<Book_Title<<endl;
cout<<"Quantity of
Book "<<Quantity_Book<<endl;
cout<<"Total cost
is "<<Total_Cost();
}
int main(){
Book b;
b.Input();
b.Display();
}
OUTPUT-
Number of books 2
Title of the Book
EnglishLitreature
Quantity of Book 3
Enter the Price of Book 200.60
Numbers of Books 2
Title of Book
EnglishLitreature
Quantity of Book 3
Total cost is 601.8
Comments
Post a Comment