Navigation X
ALERT
Click here to register with a few steps and explore all our cool stuff we have to offer!



   2661

Need help with easy Homework

by Cripple - 07 May, 2020 - 03:36 AM
This post is by a banned member (Cripple) - Unhide
Cripple  
Registered
180
Posts
78
Threads
5 Years of service
#1
#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
raza786  
Infinity
91
Posts
3
Threads
5 Years of service
#2
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
Cripple  
Registered
180
Posts
78
Threads
5 Years of service
#3
(This post was last modified: 08 May, 2020 - 02:28 AM by Cripple.)
(07 May, 2020 - 08:55 PM)raza786 Wrote: Show More
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);
}
}
}

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
raza786  
Infinity
91
Posts
3
Threads
5 Years of service
#4
(This post was last modified: 09 May, 2020 - 06:25 PM by raza786.)
(08 May, 2020 - 02:27 AM)Cripple Wrote: Show More
(07 May, 2020 - 08:55 PM)raza786 Wrote: Show More
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);
}
}
}

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 :)

Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
or
Sign in
Already have an account? Sign in here.


Forum Jump:


Users browsing this thread: 1 Guest(s)