C program to check whether given number is perfect or not - My CS Tutorial - My CS Tutorial

Breaking

Programming languages full tutorial and programs, exam papers, visual basic( Vb.net ), information of new technologies and more....

Tuesday, December 3, 2019

C program to check whether given number is perfect or not - My CS Tutorial

In this tutorial we will learn how to write a program that check given number is perfect or not.


C program code


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
  int num,a,sum=0;
  printf("\n Enter a number to know whether it is perfect or not:\n ");
  scanf("%d",&num);
  for(a=1;a<num;a++)
  {
    if(num%a==0)
    {
      sum+=a;
    }
  }
  if(sum==num)
  {
    printf("\n The given number %d is perfect.\n ",num);
  }
  else
  {
    printf("\n The given number %d is not perfect because the sum of its divisors are %d\n",num,sum);
  }
  getch();
}


OUTPUT:




In this output the value of input number is 28

Given number 28 is a perfect number because its divisors sum is equal to 28


Please share this post and blog link with your friends.For more programs use this blog.
If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.

Created by-- HARSH CHAUHAN

No comments:

Post a Comment