ViijayScript: #Program to #check #perfect #number in #C #C++ #Java C# #Python

Wednesday 15 June 2016

#Program to #check #perfect #number in #C #C++ #Java C# #Python

Hello netizens, On this post I am going to tell #Programs to #check #perfect #number in #C #C++ #Java C# #Python.

Program in C:

#include <stdio.h>
int main(){
int n,i=1,sum=0;
printf(“Enter a number: “);
scanf(“%d”,&n);
while(i < n){
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("%d is a perfect number",i);
else
printf("%d is not a perfect number",i);
return 0;
}

Program in C++:

#include<iostream>
#include<iomanip>
using namespace std;
int main(){
int n,i=1,sum=0;
cout<> n;
while (i<n){
if (n%i==0)
sum=sum+i;
i++;
}
if (sum==n)
cout << i << " is a perfect number" ;
else
cout << i << " is not a perfect number" ;
system( "pause" );
return 0;
}

Program in Java:

import java.io.*;
import java.util.*;
class perfect
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print(“\nEnter a number:”);
int n = Integer.parseInt(br.readLine());
int sum = 0;
for(int i=1;i<n;i++)
{
if(n%i==0)
{
sum = sum + i;
}
}
if(sum == n)
System.out.println("\nGivener is a perfect number");
else
System.out.println("\nGivener is not perfect number");
}
}

Program in C#:

using System;
namespace example
{
class prime
{
public static void Main()
{
Console.Write(“Enter a Number: “);
int num;
num=Convert.ToInt32(Console.ReadLine());
int k;
k=0;
for(int i=1;i<=num;i++)
{
if(num%i==0)
{
k++;
}
}
if(k==2)
{
Console.WriteLine("Prime Number");
}
else
{
Console.WriteLine("Not a Prime Number");
}
}
}
}

Program in Python:

n = int(input(“enter a number to check perfect number: “))
sum = 0
for divisor in range(1, n):
if not n % divisor:
sum += divisor
if sum == n:
print(n, “is a perfect number”)

If our post is helpful then share it with your near and dear ones.

No comments:

Post a Comment

Prove that you are Alive by Writing Comments

Scan this QR Code on LinkedIn

Scan this QR Code on LinkedIn