NewtronLearner

The task of education is to teach someone to think deeply and think critically. If you are living in 2020 So computers are very essential for you. Technology is being increased on daily bases. Such popular programmers and engineers are leading the revolution among the world. newtronlearner will provide you all the details from beginner level to Professional level. Join the program to know more.

Something About Me

test

New Posts

Post Top Ad

Your Ad Spot

Monday 23 November 2020

Some Important Programs for Java of Class 9


Write a program for finding a sum of digits. for example - 123, sum of digits is 6

(Using do while loop)

/*Pay attention. This program will take user defined input from the user.*/

Input: Enter an integer 123

Output: Sum of digits = 6

Program Code -

import java.util.Scanner;

public class Digit_Sum_DoLoop

{

    public static void main(String args[])

    {

        int m, n, sum = 0;

        Scanner s = new Scanner(System.in);

        System.out.print("Enter the number:");

        m = s.nextInt();

        do

        {

            n = m % 10;

            sum = sum + n;

            m = m / 10;

        }while(m > 0);

        System.out.println("Sum of Digits:"+sum);

    }

}


Write a program for finding a product of digits. for example - 123, product of digits is 6

(Using do while loop)

/*Pay attention. This program will take user defined input from the user.*/

Input: Enter an integer 123

Output: Product of digits = 6

Program Code -

import java.util.Scanner;

public class Digit_Product_DoLoop

{

   static int getProduct(int n) 

    { 

        int product = 1; 

  

        while (n != 0) { 

            product = product * (n % 10); 

            n = n / 10; 

        }   

        return product; 

    } 

      public static void main(String[] args) 

    { 

        Scanner s = new Scanner(System.in);

        System.out.print("Enter the number:");

        int n = s.nextInt(); 

        System.out.println(getProduct(n)); 

    }  

}


Write a program to input an integer and check whether it is Palindrome or not, using do while loop.

A Palindrome number is a number whose reversed number is equal to the number. Eg 131,77,3443 

(Using do while loop)

/*Pay attention. This program will take user defined input from the user.*/

Input: Enter an integer 131

Output: 131 is Palindrome number

Program Code -

import java.util.Scanner;

public class Palindrome_DoLoop

{

   public static void main(String[] args) 

    { 

        int reversedInteger = 0, remainder, originalInteger;

        Scanner s = new Scanner(System.in);

        System.out.print("Enter the number:");

        int num = s.nextInt(); 

        originalInteger = num;

        do

        {

            remainder = num % 10;

            reversedInteger = reversedInteger * 10 + remainder;

            num  /= 10;

        }while( num != 0 );


        if (originalInteger == reversedInteger)

            System.out.println(originalInteger + " is a palindrome.");

        else

            System.out.println(originalInteger + " is not a palindrome."); 

    }  

}


Write a program to input an integer and check whether it is Spy or not?

A Spy number is a number whose sum of digits is equal to the product of digits.
Eg 1124 
Sum of digits = 1+1+2+4 = 8 
Product of digits = 1*1*2*4= 8 
Input: Enter an integer 1124 
Output: 1124 is a Spy number
import java.util.Scanner;

public class SpyNumber 
{
	public static void main(String[] args)
	{   
		int n,product=1,sum=0;
		int ld;

		Scanner sc = new Scanner(System.in);

		System.out.print("Enter the number :" );

		n=sc.nextInt();

		while(n>0)
		{
			ld=n%10;
			sum=sum+ld;
			product=product*ld;
			n=n/10;
		}

		if(sum==product)
			System.out.println("Given number is spy number");
		else
			System.out.println("Given number is not spy number");
	}
}

Write a program to input an integer and check whether it is Niven or not? 

A Niven number is a number which is completely divisible by the sum of its digits. Eg 21, 18 Sum of digits of 21 = 3 21 is divisible by 3. Input: Enter an integer 21 Output: 21 is a Niven number 


import java.util.*;
class Niven_Number
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
         
        System.out.print("Enter a number : ");
        int n = sc.nextInt();
        int c = n, d, sum = 0;
         
        do
        {
            d = c%10;
            sum = sum + d;
            c = c/10;
        }while(c>0);
         
        if(n%sum == 0)
            System.out.println(n+" is a Niven Number.");
        else
            System.out.println(n+" is not a Niven Number.");     
    }
}


Write a program to input an integer and check whether it is Armstrong
or not, using do while loop.



public class Armstrong {

    public static void main(String[] args) {

        int number = 371, originalNumber, remainder, result = 0;

        originalNumber = number;

        do
        {
            remainder = originalNumber % 10;
            result += Math.pow(remainder, 3);
            originalNumber /= 10;
        }while (originalNumber != 0);
if(result == number) System.out.println(number + " is an Armstrong number."); else System.out.println(number + " is not an Armstrong number."); } }

Write a program to input an integer and check whether it is Pronic or
not?


import java.util.Scanner; public class Example13Pronic { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.print("Input a number : "); int num = sc.nextInt(); int ans = 0; for(int i=0; i<num; i++) { if(i*(i+1) == num) { ans = 1; break; } } if(ans == 1) System.out.println("Pronic Number."); else System.out.println("Not a Pronic Number."); } }

Write a program to input an integer and check whether it is Disarium or
not?


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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.util.Scanner;
 
public class JavaDisariumNumberProgram
{
    private static boolean isItDisariumNumber(int inputNumber)
    {
        int noOfDigits = Integer.toString(inputNumber).length();
         
        int copyOfInputNumber = inputNumber;
         
        int sum = 0;
         
        while (inputNumber > 0)
        {
            int lastDigit = inputNumber % 10;
             
            sum = sum + (int) Math.pow(lastDigit, noOfDigits);
             
            inputNumber = inputNumber / 10;
             
            noOfDigits--;
        }
         
        if (sum == copyOfInputNumber)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
     
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
         
        System.out.println("Enter a number :");
         
        int inputNumber = sc.nextInt();
         
        if (isItDisariumNumber(inputNumber))
        {
            System.out.println(inputNumber+" is a Disarium number");
        }
        else
        {
            System.out.println(inputNumber+" is not a Disarium number");
        }
         
        sc.close();
    }
}

Write a program to input time in seconds and display the time broken up as hours, minutes and seconds.

import java.util.*;

public class Exercise55 {

public static void main(String[] args)

{

Scanner in = new Scanner(System.in);

System.out.print("Input seconds: ");

int seconds = in.nextInt();

int p1 = seconds % 60;

int p2 = seconds / 60;

int p3 = p2 % 60;

p2 = p2 / 60;

System.out.print( p2 + ":" + p3 + ":" + p1);

System.out.print("\n");

}

}


Write a program to input a floating point number and round it off to the nearest integer.


class Main_concne
{
    public static void main(String[] args)
    {
        float x = 5.60f;
        int y = (int)x;
        System.out.println("y = " + y);
    }
}



Write a program to input the Basic Pay of an employee and find the gross pay of the employee for the following allowances and deductions 


public class employee

{

public static void main(double salary)

{

double allowance=30/100*salary;

double rent =15/100*salary;

double pf=12.5/100*salary;

double gross=salary+allowance+rent;

double net=salary-pf

System.out.println("gross pay=Rs."+gross);

System.out.println("net pay=Rs."+net)'

}

}


Write a program to accept 3 sides of a triangle, And check whether it can form a triangle or not.If it forms a triangle , check whether it is an acute angled, obtuse angled or right angled triangle.


public class accept3s

{

public static void main(String[] args)

{

Scanner Sc=new Scanner (System.in);

int a,b,c;

a=Sc.nextInt();

b=Sc.nextInt();

c=Sc.nextInt();

if((a+b+c)==180)

{

System.out.println("Triangle possible");

if (a==90)

System.out.println("Right angled");

else if ((a<90)&&(a>0))

System.out.println("Acute angle");

else if (a>90)&&(a<180)

System.out.println("Obtuse angled");

}

else

System.out.println("not possible");

}

}


A library charges a fine for books returned late. Following are the fines


class retLate

{

public static void main(String args[])

{

double fine=0.0; int n;

System.out.println("Enter the no of days late");

n = 5

if(n<=5)

fine=0.40*n;

else if(n<=10)

fine=0.65*n;

else

fine=0.80*n;

System.out.println("Fine = "+fine);

}

}


Write a menu driven program to input an integer and a choice of character by the user as M or B. If choice is M check whether the number is Magic or not.



class MagicNum
{
   public static boolean isMagic(int n)
   {
       int sum = 0;
       

       while (n > 0 || sum > 9)
       {
           if (n == 0)
           {
               n = sum;
               sum = 0;
           }
           sum += n % 10;
           n /= 10;
       }
       
       return (sum == 1);
   }
    
   public static void main(String args[])
    {
     int n = 1234;
     if (isMagic(n))
        System.out.println("Magic Number");
           
     else
        System.out.println("Not a magic Number");
    }
}

Write a program to input three integers and check
whether it forms Pythagorean triplet or not.


import java.io.*;
import java.util.*;
  
class PythaTriCon {
  
    static void pythagoreanTriplets(int limit)
    {
  
        int a, b, c = 0;
  
        int m = 2;
  
   
        while (c < limit) {
  
            for (int n = 1; n < m; ++n) {
  
                a = m * m - n * n;
                b = 2 * m * n;
                c = m * m + n * n;
  
                if (c > limit)
                    break;
  
                System.out.println(a + " " + b + " " + c);
            }
            m++;
        }
    }
  
    public static void main(String args[])
    {
        int limit = 20;
        pythagoreanTriplets(limit);
    }
}

No comments:

Support on Facebook Page

Post Top Ad

Your Ad Spot