ViijayScript: 06/15/16

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.

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

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

Program in C:

#include <stdio.h>
int check_prime(int);
main()
{
int i, n, result;
printf(“Enter the number of prime numbers required\n”);
scanf(“%d”,&n);
printf(“First %d prime numbers are :\n”, n);
for(i=0; i < n; i++){
result = check_prime(i);
/ if i is prime then it will return 1 /
if ( result == 1 )
printf("%d \n", i);
}
return 0;
}
int check_prime(int a)
{
int c;
/ starting from 2, if no is completely divisible by any no then it is not prime /
for ( c = 2 ; c {
if ( a%c == 0 )
return 0;
}
if ( c == a )
return 1;
}

Program in C++:

#include<iostream.h>
#include<conio.h>
void main ()
{
int num, count = 0 ;
clrscr () ;
cout <> num ;
for (int i = 2 ; i < num ; i ++ )
{
if (num % i == 0 )
{
count ++ ;
break ;
}
}
if (count == 0 )
cout << "Prime number" ;
else
cout << "Not a Prime number" ; getch () ;
}

Program in Java:

import java.util.*;
public class PrimeNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println(“Please enter a number: “);
int num = scanner.nextInt(); PrimeNumber primeNum = new PrimeNumber();
if ( primeNum.isPrime(num) ) {
System.out.printf(“\n Result: The number %d is Prime”, num);
} else {
System.out.printf(“\n Result: The number %d is not Prime”, num);
}
} // Method to check whether the number is prime or not
public boolean isPrime(int num) {
if ( num < 2 ) return false;
for (int i = 2; i <= Math.sqrt(num); i++) {
if ( num % i == 0 ) {
return false;
}
}
return true;
}
}

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:

number = input(‘Please enter a number:’)
i = 2
toggle = 0
while i<number:
if number%i == 0:
toggle = 1
print ("Your number is NOT a prime number!");
i = i + 1
if toggle == 0:
print ("Your number is a prime number!");

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

#Program to #Check #Leap #Year in #C #C++ C# #Java #Python

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

Program in C:

#include <stdio.h>
int main()
{
int year;
printf(“Enter a year to check if it is a leap year\n”);
scanf(“%d”, &year);
if ( year%100 == 0)
{
if ( year%400 == 0)
printf(“%d is a leap year.\n”, year);
else
printf(“%d is not a leap year.\n”, year);
}
else if ( year%4 == 0 )
printf(“%d is a leap year.\n”, year);
else
printf(“%d is not a leap year.\n”, year);
return 0;
}

Program in C++:

#include <iostream>
using namespace std ;
int main ()
{
int year ;
cout <> year ;
if ( ( year % 400 == 0 || year % 100 != 0 ) &&( year % 4 == 0 ))
cout << "It is a leap year" ;
else
cout << "It is not a leap year" ;
return 0 ;
}

Program in Java:

public class LeapYear { public static void main(String[] args) {
int year = Integer.parseInt(args[0]);
if ( year%100 == 0)
{
if ( year%400 == 0)
System.out.println(year+“ is a leap year."+year);
else
System.out.println(year+“ is not a leap year.”+year);
}
if( year%4 == 0 )
System.out.println(year+“is a leap year.”+year);
else
System.out.println(year+“ is not a leap year.”+year);
}
}

Program in C#:

1. /*
2. C# Program to Check Whether the Entered Year is a Leap Year or Not
3. */
4. using System ;
5. using System.Collections.Generic ;
6. using System.Linq ;
7. using System.Text ;
8.
9. namespace Program
10. {
11. class leapyear
12. {
13. static void Main (string [] args )
14. {
15. leapyear obj = new leapyear ();
16. obj.readdata ();
17. obj.leap ();
18. }
19. int y ;
20. public void readdata ()
21. {
22. Console.WriteLine ( “Enter the Year in Four Digits : ” );
23. y = Convert .ToInt32 ( Console.ReadLine ());
24. }
25. public void leap ()
26. {
27. if (( y % 4 == 0 && y % 100 != 0 ) || ( y
% 400 == 0 ))
28. {
29. Console . WriteLine (“{0} is a Leap Year” , y );
30. }
31. else
32. {
33. Console . WriteLine (“{0} is not a Leap Year” , y );
34. }
35. Console.ReadLine ();
36. }
37. }
38. }

Program in Python:

def IsLeapYear(year):
if((year % 4) == 0):
if((year % 100) == 0):
if( (year % 400) == 0):
return 1
else:
return 0
else:
return 1
return 0 n = 0
print “Program to check Leap Year”
print “Enter Year: “, n = input()
if( IsLeapYear(n) == 1):
print n, “is a leap year”
else:
print n, “is NOT a leap year”

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

Scan this QR Code on LinkedIn

Scan this QR Code on LinkedIn