Wednesday, February 6, 2019

Given Number is Perfect Or Not - using "C-Language" in Ubuntu.

//This code works in unix environment using cc, or gcc commands. 
//It works on windows with "Turbo C", with small alterations in code.
Perfect Number :
A perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself. 
File Name : perfect.c
#include <stdio.h>
void main()
{
  int n,i,s=0;
  printf("Enter a Number :");
  scanf("%d",&n);              
  for(i=1;i<n;i++)
  {
    if(n%i==0)                 
    s=s+i;                      
  }
  if(s==n)
       printf("Given no is a Perfect Number");
  else
       printf("Given no is not a Perfect Number");
  }

Command To Compile in Ubuntu Terminal:

cc perfect.c

Note : The above command generates a.out(executable) file which can be executed as follows.


Command To Execute a.out file:

./a.out

Output1:

Enter a Number :6
Given no is a Perfect Number
Output2:
Enter a Number :28
Given no is a Perfect Number
Output3:
Enter a Number :53
Given no is not a Perfect Number

No comments:

Post a Comment

Hadoop Commands

HADOOP COMMANDS OS : Ubuntu Environment Author : Bottu Gurunadha Rao Created: 31-Jan-2022 Updated: 31-Jan-2022 Release  : 1.0.1 Purpose: To ...

Search This Blog