Perfect Numbers C Program

This is a discussion on Perfect Numbers C Program within the C Programming forums, part of the General Programming Boards category; Hi, I was having a bit of trouble with a program that I was supposed to write for a homework ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    14

    Question Perfect Numbers C Program

    Hi, I was having a bit of trouble with a program that I was supposed to write for a homework assignment. The program's purpose is to find all of the perfect numbers between 1 and 100 (For those who dont know, a perfect number is a number is equal to the sum of all its divisors not including itself). The program runs, but it returns that there are no perfect integers. Here is the code I'm using:

    Code:
    /*
    *File:perfect.c
    *The program computes and displays all of the perfect numbers from 1 to 100.
    */
    
    #include "stdafx.h"
    #include <stdio.h>
    
    bool check_perfect(int num);
    
    main()
    {
    	int num;
    	bool flag;
    	
    	printf("The perfect numbers in the range 1-100:\n");
    	for (num=1;num<=100;num++);
    	{
    		flag=check_perfect(num);
    		if (flag==true) (printf("%d\n",num));
    	}
    }
    
    bool check_perfect(int num)
    {
    	int sum,num1;
    	bool check;
    	sum=0;
    	for (num1=1;num1<=(num-1);num1++)
    	{
    		check=(num%num1==0);
    		if (check==true) sum=sum+num1;
    	}
    	if (num==sum) return (true);
    	else return (false);
    }
    Thanks for any help.

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Moved to the C forum

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    10
    Quote Originally Posted by SlayerBlade

    Code:
    	printf("The perfect numbers in the range 1-100:\n");
    	for (num=1;num<=100;num++);  //<-heres the problem
    	{
    		flag=check_perfect(num);
    		if (flag==true) (printf("%d\n",num));
    	}
    }
    for loops don't typically have semicolons at the end. Remove it and the code will function as you intended.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program to convert numbers into words
    By Kingsley in forum C Programming
    Replies: 5
    Last Post: 07-01-2006, 07:50 PM
  2. Replies: 6
    Last Post: 05-18-2006, 05:35 PM
  3. Need HELP with Program
    By Jeff in forum C Programming
    Replies: 25
    Last Post: 09-23-2004, 08:03 PM
  4. Program that prints numbers in columns
    By rayrayj52 in forum C++ Programming
    Replies: 12
    Last Post: 09-20-2004, 02:43 PM
  5. Homework help
    By Jigsaw in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 04:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21