[go: up one dir, main page]

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

Simple Programs Set3

The document contains five C++ code snippets demonstrating basic programming concepts. The snippets include calculating the sum of digits, checking for Armstrong numbers, determining if a number is prime, generating a multiplication table, and implementing a simple calculator using switch-case. Each code example 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)
10 views2 pages

Simple Programs Set3

The document contains five C++ code snippets demonstrating basic programming concepts. The snippets include calculating the sum of digits, checking for Armstrong numbers, determining if a number is prime, generating a multiplication table, and implementing a simple calculator using switch-case. Each code example 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
You are on page 1/ 2

1) Sum of Digits

#include <iostream>
using namespace std;
int main() {
int n = 1234, sum = 0;
while(n > 0) {
sum += n % 10;
n /= 10;
}
cout << "Sum = " << sum;
return 0;
}
Output: Sum = 10

2) Armstrong Number Check


#include <iostream>
#include <cmath>
using namespace std;
int main() {
int n = 153, temp = n, sum = 0, d;
while(temp > 0) {
d = temp % 10;
sum += pow(d, 3);
temp /= 10;
}
if(sum == n)
cout << "Armstrong Number";
else
cout << "Not Armstrong";
return 0;
}
Output: Armstrong Number

3) Prime Number Check


#include <iostream>
using namespace std;
int main() {
int n = 7, flag = 1;
if(n <= 1) flag = 0;
for(int i = 2; i <= n/2; i++) {
if(n % i == 0) {
flag = 0;
break;
}
}
if(flag) cout << "Prime";
else cout << "Not Prime";
return 0;
}
Output: Prime

4) Multiplication Table
#include <iostream>
using namespace std;
int main() {
int n = 5;
for(int i = 1; i <= 10; i++)
cout << n << " x " << i << " = " << n*i << "\n";
return 0;
}
Output: 5 x 1 = 5 5 x 2 = 10 ... 5 x 10 = 50

5) Simple Calculator (Switch Case)


#include <iostream>
using namespace std;
int main() {
int a = 10, b = 5;
char op = '+';
switch(op) {
case '+': cout << "Sum = " << a+b; break;
case '-': cout << "Difference = " << a-b; break;
case '*': cout << "Product = " << a*b; break;
case '/': cout << "Quotient = " << a/b; break;
default: cout << "Invalid Operator";
}
return 0;
}
Output: Sum = 15

You might also like