program which check perfect numbers - something gets wrong [closed] - c++

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 have to write a function which gets a number, and prints all the perfect numbers that are smaller than the number given. If there are not any, the function prints an appropriate message.
The program I have just written is compiled well, but it's working only partially. If the input number is 5, for instance, for some reason I don't get any message that there are not perfect numbers in this range (=until 5) .
Can someone please explain to me what's wrong in this program?
I would appreciate any help!
#include<iostream>
using namespace std;
void check (int num, int & j);
void main()
{
int num,j, count;
cout << "List all the perfect numbers less than: ";
cin >> num;
check (num, j);
}
void check (int num, int & j)
{
int i,sum, count=0;
for(j=2;j<num;num++)
{
sum=0;
for(i=1;i<j;i++)
{
if(j%i == 0)
sum += i;
}
if(sum == j)
{cout << j <<endl;
count+=1;
}
}
if (count==0)
cout<<"there are no perfect numbers"<<endl;
}

There is an infinite loop in your code:
for(j=2;j<num;num++)
I think it should be
for(j=2;j<num;j++)

Related

my bool function not working,dont know why [closed]

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 last month.
Improve this question
My intention was to count the sum of prime numbers in a certain range. Its working fine when the range is less than or equal to 8. When I take input 9 it shows output 26 while the answer is 17. My program identifying 9 as a prime and adding with the sum.
{
for(int i=3; i*i<=n; i+=2)
{
if(n%i==0) return false;
}
return true;
}`
`int main(){
int t,sum=0;
cin >> t;
for(int k = 0; k < t; k++){
int n;
cin >> n;
if(n>=2) sum=2;`
` for(int i=3;i<=n;i+=2)
{
ifPrime(i);
if(ifPrime) sum=sum+i;
}`
` cout<<sum<<endl;
sum=0;
}
return 0;
}
my guess
this
ifPrime(i);
if(ifPrime) sum=sum+i;
should be
if(ifPrime(i)) sum=sum+i;
but without seeing all the code its impossible to say

this is showing wrong output when i am giving this more then one test cases [closed]

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 2 years ago.
Improve this question
this is codechef January challenge's first problemcodechef.
When i am giving more than one test case ,it is giving wrong output. and when i am giving one test cases it is going right.
my code:-
#include<iostream>
using namespace std;
int long long N;
int main()
{
int i,j, N,t,d,k,sum=0;
cin>>t;
for(j=1;j<=t;j++)
{
cin>>N>>k>>d;
for(i=0;i<N;i++)
{
int num;
cin >> num;
sum += num;
}
for(i=0;i<N;i++)
{
sum= int(sum/k);
if(sum>d)
{
cout<<d<<endl;
}
else
{
cout<<sum<<endl;
}
}
}
return 0;
}
Your output is in the wrong place, it should be inside the test case loop not after it. You also need to print a newline after every result
Your code is like this
for(j=1;j<=t;j++)
{
...
}
if(sum>d)
{
cout<<d;
}
else
cout<<sum;
but it should be like this
for(j=1;j<=t;j++)
{
...
if(sum>d)
cout<<d<<'\n';
else
cout<<sum<<'\n';
}
Mistakes like this are much easier to spot if you get into the habit of indenting your code consistently.
Another error is that you don't reset sum to zero for each test case. Your loop should also look like this
for(j=1;j<=t;j++)
{
sum=0;
...
}
Some sloppy mistakes. To be a programmer you really have to pay attention to the details.
For free, here's another tip. You don't need to use an array for this problem. To add up numbers you don't need to put them all in an array first and then add them up later. You can just add them up as you input them.
int sum = 0;
for (int i = 0; i < N; i++)
{
int num;
cin >> num;
sum += num;
}

My code seems to have a syntax error in finding the maximum number [closed]

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 5 years ago.
Improve this question
What is wrong with my code?
It has to find the maximum between n numbers.
#include<iostream>
using namespace std;
int main()
{
int n,i = 0;
cin >> n;
int a[n];
while(i < n)
{
cin >> a[i];
i++;
}
i=1;
while(i <= (n + 1))
{
if (a[i] > a[0])
{
a[0] = a[i];
}
i++;
}
cout << a[0];
return 0;
}
This is meant to be a learning thing for you so I don't want to do your code for you. That being said I would look at while(i <= (n + 1)). You are stepping outside of the bounds of your array.

Hint: Missed newline ? while trying to solve the below quiz [closed]

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 8 years ago.
Improve this question
Problem
This is similar to the previous problem, however there will be multiple integers in the input. You have to write a computer program to read each integer and print Even if the integer is divisible by 2, else print Odd. To help further, the number of integers (T) to read will be the first input to the computer program.
Input Format:
First line of input contains count of integers: T. T>=1
After that, each line contains the integer N.
Sample Input:
2
4
5
Sample Output:
Even
Odd
Note: There should be a newline after each output. Otherwise you might end up printing EvenOdd here, which will result in wrong answer.
#include <iostream>
using namespace std;
int main()
{
int a[3];
cin>>a[0];
cin>>a[1];
cin>>a[2];
for(int i =0; i <sizeof(a)/sizeof(int); i++)
{
if (a[i]%2 == 0) cout<<"Even"<<endl;
else cout<<"Odd"<<endl;
}
return 0;
}
You don't need neither array nor vectors for this:
int main() {
int n;
cin >> n;
while(n--) {
int number;
cin >> number;
// do something with number
}
return 0;
}
Note that this structure is the most common to solve this kind of problems
your problem will be solved if you change
for(int i =0; i <sizeof(a)/sizeof(int); i++)
to
for(int i =1; i <sizeof(a)/sizeof(int); i++)
But, how'd you know the number of elemnts for the array beforehand?
To correct this, you don't declare the array statically. Take the first input, and allocate memory for that many ints.
#include <iostream>
using namespace std;
int main()
{
int a[3];
cin>>a[0];
cin>>a[1];
cin>>a[2];
for(int i =0; i <sizeof(a)/sizeof(int); i++)
{
if (a[i]%2 == 0)
cout<<"Even"<<endl;
else
cout<<"Odd"<<endl;
}
return 0;
}
this code which u posted is working perfectly bro :)

Program returning garbage after array formed in function [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
This program is supposed to use a function to gather 4 grades into an array, use another function to average the grades, and another function to display the average and 4 original grades.
The 4 grades have to be between 0 and 100.
I have this program printing out grade[2] just to test it, but it always outputs garbage.
What is it that I'm doing wrong here?
Also, if I try to call getGrades(grade, 5) without the "float grade[5]" line in there first, it tells me that "grade" is an undeclared identifier. Which leads me to believe i've done something wrong in writing that function.
Thanks
#include <iostream>
using namespace std;
float computeAverage(float exam1, float exam2, float exam3, float exam4)
{
float average;
average = exam1*.2 + exam2*.2 + exam3*.2 + exam4*.4;
return average;
}
void getGrades(float a[], int size)
{
int i;
float num;
for (i=0; i<4; i++)
{
cin >> num;
if (num>=0 && num <=100)
num = a[i];
else
{
cout << "That number is out of range.";
i--;
}
}
}
int main()
{
float grade[5];
getGrades(grade,5);
grade[4] = computeAverage(grade[0], grade[1], grade[2], grade[3]);
cout << grade[2];
return 0;
}
You are not storing the values in grade array in getGrades function that is the reason you are getting garbage value.
Change this:-
num = a[i];
to
a[i] = num;
num = a[i];
should be
a[i] = num;
Welcome to computer science :-)