Saturday, August 1, 2015

What is neon number:

A neon number is that where sum of digits of square of the number is equal to the number.

For example if the input number is 9, it's square is 9*9 = 81 and sum of the digits is 9. 9 is a neon number


Java Program to check neon number


import java.io.*;

class Neon
{
int n,sq,sum=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void showNeon() throws Exception
{
 System.out.println("Enter the number:");
 n=Integer.parseInt(br.readLine());
 sq=n*n;
 while(sq>0)
{
 sum=sum+sq%10;
 sq=sq/10;
}
if(n==sum)
 System.out.println(n+ " is a neon number");
else
 System.out.println(n+ " is not a neon number");
}
public static void main(String args[])throws Exception
{
 Neon ob=new Neon();
ob.showNeon();
}
}
A number whose sum of digits is 1 is called a magic no. e.g.158, 91 etc.
class Magic_No
{
public static void main(int n)
{
int rem=0,s=0,s1=0,rem2=0,m=0,s2=0,rem3=0;
while(n>0)
{
rem=n%10;
s=s+rem;
n=n/10;
}
while(s>0)
{
rem2=s%10;
s1=s1+rem2;
s=s/10;
}
while(s1>0)
{
rem3=s1%10;
s2=s2+rem3;
s1=s1/10;
}
if(s1==1||s2==1)
{
System.out.println(“MAgic no.”);
}
else
{
System.out.println(“Normal”);
}
}
}



Program: Write a program to find perfect number or not.


Description:
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. Equivalently, a perfect number
is a number that is half the sum of all of its positive divisors.
The first perfect number is 6, because 1, 2 and 3 are its proper
positive divisors, and 1 + 2 + 3 = 6. Equivalently, the number 6
is equal to half the sum of all its positive divisors:
                               ( 1 + 2 + 3 + 6 ) / 2 = 6.

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.java2novice.algos;

public class IsPerfectNumber {

    public boolean isPerfectNumber(int number){
         
        int temp = 0;
        for(int i=1;i<=number/2;i++){
            if(number%i == 0){
                temp += i;
            }
        }
        if(temp == number){
            System.out.println("It is a perfect number");
            return true;
        } else {
            System.out.println("It is not a perfect number");
            return false;
        }
    }
     
    public static void main(String a[]){
        IsPerfectNumber ipn = new IsPerfectNumber();
        System.out.println("Is perfect number: "+ipn.isPerfectNumber(28));
    }
}

Output:
28
It is a perfect number
Is perfect number: true

Java program to print a Pascal Triangle

Pascal triangle is a triangular array of binomial coefficients. The first row of pascal triangle contains 1.
The subsequent rows are formed by adding the above left number with the above right number. We will write a Java Program to print a Pascal Triangle.
The program takes the number of rows as input and display the pascal triangle. It uses three for loops.
The first for loop is used to determine the row in which the numbers should be printed. The second for loop
is used to print the spaces in the beginning of the line and the other for loop is used to print the number.
Java program to print a Pascal Triangle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.io.*;
class Pascal
{
    public static void main(String[] args) throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("\nEnter the number of rows : ");
        int r = Integer.parseInt(br.readLine());
        for(int i=0;i<r;i++)
        {
            for(int k=r;k>i;k--)
            {
                System.out.print(" ");
            }
            int number = 1;
            for(int j=0;j<=i;j++)
            {

                 System.out.print(number+" ");
                 number = number * (i - j) / (j + 1);
                  
            }
            System.out.println();
        }

    }
}
Output:.

This java program reverses a string entered by the user. We use charAt method to extract characters from the string and append them in reverse order to reverse the entered string.

Java programming code

import java.util.*;
 
class ReverseString
{
   public static void main(String args[])
   {
      String original, reverse = "";
      Scanner in = new Scanner(System.in);
 
      System.out.println("Enter a string to reverse");
      original = in.nextLine();
 
      int length = original.length();
 
      for ( int i = length - 1 ; i >= 0 ; i-- )
         reverse = reverse + original.charAt(i);
 
      System.out.println("Reverse of entered string is: "+reverse);
   }

}

No comments:

Post a Comment