Alphalearningschool
        

<<< Prev

Next >>>

Up
java excercise 2

 

11) Java Program to   Calculate Average and Percentage Marks

To calculate average and percentage marks of a students, you have to ask to the user to enter marks obtained in some subjects (say 5). Place summation of 5 subject's marks in a variable say sum and place sum/5 in a variable say avg then place sum/500*100 in a variable say perc, then display the result on the output screen          

 

import java.util.Scanner;

public class Avgpercentage
{
    public static void main(String args[])
    {
        int mark[] = new int[5];
        int i;
        float sum=0;
        float avg, perc;
        Scanner scan = new Scanner(System.in);
		
        System.out.print("Enter marks Obtained in 5 Subjects : ");
        for(i=0; i<5; i++)
        {
            mark[i] = scan.nextInt();
            sum = sum + mark[i];
        }
		
        avg = sum/5;
        perc = (sum/500) * 100;
		
        System.out.print("Average Marks = " +avg);
		
        System.out.print("\nPercentage = " +perc+ "%");
    }
}

 

Output:

12)Program to Calculate Arithmetic Mean or Average

To calculate the arithmetic mean of some numbers in Java Programming, you have to ask to the user to enter number size then ask to enter the numbers of that size to perform the addition, then make a variable responsible for the average and place addition/size in average, then display the result on the output screen

 

Arithmeticmean
{
    public static void main(String args[])
    {
        int n, i, sum=0, armean;
        int arr[] = new int[50];
        Scanner scan = new Scanner(System.in);
		
        System.out.print("How many Number you want to Enter ? ");
        n = scan.nextInt();
		import java.util.Scanner;

public class
        System.out.print("Enter " +n+ " Numbers : ");
        for(i=0; i<n; i++)
        {
            arr[i] = scan.nextInt();
            sum = sum + arr[i];
        }
		
        armean = sum/n;
		
        System.out.print("Arithmetic Mean = " +armean);
    }
}

 

Output:

13) Java Program to Total Surface Area Of  Cuboid

  The total surface area of a cuboid is the sum of areas of its six faces.

 

 

import java.util.Scanner;

public class  Totalsurfaceofcuboid
{
      
   public static void main(String args[]) 
    {   
       	
            	
Scanner s= new Scanner(System.in);
System.out.println("Enter the length of Cubiod:");
double l=s.nextDouble();
 
System.out.println("Enter the breadth of Cubiod:");
double b=s.nextDouble();
 
System.out.println("Enter height of Cubiod:");
double h=s.nextDouble();
   		 
      double  area=(2*((l*b)+(b*h)+(h*l)));
 
  System.out.println("Total SurfaceArea Of Cuboid is:" +area);   
          
   }
}

 

Output:

14) Java Program To Calculate Electricity Bill 

To calculate the arithmetic mean of some numbers in Java Programming, you have to ask to the user to enter number size then ask to enter the numbers of that size to perform the addition, then make a variable responsible for the average and place addition/size in average, then display the result on the output screen

import java.util.Scanner;
public class Calelectricitybill
{
    public static void main(String args[]) 
    {   
   long units;

   Scanner sc=new Scanner(System.in);

   System.out.println("enter number of units");
   
       units=sc.nextLong();

 	   double billpay=0;

       if(units<100)
	billpay=units*1.20;

	    else if(units<300)
	billpay=100*1.20+(units-100)*2;

    else if(units>300)
	billpay=100*1.20+200 *2+(units-300)*3;

          System.out.println("Bill to pay : " + billpay); 
} 
}

 

Output:

15)  Java Program to Calculate Grade of Students

To calculate the grade of a student on the basis of his/her total marks in Java Programming, you have to ask to the user to enter the marks obtained in some subjects (5 subjects here), then calculate the percentage and start checking for the grades to display the grade on the output screen.

 

import java.util.Scanner;

public class Gradeofstudent
{
    public static void main(String args[])
    {
        int mark[] = new int[5];
        int i;
        float sum=0, avg;
        Scanner scan = new Scanner(System.in);
		
        System.out.print("Enter Marks Obtained in 5 Subjects : ");
        for(i=0; i<5; i++)
        {
            mark[i] = scan.nextInt();
            sum = sum + mark[i];
        }
		
        avg = sum/5;
		
        System.out.print("Your Grade is ");
        if(avg>80)
        {
            System.out.print("A");
        }
        else if(avg>60 && avg<=80)
        {
            System.out.print("B");
        }
        else if(avg>40 && avg<=60)
        {
            System.out.print("C");
        }
        else
        {
            System.out.print("D");
        }
    }
}

 

Output:

 

 

16)  Java Program to Print Table of Number

To print the table of a number in Java Programming, you have to ask to the user to enter any number and start multiplying that number from 1 to 10 and display the multiplication result at the time of multiplying on the output screen.

 

import java.util.Scanner;

public class printtableofnum
{
    public static void main(String args[])
    {
        int num, i, tab;
        Scanner scan = new Scanner(System.in);
		
        System.out.print("Enter a Number : ");
        num = scan.nextInt();
		
        System.out.print("Table of " + num + " is\n");
        for(i=1; i<=10; i++)
        {
            tab = num*i;
           System.out.print(num + " * " + i + " = " + tab+"\n");
        }
    }
}

 

Output:

 

17)  Java Program to Print Prime Numbers

To print all the prime numbers between the particular range provided by the user in Java Programming, you have to check the division from 2 to one less than that number (say n-1), if the number divided to any number from 2 to on less than that number then that number will not be prime, otherwise that number will be prime number.

 

import java.util.Scanner;

public class Primenum
{
    public static void main(String args[])
    {
        int start, end, i, j, count=0;
        
        /* to print all the prime numbers between any range
         * enter the two number (starting and ending number)
		 */
        
        Scanner scan = new Scanner(System.in);
		
        System.out.print("Enter the Range :\n");
		
        System.out.print("Enter Starting Number : ");
        start = scan.nextInt();
        System.out.print("Enter Ending Number : ");
        end = scan.nextInt();
		
        System.out.print("Prime Numbers Between " + start + " and " +end+ " is :\n");
        for(i=start; i<=end; i++)
        {
            count = 0;
            for(j=2; j<i; j++)
            {
                if(i%j == 0)
                {
                    count++;
                    break;
                }
            }
            if(count == 0)
            {
                System.out.print(i + " ");
            }
        }
    }
}

 

Output:

18) Java Program to  hange Two Numbers

To interchange two numbers in Java Programming, make a variable say temp of the same type, place the first number to the temp, then place the second number to the first and place temp to the second

 

import java.util.Scanner;

public class Interchangenum
{
    public static void main(String args[])
    {
        int a, b, temp;
        Scanner scan = new Scanner(System.in);
		
        System.out.print("Enter Value of A and B :\n");
        System.out.print("A = ");
        a = scan.nextInt();
        System.out.print("B = ");
        b = scan.nextInt();
		
        temp = a;
        a = b;
        b = temp;
		
        System.out.print("Number Interchanged Successfully..!!\n");
		
        System.out.print("A = " +a);
        System.out.print("\nB = " +b);
    }
}

 

 

Output: 

19) Java Program to   Count Positive, Negative and Zero

To count the number of positive number, negative number, and zero from the given set of numbers entered by the user, you have to first ask to the user to enter a set of numbers (10 numbers here) to check all the number using for loop to count how many positive, negative, and zero present in the provided set of numbers and display the output on the screen as shown in the following program

 

/* Java Program Example - Count Positive, Negative, Zero */
		
import java.util.Scanner;

public class Countposnegzero
{
    public static void main(String args[])
    {
        int countp=0, countn=0, countz=0, i;
        int arr[] = new int[10];
        Scanner scan = new Scanner(System.in);
		
        System.out.print("Enter 10 Numbers : ");
        for(i=0; i<10; i++)
        {
            arr[i] = scan.nextInt();
        }
        for(i=0; i<10; i++)
        {
            if(arr[i] < 0)
            {
                countn++;
            }
            else if(arr[i] == 0)
            {
                countz++;
            }
            else
            {
                countp++;
            }
        }
		
        System.out.print(countp + " Positive Numbers");
        System.out.print("\n" + countn + " Negative Numbers");
        System.out.print("\n" + countz + " Zero");
    }
}

 
Output:  

20) Find Largest of Two Numbers

To find the largest number of/in two numbers in Java Programming, you have to ask to the user to enter the two number to start checking which one is larger to display the largest number on the screen as shown in the following program.

 

 

/* Java Program Example - Find Largest of Two Numbers */
		
import java.util.Scanner;

public class Largestoftwo
{
    public static void main(String args[])
    {
        int a, b, big;
        Scanner scan = new Scanner(System.in);
		
        System.out.print("Enter Two Number : ");
        a = scan.nextInt();
        b = scan.nextInt();
		
        if(a>b)
        {
            big = a;
        }
        else
        {
            big = b;
        }
		
        System.out.print("Largest of Two Number is " +big);
    }
}

 Output:

 

21) Java Program to   Find Factorial of a Number

To find the factorial of any number in Java Programming, you have to ask to the user to enter the number, now find the factorial of the entered number using for loop and display the factorial result of the given number on the output screen as shown in the following program.

 

/* Java Program Example - Find Factorial of a Number */
		
import java.util.Scanner;

public class Factorial
{
    public static void main(String args[])
    {
        int num, i, fact=1;
        Scanner scan = new Scanner(System.in);
		
        System.out.print("Enter a Number : ");
        num = scan.nextInt();
		
        for(i=num; i>0; i--)
        {
            fact = fact*i;
        }
		
        System.out.print("Factorial of " + num + " is " + fact);
    }
}

 

Output:

22)  Java Program to Find HCF & LCM of Two Numbers

To find the HCF and LCM of two number in Java Programming, you have to ask to the user to enter the two number, to find the HCF and LCM of the given two number to display the value of the HCF and LCM of the two numbers on the output screen as shown in the following program

 

import java.util.Scanner;

public class HcfLcm
{
    public static void main(String args[])
    {
        int a, b, x, y, t, hcf, lcm;
        Scanner scan = new Scanner(System.in);
		
        System.out.print("Enter Two Number : ");
        x = scan.nextInt();
        y = scan.nextInt();
		
        a = x;
        b = y;
		
        while(b != 0)
        {
            t = b;
            b = a%b;
            a = t;
        }
		
        hcf = a;
        lcm = (x*y)/hcf;
		
        System.out.print("HCF = " +hcf);
        System.out.print("\nLCM = " +lcm);
    }
}

 Output: