1) Count Digits in a Number
#include <iostream>
using namespace std;
int main() {
int n = 12345, count = 0;
while(n > 0) {
count++;
n /= 10;
}
cout << "Digits = " << count;
return 0;
}
Output: Digits = 5
2) Greatest Common Divisor (GCD)
#include <iostream>
using namespace std;
int main() {
int a = 12, b = 18;
while(a != b) {
if(a > b) a -= b;
else b -= a;
}
cout << "GCD = " << a;
return 0;
}
Output: GCD = 6
3) Least Common Multiple (LCM)
#include <iostream>
using namespace std;
int main() {
int a = 12, b = 18, lcm;
int maxVal = (a > b) ? a : b;
while(true) {
if(maxVal % a == 0 && maxVal % b == 0) {
lcm = maxVal;
break;
}
maxVal++;
}
cout << "LCM = " << lcm;
return 0;
}
Output: LCM = 36
4) Print ASCII Values
#include <iostream>
using namespace std;
int main() {
char c = 'A';
cout << "ASCII of " << c << " = " << int(c);
return 0;
}
Output: ASCII of A = 65
5) Print Alphabets (A–Z)
#include <iostream>
using namespace std;
int main() {
for(char c = 'A'; c <= 'Z'; c++)
cout << c << " ";
return 0;
}
Output: A B C D ... Z