-
Notifications
You must be signed in to change notification settings - Fork 0
basic mathematics
codingdud edited this page Sep 7, 2024
·
1 revision
- Armstrong Number Check
- Palindrome Check
- Count Number of Digits
- Fibonacci Sequence
- GCD Calculation Using Euclidean Algorithm
- Prime Number Check
- Finding Factors of a Number
- Reverse a Number
- Monotonic Numbers
Code:
#include<bits/stdc++.h>
using namespace std;
int main() {
int num, re = 0, og;
cin >> num;
og = num;
while(num > 0) {
re = re + pow(num % 10, 3);
num /= 10;
}
if(og == re) cout << "Armstrong number";
else cout << "Not an Armstrong number";
return 0;
}
Explanation:
- The code checks if a given number is an Armstrong number (also called a narcissistic number). For a 3-digit number, the sum of the cubes of its digits is compared to the original number.
- Example: 153 → (1^3 + 5^3 + 3^3 = 153).
- The loop calculates the sum of the cubes of the digits and checks if it equals the original number.
Code:
#include<iostream>
using namespace std;
int main() {
int num, rev = 0;
cin >> num;
int og = num;
while(num > 0) {
rev = rev * 10 + num % 10;
num /= 10;
}
if(og == rev) {
cout << "Palindrome";
} else {
cout << "Not Palindrome";
}
return 0;
}
Explanation:
- This code checks if a given number is a palindrome, which means it reads the same backward as forward.
- The number is reversed, and if the reversed number is equal to the original, it’s a palindrome.
Code:
#include<bits/stdc++.h>
using namespace std;
int count(int n) {
int cnt = (int)log10(n) + 1;
cout << cnt;
}
int main() {
int num;
cin >> num;
count(num);
return 0;
}
Explanation:
- The code counts the number of digits in a number using logarithmic functions and division.
- Using
log10
, it computes the number of digits in base 10 efficiently.
Code:
#include<iostream>
using namespace std;
int main() {
int n;
cin >> n;
int a = 0, b = 1, c = 0;
cout << a << " " << b << " ";
for(int i = 2; i < n; i++) {
c = a + b;
cout << c << " ";
a = b;
b = c;
}
}
Explanation:
- The code generates the Fibonacci sequence up to
n
terms. - Each number in the Fibonacci sequence is the sum of the two preceding ones.
Code:
#include<bits/stdc++.h>
using namespace std;
// Euclidean algorithm
int gcdm(int a, int b) {
if(a == 0) return b;
return gcdm(b % a, a);
}
int main() {
cout << gcdm(12, 4);
}
Explanation:
- This code computes the greatest common divisor (GCD) of two numbers using the Euclidean algorithm.
- The function
gcdm
uses recursion and modulo operations to calculate the GCD.
Code:
#include<bits/stdc++.h>
using namespace std;
int main() {
int num;
cin >> num;
int root = sqrt(num);
int counter = 0;
for(int i = 1; i < root; i++) {
if(num % i == 0) {
counter++;
if((num / i) != i) counter++;
}
}
if(counter > 2) cout << "Not prime";
else cout << "Prime number";
}
Explanation:
- The code checks whether a number is prime by counting the number of divisors.
- If there are more than two divisors, it’s not a prime number.
Code:
#include<bits/stdc++.h>
using namespace std;
void factorial(int num) {
vector<int> v;
int root = sqrt(num);
for(int i = 1; i < root; i++) {
if(num % i == 0) {
v.push_back(i);
v.push_back(num / i);
}
}
sort(v.begin(), v.end());
for(int i : v) {
cout << i << " ";
}
}
int main() {
int num;
cin >> num;
factorial(num);
}
Explanation:
- This code finds all factors of a given number.
- It iterates up to the square root of the number, checking for divisors, and stores them in a sorted list.
Code:
#include<iostream>
using namespace std;
int main() {
int num;
cin >> num;
int rev = 0;
while(num != 0) {
rev = rev * 10 + num % 10;
num /= 10;
}
cout << rev;
return 0;
}
Explanation:
- The code reverses a given number by extracting the last digit and appending it to the reversed number.
Code:
#include<bits/stdc++.h>
using namespace std;
int main() {
int temp, temp2, flag = 1;
for(int i = 0; i < 1000; i++) {
flag = 1;
temp = i;
while(temp != 0) {
temp2 = temp % 10;
temp = temp / 10;
if(temp != 0 && temp2 > temp % 10) {
flag = 0;
break;
}
}
if(flag == 1) {
cout << i << " ";
}
}
}
Explanation:
- This code prints all monotonic numbers from 0 to 999. A monotonic number has digits that either always increase or always stay the same when read from left to right.
- Twitter: @AlgoDocHub
- Facebook: AlgoDocHub
- Instagram: @AlgoDocHub
Contact Us: Have questions, suggestions, or feedback? Don't hesitate to reach out! You can contact the maintainers of AlgoDocHub by opening an issue or joining our Discord community.
Happy coding, and may your algorithms always run efficiently! *