Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Hi I'm doing a prime number work for class, and I'm running into a problem. Could someone please help me a little bit?
With some numbers it will work but other numbers it won't:
#include<iostream>
#include "cmath"
#include "ctime"
using namespace std;
int main(){
int num;
int i = 2;
int result;
cout << "What is the prime you want to enter";
cin >> num;
for(; i < num; i++){
result = num / i;
}
if (num % i == 0) {
cout << "Your number is not prime\nIt is divisble by: " << i << endl;
}
if(num % i != 0){
cout << "your number is prime\n";
}
return 0;
}
this if (num % i == 0) { .... must be done inside the loop, otherwise you are going to check only the last value of i
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 days ago.
Improve this question
#include <iostream>
using namespace::std;
int iseven(int a)
{
if (a / 2 == 0) {
return 1;
}
return 0;
}
int main()
{
int num;
cin >> num;
if (iseven(num)) {
cout << "number even";
}
else {
cout << "odd h number";
}
return 0;
}
I am expecting even and odd of the number, but I am getting odd as a only output. help me
You need to compute a modulo b to find the remainder of a / b. Modulo operator is %.
Also, replace return type of the function with bool.
#include <iostream>
using namespace std;
bool iseven(int a)
{
return a % 2 == 0;
}
int main()
{
int num;
cin >> num;
if (iseven(num)) {
cout << "even";
}
else {
cout << "odd";
}
return 0;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
/*when you will enter any number between 0-100 .
There are four ranges and the program will show in which range your number lies*/
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Enter your number between 0-100\n";
cin >> number;
if(number <= 25 || number == 0)
{
cout << "your number is between 0-25\n";
}
else if (number > 25 || number <= 50)
{
cout << "your number is between 25-50\n";
}
else if (number > 50 || number <= 75)
{
cout << "your number is between 50-75\n";
}
else if(number >75 || number <= 100)
{
cout << "your number is between 75-100\n";
}
else
{
cout<<"number is not between 0-100\n";
}
return 0;
}
The second if will always fire if the first if did not. Reason is it should not be OR, ||, but and AND, &&, same with the remaining tests.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I am new in programming and trying to solve some questions and in this question, I am trying to find prime numbers up to n digits using only 1 loop statement.
One loop, and recursion:
#include <iostream>
#include <cmath>
#define NUDIGS 3 //number of digits
bool TestPrime(int n, int ndiv)
{
if ( n == ndiv )
return true;
if ( (n % ndiv) == 0 )
return false;
return TestPrime (n, ++ndiv) ;
}
int main()
{
//calculate the max number with BUDIGITS
int max = pow(10, NUDIGS) - 1;
std::cout << "testing numbers from 2 to " << max << std::endl;
int ntot = 0;
//Loop testing every number. '1' is not considered as a prime number.
for (int i=2; i <= max; i++)
{
//call a recursive test
if ( TestPrime(i, 2) )
{
std::cout << i << " is prime " << std::endl;
ntot++;
}
}
std::cout << ntot << " prime numbers found" << std::endl;
}
//this program will print prime number from 2 to N . and break loop
without using break statment
#include <iostream>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int range;
cin>>range;
int num=2;
for(int i=2;i<=num;i++){
if(num>range){
i=num+1;
}
else{
if(i==num){
cout<<num<<" ";
i=1;
num=num+1;
}
else if(num%i==0){
i=1;
num=num+1;
}
}
}
cout<<endl;
return 0;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have to write a program in which integer value is entered from user and the string has to be displayed that many times. But I am getting errors.
#include<iostream>
#include<string>
using namespace std;
int main()
{
int N;
cout << "Enter N: ";
cin >> N;
cout << string(N, "Well Done");
return 0;
}
Note: I am not permitted to use a loop in this assignment.
If you may not use a loop, you may use goto to get around the restriction:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int N;
cout << "Enter N: ";
cin >> N;
{
int i = 0;
goto test;
begin:
cout << "Well Done";
++i;
test:
if (i < N)
goto begin;
}
return 0;
}
Note that goto is widely considered bad practice.
EDIT2: IN THE ORIGINAL ASKER's COMMENTS, LOOPS OF ANY KIND ARE PROHIBITED IN THIS ASSIGNMENT.
Use recursion.
void printN(int n, string s) {
if (n <= 0) return;
cout << s << endl;
printN(n-1, s);
}
Then you can call this from your main program as follows:
printN(userInput, "Hi my name is ricky bobby");
EDIT: just saw you haven't learned recursion yet. Look up this term, and familiarize yourself with it. This is a way to do iteration without looping (this is the most simplistic way I can describe it)
std::string does not have a constructor that repeats a string N times (it does have one for repeating a single character N times, though). What you need is a loop instead, eg:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int N;
cout << "Enter N: ";
cin >> N;
for (int i = 0; i < N; ++i)
cout << "Well Done";
return 0;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am trying to write an algorithm which can show me the number I enter in the mirror. I mean, if I enter 173, it should show me that the number in the mirror is 371. I wrote the code, but something it's not working and I can't figure what. Thanks a lot !
#include <iostream>
using namespace std;
void Citire(int &n)
{
cout << "\nda numarul:";
cin >> n;
while (n <= 0)
{
cout << "ai gresit, da natural:";
cin >> n;
}
}
int Oglinda(int &n)
{
int Og = 0;
int UltCif;
while (n > 0)
{
UltCif = n % 10;
Og = Og * 10 + UltCif;
n = n / 10;
}
return Og;
}
int main()
{
int n;
int Og;
Citire(n);
Oglinda(Og);
cout << endl << Og << " este oglinda numarului " << n << endl;
return 0;
}
I think you mean:
Og = Oglinda(n);
And that function's signature should be:
int Oglinda(int n);
Otherwise, you're modifying n in the function - which you don't want to do since you want to print its original value at the end.