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

C++ Number and Character Operations

The document contains five C++ code snippets demonstrating basic programming concepts. These include counting digits in a number, finding the greatest common divisor (GCD), calculating the least common multiple (LCM), printing ASCII values of characters, and printing the English alphabet from A to Z. Each snippet is accompanied by its expected output.

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)
6 views2 pages

C++ Number and Character Operations

The document contains five C++ code snippets demonstrating basic programming concepts. These include counting digits in a number, finding the greatest common divisor (GCD), calculating the least common multiple (LCM), printing ASCII values of characters, and printing the English alphabet from A to Z. Each snippet is accompanied by its expected output.

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

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

You might also like