This post is by a banned member (Cripple) - Unhide
OP 07 May, 2020 - 03:36 AM
Reply
#include <stdio.h>
#include <stdlib.h>
int statement(int);
int main()
{
int first;
int last;
int start;
int end;
int sum;
int statement;
first = 1;
end = 1000;
printf("Perfect numbers between 1 and 1000:\n", start, end);
for(first=start; first<=end; first++){
sum = 0;
for(last=1; last<first; last++){
if(first % last == 0){
sum += last;
}
}
}
}
int statement(int sum){
int first;
statement = false;
if (sum == first){
statement = true;
}
if(statement() == true){
printf("%d, ", &first);
}
}
thats my current homework and shit wont run and idfk what im doing wrong also output should look like this
Sample output:
6 is a perfect number because
6 = 1+2+3 (the sum of its factors)
basically
@@ is a perfect number because
@@ = @ + @ + @ + @ + @ ... + @ (the sum of its factors)
Havent incorperated that partt yet tho cuz idk how lol plz help thjanks
Config Shop No Longer Exist
This post is by a banned member (raza786) - Unhide
07 May, 2020 - 08:55 PM
Reply
Here you go bro,
Code:
#include <iostream>
using namespace std;
bool IsPerfectOrNot(int num) {
int i, div, sum = 0;
for (i = 1; i < num; i++)
{
div = num % i;
if (div == 0)
sum = sum + i;
}
if (sum == num)
return true;
else
return false;
}
void PerfectSum(int n)
{
int result = 0;
for (int i=1; i<=n; i++)
{
if (n%i == 0) {
if (i == 1) {
cout << i;
result += i;
}
else if (i == n) {
cout << " = " << result << endl;
}
else {
cout << " + " << i;
result += i;
}
}
}
}
void main()
{
int first;
int start;
int end;
first = 1;
end = 1000;
cout << "Perfect numbers between 1 and 1000:\n";
cin >> start >> end;
for (first = start; first <= end; first++) {
if (IsPerfectOrNot(first) == true) {
cout << first << " is a perfect number.\n";
PerfectSum(first);
}
}
}
This post is by a banned member (Cripple) - Unhide
OP 08 May, 2020 - 02:27 AM
(This post was last modified: 08 May, 2020 - 02:28 AM by Cripple.)
Reply
(07 May, 2020 - 08:55 PM)raza786 Wrote: Show MoreHere you go bro,
Code:
#include <iostream>
using namespace std;
bool IsPerfectOrNot(int num) {
int i, div, sum = 0;
for (i = 1; i < num; i++)
{
div = num % i;
if (div == 0)
sum = sum + i;
}
if (sum == num)
return true;
else
return false;
}
void PerfectSum(int n)
{
int result = 0;
for (int i=1; i<=n; i++)
{
if (n%i == 0) {
if (i == 1) {
cout << i;
result += i;
}
else if (i == n) {
cout << " = " << result << endl;
}
else {
cout << " + " << i;
result += i;
}
}
}
}
void main()
{
int first;
int start;
int end;
first = 1;
end = 1000;
cout << "Perfect numbers between 1 and 1000:\n";
cin >> start >> end;
for (first = start; first <= end; first++) {
if (IsPerfectOrNot(first) == true) {
cout << first << " is a perfect number.\n";
PerfectSum(first);
}
}
}
Hey bro thanks so much but we have learned about iostream? thats C++ i think im learning just C
Config Shop No Longer Exist
This post is by a banned member (raza786) - Unhide
09 May, 2020 - 06:24 PM
(This post was last modified: 09 May, 2020 - 06:25 PM by raza786.)
Reply
(08 May, 2020 - 02:27 AM)Cripple Wrote: Show More (07 May, 2020 - 08:55 PM)raza786 Wrote: Show MoreHere you go bro,
Code:
#include <iostream>
using namespace std;
bool IsPerfectOrNot(int num) {
int i, div, sum = 0;
for (i = 1; i < num; i++)
{
div = num % i;
if (div == 0)
sum = sum + i;
}
if (sum == num)
return true;
else
return false;
}
void PerfectSum(int n)
{
int result = 0;
for (int i=1; i<=n; i++)
{
if (n%i == 0) {
if (i == 1) {
cout << i;
result += i;
}
else if (i == n) {
cout << " = " << result << endl;
}
else {
cout << " + " << i;
result += i;
}
}
}
}
void main()
{
int first;
int start;
int end;
first = 1;
end = 1000;
cout << "Perfect numbers between 1 and 1000:\n";
cin >> start >> end;
for (first = start; first <= end; first++) {
if (IsPerfectOrNot(first) == true) {
cout << first << " is a perfect number.\n";
PerfectSum(first);
}
}
}
Hey bro thanks so much but we have learned about iostream? thats C++ i think im learning just C
you can change the part of C++ to C which is not much of a difference, only difference here in the code might be just the cout and cin which you can replace with printf and scanf... Leave a like if you liked the work :)
|