C Program to Check Perfect Number

C program to check perfect number; Through this tutorial, we will learn how to check perfect number in c program using for loop, while loop and function.

C Program to Check Perfect Number

  • C Program to find Perfect Number using For Loop
  • C Program to find Perfect Number using While Loop
  • C Program to find Perfect Number using Function

C Program to find Perfect Number using For Loop

/* C Program to find Perfect Number using For Loop */

# include <stdio.h>

int main()
{
 int i, Number, Sum = 0 ;

 printf("\n Please Enter any number :- ") ;
 scanf("%d", &Number) ;

 for(i = 1 ; i < Number ; i++)
  {
   if(Number % i == 0)
     Sum = Sum + i ;
  }

 if (Sum == Number)
    printf("\n %d is a Perfect Number", Number) ;
 else
    printf("\n%d is not the Perfect Number", Number) ;

return 0 ;
}

The output of the above c program; as follows:

Please Enter any number :- 15
15 is not the Perfect Number

C Program to find Perfect Number using While Loop

/* C Program to find Perfect Number using While Loop */

# include <stdio.h>

int main()
{
  int i = 1, Number, Sum = 0 ;

  printf("\n Please Enter any number :- ") ;
  scanf("%d", &Number) ;

  while( i < Number )
   {
     if(Number % i == 0)
        Sum = Sum + i ;
     i++;
   }

  if (Sum == Number)
    printf("\n %d is a Perfect Number", Number) ;
  else
    printf("\n%d is not the Perfect Number", Number) ;

 return 0 ;
}

The output of the above c program; as follows:

Please Enter any number :- 6
6 is a Perfect Number

C Program to find Perfect Number using Function

/*C program to check nunber is perfect or not.*/

#include <stdio.h>

/*function to check perfect number or not*/
int isPerfect(int num)
{
    int loop, sum = 0;

    for (loop = 1; loop < num; loop++) {
        if (num % loop == 0)
            sum += loop;
    }

    if (sum == num)
        return 1; /*Perfect Number*/
    else
        return 0; /*Not Perfect Number*/
}

int main()
{
    int num, loop;
    int sum;

    printf("Enter an integer number: ");
    scanf("%d", &num);

    if (isPerfect(num))
        printf("%d is a perfect number.", num);
    else
        printf("%d is not a perfect number.", num);

    return 0;
}

The output of the above c program; as follows:

Enter an integer number: 9
9 is not a perfect number.

Recommended C Programs

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *