My C++ code won't loop like it should. What am I doing wrong?
#include <iostream>
using namespace std;
int main()
{
int n;
int flag= 1;
cout << "Prime/Not Prime" << endl;
cout << "Enter the Number to check Prime:"
<< endl;
cin >> n;
int m = n / 2;
int i;
for (i = 2; i <= m; i++)
if (n % i == 0)
flag = 0;
if (flag == 0)
cout << "Number is not Prime
" << endl;
else
cout << "Number is Prime"
<< endl;
}
Screenshot
--------------------------------------------------------------------
Program
//Header file
#include <iostream>
using namespace std;
int main()
{
//Variable for input read
int n;
//Variable to check prime or not
int flag = 1;
//Header
cout << "Prime/Not Prime" << endl;
//Prompt for input
cout << "Enter the Number to check
Prime:";
cin >> n;
//Get half of the number
int m = n / 2;
//Loop to find prime or not
//If it divisible by any number , then not
prime
for (int i = 2; i <= m; i++) {
if (n % i == 0) {
flag = 0;
}
}
//Check and display
if (flag == 0)
cout << "Number is not Prime
" << endl;
else
cout << "Number is Prime"
<< endl;
}
-------------------------------------
Output
Prime/Not Prime
Enter the Number to check Prime:121
Number is not Prime
-------------------------------------------------------
Note
This program is working perfectly, what actually you expect,.
Is it repeated to enter input?
I assume you want that way then check the below code.
Program
//Header file
#include <iostream>
using namespace std;
int main()
{
//Variable for input read
int n;
while (true) {
//Variable to check prime or
not
int flag = 1;
//Header
cout << "Prime/Not Prime"
<< endl;
//Prompt for input
cout << "Enter the Number to
check Prime:";
cin >> n;
//Get half of the number
int m = n / 2;
//Loop to find prime or not
//If it divisible by any number ,
then not prime
for (int i = 2; i <= m; i++)
{
if (n % i == 0)
{
flag = 0;
}
}
//Check and
display
if (flag == 0)
cout <<
"Number is not Prime " << endl;
else
cout <<
"Number is Prime" << endl;
//Repeatation part
char ch;
cout << "Do you want to enter
next number(y/n)? ";
cin >> ch;
ch = toupper(ch);
//Error check
while (ch != 'Y' and ch != 'N')
{
cout <<
"ERROR!!!Choice should be y/n." << endl;
cout <<
"Do you want to enter next number(y/n)? ";
cin >>
ch;
ch =
toupper(ch);
}
//exit from program
if (ch == 'N'){
exit(0);
}
}
}
My C++ code won't loop like it should. What am I doing wrong? #include <iostream> using...
CAN YU HELP ME CONSTRUCT A FLOW CHART FOR THE FOLLOW PROGRAM ? C++ CODE: #include<iostream> using namespace std; int main() { //declaring variable num int num; //prompting user to enter a number cout<<"Input a number to check prime or not:"; //reading input from user cin>>num; //loop to check user input for positive number while(num<0) { cout<<"Error! Positive Integers Only.\n"; cout<<"Input a number to check prime or not:"; cin>>num; } //initializing isPrime variable with false bool isPrime = true; //loop...
include<iostream> #include<cstring> using namespace std; char reg[30]; int main() { //char reg[30]; char DNA[30]; int flag = 0; int n, ni, i, j, k; cout << "Enter a regular expression" << ; cin >> reg; cout << endl; n = reg.size(); for (int i = 0; i < n; i++) { strcpy(DNA, reg.substr(i, j)); ni = DNA.lenghth(); for (k = 0; k < ni;...
I need a really good Pseudo/Algorithm code for this C++ program below. #include <iostream> #include<fstream> using namespace std; int main() { int low, high, i;//integer varaible bool flag;//boolean flag string a = "Enter two numbers(intervals): ";//string datatype ofstream f;//fow writing to file f.open("a.txt"); cout << a; cin >> low >> high; cout << "Prime numbers between " << low << " and " << high << " are: "; do /*...
#include <iostream> using namespace std; int main() { int i,n; int counter=0; cin >> n; for (i = 1; i <= n; i++) { if (n % i == 0 ) { counter++; } /*else { counter++; i++; }*/ } if (counter == 2) cout << n <<...
How do can I update this code (Code A): Code (A) #include using namespace std; int fibonacci(int n) { int a = 0, b = 1, c; if (n <= 1) return n; for (int i = 2; i <= n; i++) { c = a + b; a = b; b = c; } return b; } int fibonacciRecursive(int n) { if (n <= 1) { return n; } return fibonacciRecursive(n-1) + fibonacciRecursive(n-2); } int main() { int n;...
what is the output for the following code? explain the steps. /*#include <iostream> using namespace std; int f(int &i) { i = 10; return(5 * i); } int main() { int n = 5; f(n); cout << n << "\n"; return 0; } #include <iostream> using namespace std; int sub1(int n) { n--; return n; } int main() { int m = 10; for(int j = 0; j < 10; j++) m -= sub1(j); cout << m << "\n"; return...
#include<iostream> using namespace std; int main() { float num,avg,sum=0.0; int count=0; bool moreNumbers=true; cout<<"Enter grades:"<<endl; while(moreNumbers) { cin>>num; if(num < 0) { moreNumbers=false; } else { count = count+1; sum=sum+num; } } avg=sum/count; cout<<"Average grade:"<<avg<<endl; cout<<"How many grades were entered:"<<count<<endl; return 0; } Question: We need to add another loop to check input. Just like the input loop we already have, this one should use a boolean variable as a control. So, the logic could be something like this: Loop...
Fix this code so only the function prototype comes before main. #include <iostream> using namespace std; bool isMultiple(int num1, int num2) { return num1 % num2 == 0; } int main() { char ch = 'Y'; int num1, num2; while(ch =='Y') // While ch is equal to Y { cout << "Enter two numbers(largest first): "; cin >> num1; // Getting 1st number cin >> num2; // Getting 2nd number if(isMultiple(num1, num2)) cout << num2 << " " << "IS...
Please help! Studying for my c++ test! What does the following code produce? include <iostream> using namespace std; void my_function(int n); void main() { my_function(546); } void my_function(int n) { if (n < 10) cout << n << endl; else { my_function(n/10); cout << (n%10) << endl; } } What is the output of the following code? int j=32, k=5, r; r = j ^ k; cout << r...
im not sure why my code isnt working? include <iostream> #include <iomanip> using namespace std; int main() { int amountOfCoffee; double Price; char salesTaxChargeability; double TotalAmount; const double SALESTAX = 0.035; // 3.5 % cout << "\nEnter the number of pounds of coffee ordered in Pounds :"; cin >> amountOfCoffee; cout << "\nEnter the price of coffee per Pound :"; cin >> Price; cout << "\nIs sales tax Chargeable (y or n): "; cin >> salesTaxChargeability; if ( (salesTaxChargeability ==...