Experiment 04
AIM: Programming using looping and unconditional statements
Task 1: Write a program to display the sum of N terms of even natural numbers.
Hint:-Suppose value of N=6, then first N terms are 2+4+6+8+10+12
#include <iostream> using namespace std; int main() { int n, i, x, s; cout << "Enter the value of n: "; cin >> n; if (n == 0) { cout << "The sum is 0"; } else if (n < 0) { cout << "Invalid Input "; } else { for (i = 0, s = 0, x = 2; i < n; i = i + 1, x = x + 2) { s = s + x; } cout << "The sum is:" << s; } return 0; }
Task 2: Write a program to find the sum of all numbers between M and N, where N>M.
#include <iostream> using namespace std; int main() { int n, m, s; cout << "Enter the values of N & M (N should be greater than M):" << endl; cin >> n >> m; if (n < m) { n = n + 1; for (s = 0; n < m; n = n + 1) { s = s + n; } cout << "The sum is " << s; } else { cout << "Invalid "; } return 0; }
Task 3: Write a program to find the power of a number XY; here, X is base and Y is exponent.
#include <iostream> using namespace std; int main() { int x, y, i, a; cout << "Enter the value of X and Y (X^Y)" << endl; cin >> x >> y; if (x == 0 && y == 0) { cout << "Answer is Infinity"; } else { for (i = 0, a = 1; i < y; i = i + 1) { a = a * x; } cout << "Answer is " << a; } return 0; }
Task 4: Write a program to count +ve number, -ve number and zeros until user want, make use of do while loop.
#include <iostream> using namespace std; int main() { int a, p = 0, n = 0, z = 0; char c; do { cout << "Enter the number: "; cin >> a; if (a == 0) { z = z + 1; } else if (a > 0) { p = p + 1; } else { n = n + 1; } cout << "Do you wish to continue? (Y for yes, N for No)" << endl; cin >> c; } while (c == 'y' || c == 'Y'); cout << "Positive: " << p << endl << "Negative: " << n << endl << "Zero: " << z; return 0; }
Task 5: Write a program to check whether a number is a strong number or not.
#include<iostream> using namespace std; int main() { int n, n2, f, i, j, k; cout << "Enter the number: "; cin >> n; n2 = n; for (i = 0; n > 0; i = i + 1) { n = n / 10; } n = n2; for (f = 1, k = 0, j = n % 10; i > 0;) { if (j == 0) { i = i - 1; n = n / 10; j = n % 10; k = k + 1; f = 1; } f = f * j; j = j - 1; if (j == 1) { i = i - 1; n = n / 10; j = n % 10; k = k + f; f = 1; } } cout << endl << "Sum of factorial of digits: " << k; if (k == n2) { cout << endl << n2 << " is a Strong Number" << endl; } else { cout << endl << n2 << " is not a Strong Number" << endl; } return 0; }
Task 6: Write a program to accept a number from the user. Find and print the sum of digits of the number.
#include <iostream> using namespace std; int main() { int n1, n2, n3, i, s; cout << "Enter the number: "; cin >> n1; n3 = n1; for (n2 = 0; n1 > 0; n2++) { n1 = n1 / 10; } for (i = 0, s = 0; i < n2; i++) { s = s + (n3 % 10); n3 = n3 / 10; } cout << endl << s; return 0; }
Task 7: Write a program to accept a number from user and display if the number is Armstrong number. (Armstrong number is the number in any given number base, which forms the total of the same number, when each of its digits is raised to the power of the number of digits in the number.)
#include <iostream> using namespace std; #include <math.h> int main() { int n1, n2, n3, n4, i, a; cout << "Enter the number: "; cin >> n1; n3 = n1, n4 = n1; for (n2 = 0; n1 > 0; n2 = n2 + 1) { n1 = n1 / 10; } for (i = 0, a = 0; i < n2; i = i + 1) { a = a + pow(n3 % 10, n2); n3 = n3 / 10; } if (a == n4) { cout << n4 << " is an Armstrong number"; } else { cout << n4 << " is not an Armstrong number"; } return 0; }
Task 8: Write a program to print the sum of the last and the first digit of a number the user gives.
#include <iostream> using namespace std; #include <math.h> int main() { int n1, n2, n3; cout << "Enter the number: "; cin >> n1; n3 = n1; for (n2 = 0; n1 > 0; n2 = n2 + 1) n1 = n1 / 10; if (n2 == 1) { cout << "Invalid input" << endl; } else { for (n1 = n3; n2 > 1; n2--) n1 = n1 / 10; cout << "The sum is: " << (n3 % 10) + (n1) << endl; } return 0; }
Task 9: Write a program to check whether the entered number is a palindrome.
#include <iostream> using namespace std; int main() { int no1, no2, rev; cout << "Enter the Number : "; cin >> no1; no2 = no1; for (rev = 0; no1 > 0;) { rev = rev * 10 + (no1 % 10); no1 = no1 / 10; } if (no2 == rev) { cout << "The number is a Palindrome"; } else { cout << "The number is not a Palindrome"; } return 0; }
Task 10: Implement a program to print all Leap Years from 1 to N using C++ program.
#include <iostream> using namespace std; int main() { int n, i; cout << "Enter the year: "; cin >> n; for (i = 1; i <= n; i++) { if (i % 4 == 0) { if (i % 100 == 0) { if (i % 400 == 0) { cout << i << endl; } } else cout << i << endl; } } return 0; }
Task 11: Write a program to check whether the entered number is prime or not. (make use of break)
#include <iostream> using namespace std; int main() { int i, n, j; cout << "Enter the number: "; cin >> n; if (n == 0 || n == 1) cout << n << " is a Special number"; else if (n == 2 || n == 3) cout << n << " is a Prime number"; else { for (i = 2, j = 0; i < n; i++) if (n % i == 0) { j = 1; break; } if (j == 1) cout << n << " is Not a prime number"; else cout << n << " is a Prime number"; } return 0; }
Task 12: Write a program to print the entire uppercase and lowercase letters using a loop (use continue).
#include <iostream> using namespace std; int main() { int i; for (i = 65; i <= 122; i++) { if (i > 90 && i < 97) continue; cout << " " << (char)i << endl; } return 0; }