[go: up one dir, main page]

0% found this document useful (0 votes)
7 views2 pages

Simple Programs Set2

The document contains five C++ programs demonstrating basic algorithms: finding the largest of three numbers, calculating the factorial of a number, generating a Fibonacci series, reversing a number, and checking if a number is a palindrome. Each program includes code snippets and their respective outputs. The outputs confirm the correctness of the algorithms implemented.

Uploaded by

Abhilash Alshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Simple Programs Set2

The document contains five C++ programs demonstrating basic algorithms: finding the largest of three numbers, calculating the factorial of a number, generating a Fibonacci series, reversing a number, and checking if a number is a palindrome. Each program includes code snippets and their respective outputs. The outputs confirm the correctness of the algorithms implemented.

Uploaded by

Abhilash Alshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1) Largest of Three Numbers

#include <iostream>
using namespace std;
int main() {
int a = 10, b = 25, c = 15;
if(a >= b && a >= c)
cout << "Largest = " << a;
else if(b >= a && b >= c)
cout << "Largest = " << b;
else
cout << "Largest = " << c;
return 0;
}
Output: Largest = 25

2) 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

3) 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

4) Reverse a Number
#include <iostream>
using namespace std;
int main() {
int n = 1234, rev = 0;
while(n > 0) {
rev = rev*10 + n%10;
n /= 10;
}
cout << "Reverse = " << rev;
return 0;
}
Output: Reverse = 4321

5) Palindrome Check
#include <iostream>
using namespace std;
int main() {
int n = 121, temp = n, rev = 0;
while(temp > 0) {
rev = rev*10 + temp%10;
temp /= 10;
}
if(rev == n)
cout << "Palindrome";
else
cout << "Not Palindrome";
return 0;
}
Output: Palindrome

You might also like