1) Hello World
#include <iostream>
using namespace std;
int main() {
cout << "Hello World";
return 0;
}
Output: Hello World
2) Sum of Two Numbers
#include <iostream>
using namespace std;
int main() {
int a = 5, b = 7;
cout << "Sum = " << (a + b);
return 0;
}
Output: Sum = 12
3) Swap Two Numbers
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 20, temp;
temp = a;
a = b;
b = temp;
cout << "a = " << a << ", b = " << b;
return 0;
}
Output: a = 20, b = 10
4) Largest of Two Numbers
#include <iostream>
using namespace std;
int main() {
int a = 15, b = 25;
if(a > b)
cout << "Largest = " << a;
else
cout << "Largest = " << b;
return 0;
}
Output: Largest = 25
5) Check Even or Odd
#include <iostream>
using namespace std;
int main() {
int n = 11;
if(n % 2 == 0)
cout << "Even";
else
cout << "Odd";
return 0;
}
Output: Odd
6) Factorial of a Number
#include <iostream>
using namespace std;
int main() {
int n = 5, fact = 1;
for(int i = 1; i <= n; i++)
fact *= i;
cout << "Factorial = " << fact;
return 0;
}
Output: Factorial = 120
7) Fibonacci Series
#include <iostream>
using namespace std;
int main() {
int n = 5, a = 0, b = 1, c;
cout << a << " " << b << " ";
for(int i = 2; i < n; i++) {
c = a + b;
cout << c << " ";
a = b; b = c;
}
return 0;
}
Output: 0 1 1 2 3
8) Reverse a Number
#include <iostream>
using namespace std;
int main() {
int n = 123, rev = 0, rem;
while(n != 0) {
rem = n % 10;
rev = rev * 10 + rem;
n /= 10;
}
cout << "Reversed = " << rev;
return 0;
}
Output: Reversed = 321
9) Palindrome Number
#include <iostream>
using namespace std;
int main() {
int n = 121, temp = n, rev = 0, rem;
while(temp != 0) {
rem = temp % 10;
rev = rev * 10 + rem;
temp /= 10;
}
if(n == rev)
cout << "Palindrome";
else
cout << "Not Palindrome";
return 0;
}
Output: Palindrome
10) Armstrong Number
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int n = 153, temp = n, sum = 0, rem;
while(temp != 0) {
rem = temp % 10;
sum += pow(rem, 3);
temp /= 10;
}
if(sum == n)
cout << "Armstrong";
else
cout << "Not Armstrong";
return 0;
}
Output: Armstrong