Sample C++ programs
26. C++ program to print Helloworld
#include <iostream>
using namespace std;
int main()
{
cout << "Hello
World";
return 0;
}
27. C++ program to find factorial of number in C++
#include<iostream>
using namespace std;
int main()
{
int num,factorial=1;
cout<<" Enter
Number To Find Its Factorial: ";
cin>>num;
for(int a=1;a<=num;a++)
{
factorial=factorial*a;
}
cout<<"Factorial of Given Number is
="<<factorial<<endl;
return 0;
}
28. C++ program code to swap the values of two integer
#include<iostream>
using namespace std;
int main()
{
int var1, var2, swap;
cout<<"Enter value
for first integer: ";
cin>>var1;
cout<<"Enter value
for second integer: ";
cin>>var2;
cout<<" Values Before
swapping: "<<endl;
cout<<"First Integer
="<<var1<<endl;
cout<<"Second
Interger ="<<var2<<endl;
swap=var1;
var1=var2;
var2=swap;
cout<<" Values After
swapping: "<<endl;
cout<<"First Integer
="<<var1<<endl;
cout<<"Second
Interger ="<<var2<<endl;
return 0;
}
29. C++ program to find Fibonacci Series upto given range
#include<iostream>
using namespace std;
int main()
{
int range, first = 0,
second = 1, fibonicci=0;
cout << "Enter Range
for Terms of Fibonacci Sequence: ";
cin >> range;
cout <<
"Fibonicci Series upto " << range << " Terms
"<< endl;
for ( int c = 0 ; c <
range ; c++ )
{
if ( c <= 1 )
fibonicci = c;
else
{
fibonicci = first +
second;
first = second;
second = fibonicci;
}
cout << fibonicci
<<" ";
}
return 0;
}
30. C++ program to Reverse a Number in C++ Program Code
#include<iostream>
using namespace std;
int main() {
int number, reverse = 0;
cout<<"Input a Number to Reverse and press Enter: ";
cin>> number; // Taking Input Number in variable number
for( ; number!= 0 ; )
{
reverse = reverse * 10;
reverse = reverse +
number%10;
number = number/10;
}
cout<<"New Reversed
Number is: "<<reverse;
return 0;
}
EmoticonEmoticon