Factorial, Fibonacci, and Palindrome Programs in C++ and VB (with Examples)
Factorial, Fibonacci, and Palindrome Programs in C++ and VB
definitions for Factorial, Fibonacci, and Palindrome:
-
Factorial:
The factorial of a non-negative integern, denoted asn!, is the product of all positive integers from1ton.- Example:
5! = 5 × 4 × 3 × 2 × 1 = 1203! = 3 × 2 × 1 = 6
- Example:
-
Fibonacci Sequence:
The Fibonacci sequence is a series of numbers where each term is the sum of the two preceding terms. It starts with0and1- Example:
- Fibonacci sequence:
0, 1, 1, 2, 3, 5, 8, 13, ...
- Fibonacci sequence:
- Example:
-
Palindrome:
A palindrome is a number, word, or sequence that reads the same forward and backward.- Example:
121,1331,1221(Palindrome numbers)123,456(Not palindromes)
- Example:
1. Factorial Programs
Visual Basic (VB):
Private Sub Form_Load()
Dim n As Integer
Dim fact As Long
Dim i As Integer
n = InputBox("Enter a number here:")
fact = 1
For i = 1 To n
fact = fact * i
Next
MsgBox fact
End Sub
C++:
#include <iostream>
using namespace std;
int main() { int n, fact = 1; cout << "Enter a positive integer: "; cin >> n; for (int i = 1; i <= n; ++i) { fact *= i; } cout << "Factorial of " << n << " = " << fact << endl; return 0; }
2. Fibonacci Series Programs
Visual Basic (VB):
Private Sub Command1_Click()
Dim n As Integer, i As Integer
Dim fib1 As Long, fib2 As Long, fib As Long
Dim result As String
n = Val(InputBox("Enter the number of terms:", "Fibonacci Series"))
fib1 = 0
fib2 = 1
result = CStr(fib1)
For i = 1 To n - 1
fib = fib1 + fib2
fib1 = fib2
fib2 = fib
result = result & ", " & CStr(fib1)
Next
MsgBox "Fibonacci Series: " & result, vbInformation, "Result"
End Sub
C++:
#include <iostream>
using namespace std;
int main() {
int n, first = 0, second = 1, next;
cout << "Enter the number of terms: ";
cin >> n;
cout << "Fibonacci Series: ";
for (int i = 0; i < n; ++i) {
cout << first << " ";
next = first + second;
first = second; second = next; } return 0; }
3. Palindrome Check Programs
Visual Basic (VB):
Private Sub Command1_Click()
Dim num As String, reversedNum As String
Dim i As Integer
num = InputBox("Enter a number:", "Palindrome Check")
reversedNum = ""
For i = Len(num) To 1 Step -1
reversedNum = reversedNum & Mid(num, i, 1)
Next
If num = reversedNum Then
MsgBox num & " is a palindrome.", vbInformation, "Result"
Else
MsgBox num & " is not a palindrome.", vbExclamation, "Result"
End If
End Sub
C++:
#include <iostream>
using namespace std;
int main() {
int num, original, reversed = 0, remainder;
cout << "Enter an integer: ";
cin >> num;
original = num;
while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder; num /= 10; } if (original == reversed) cout << original << " is a palindrome." << endl; else cout << original << " is not a palindrome." << endl; return 0; }
Understanding C++ Programming Syntax
Important notice
⚠️ All Jobs and Opportunities published on fdo.net.rw are completely free to apply.
Never pay any fee during the recruitment process!
Even if fdo.net.rw does its best to avoid scam job or opportunity offers, job seekers or opportunity seekers are 100% responsible for applying at their own risk.
Check carefully before applying. If you doubt the legitimacy of any offer, do not apply and contact us at
freedigopp54@gmail.com
Never pay to get a job or opportunity - if you do, you do so at your own risk.
