/* Program for displaying all PRIME numbers and their quantity(amount) within SPECIFIED RANGE. */ #include<iostream> using namespace std; int main() { int i, j, num1, num2, count = 0; bool a = 0; cout << "Please enter only non-negative Numbers:\n"; cout << "Enter starting number?\t"; cin >> num1; cout << "Enter ending number?\t"; cin >> num2; while (num1 < 0 || num2 < 0) { cout << "Please enter only non-negative Numbers:\n"; cout << "Enter starting number?\t"; cin >> num1; cout << "Enter ending number?\t"; cin >> num2; } for (j = num1; j <= num2; j++) { if (j == 0 || j == 1) continue; for (i = 2; i < = j / 2; i++) { if (j % i == 0) { a = 1; break; } } if (a != 1 || j == 2) { cout << j << " is a Prime Number\n"; count++; } a = 0; } cout << "Total Prime numbers in Entered Range is " << count << endl; return 0; }
OUTPUT
/* Program for displaying all PRIME numbers, their sum and quantity(amount) within SPECIFIED RANGE. */ #include<iostream> using namespace std; int main() { int i, j, num1, num2, count = 0, sum = 0; bool a = 0; //flag variable cout << "Please enter only non-negative Numbers:\n"; cout << "Enter starting number?\t"; cin >> num1; cout << "Enter ending number?\t"; cin >> num2; while (num1 < 0 || num2 < 0) { cout << "Please enter only non-negative Numbers:\n"; cout << "Enter starting number?\t"; cin >> num1; cout << "Enter ending number?\t"; cin >> num2; } for (j = num1; j <= num2; j++) { if (j == 0 || j == 1) continue; for (i = 2; i < = j / 2; i++) { if (j % i == 0) { a = 1; break; } } if (a != 1 || j == 2) { cout << j << " is a Prime Number\n"; sum = sum + j; count++; } a = 0; } cout << "Total Prime numbers in Entered Range is \t" << count << endl; cout << "Sum of all Prime numbers in Entered Range is \t" << sum << endl; return 0; }
OUTPUT
/* Program for displaying First "n" PRIME numbers */ #include<iostream> #include<iomanip> using namespace std; int main() { int i, j = 2, n, count = 0; bool a = 0; cout << "How many Prime numbers do u want to display?\t"; cin >> n; cout << "First \"" << n << "\" Prime numbers are:\n"; while (count < n) { for (i = 2; i <= j / 2; i++) { if (j % i == 0) { a = 1; break; } } if (a != 1 || j == 2) { count++; cout << setw(5) << count << "(th) Prime Number = " << j << "\n"; } a = 0; j++; } return 0; }
OUTPUT
/* Program for displaying First "n" PRIME numbers and their "Sum". */ #include<iostream> #include<iomanip> using namespace std; int main() { int i, j = 2, n, count = 0, sum = 0; bool a = 0; cout << "How many Prime numbers do u want to display?\t"; cin >> n; cout << "First \"" << n << "\" Prime numbers are:\n"; while (count < n) { for (i = 2; i <= j / 2; i++) { if (j % i == 0) { a = 1; break; } } if (a != 1 || j == 2) { count++; if (count == 1) cout << setw(5) << count << "st Prime Number = " << j << "\n"; else if (count == 2) cout << setw(5) << count << "nd Prime Number = " << j << "\n"; else if (count == 3) cout << setw(5) << count << "rd Prime Number = " << j << "\n"; else cout << setw(5) << count << "th Prime Number = " << j << "\n"; sum = sum + j; } a = 0; j++; } cout << "Sum of First \"" << n << "\" Prime numbers is \t" << sum << endl; return 0; }
OUTPUT
No comments:
Post a Comment