Saturday, 24 November 2018

JAVA PROGRAMS

JAVA PROGRAMS



PROBLEM 1

Write a program  in java using for loop to print all the odd and even number upto 30 terms.

solution

import java.util.Scanner;

public class CHECK_EvenOdd {

    public static void main(String args[])
    {
        System.out.print("Printing Even numbers between 1 and 10 :");
        for(int i=1; i <= 10; i++)
        {             
        if(i % 2 == 0)         
        {     
               System.out.print(i + " ");
              }
   
        }
        System.out.print("\nPrinting odd numbers between 1 and 10 :");
        for(int i=1; i <= 10; i++)
        {             
        if(i % 2 != 0)              {     
                  System.out.print(i + " ");
              }
   
        }
    }
}
When you run the program, the output will be:

PROBLEM 2

Write a program  in java to print all the factors of 20.

solution

public class Factors {

    public static void main(String[] args) {

        int number = 20;

        System.out.print("Factors of " + number + " are: ");
        for(int i = 1; i <= number; ++i) {
            if (number % i == 0) {
                System.out.print(i + " ");
            }
        }
    }

}

When you run the program, the output will be:

PROBLEM 3

Write a program using while loop to generate the first 10 natural numbers and their sum.

solution

public class SumNatural 
{

    public static void main(String[] args) 
    
    {
        int num = 10, sum = 0,i=1;        
        System.out.println ("The first 10 natural numbers are:\n");     
        while(i <= num)
        {
            sum = sum + i;   
            System.out.print (i+" ");
            i++;
        }
         System.out.println ("\n");
        System.out.println("The Sum of First 10 Natural numbers = " + sum);
    }
}

When you run the program, the output will be:


PROBLEM 4

Write a program to check whether the product of two numbers is a buzz number or not.

Buzz Number: A number is said to be Buzz Number if it ends with 7 or is divisible by 7.

Example: 1007 is a Buzz Number as it end with 7. 343 is also a Buzz Number as it is divisible by 7 and 77777 is also a Buzz Number as it ends with 7 and also it is divisible by 7.

solution


class BUZZ
{
public static void main(int num)
{

 System.out.println("Enter the number:"+num);
if ( num % 10 == 7  ||  num % 7 == 0 )
System.out.println("Entered number is a Buzz number.");
else
System.out.println("Entered number is not a Buzz number.");
}
}





When you run the program, the output will be:




PROBLEM 5


Write a program to print the Fibonacci series  upto 10 terms

solution


public class Fibonacci {

    public static void main(String[] args) {

        int n = 10, t1 = 0, t2 = 1;
        System.out.print("First " + n + " terms: ");

        for (int i = 1; i <= n; ++i)
        {
            System.out.print(t1 + "  ");

            int sum = t1 + t2;
            t1 = t2;
            t2 = sum;
        }
    }
}

output

































PROBLEM 6


Write a program to print all the prime numbers between 1 and 100.

solution



class PrimeNumbers
{
   public static void main (String[] args)
   {
       int i =0;
       int num =0;
       String  primeNumbers = "";

       for (i = 1; i <= 100; i++)       
       {  
          int counter=0;
          for(num =i; num>=1; num--)
  {
             if(i%num==0)
     {
  counter = counter + 1;
     }
  }
  if (counter ==2)
  {
           primeNumbers = primeNumbers + i + " ";
  }
       }
       System.out.println("Prime numbers from 1 to 100 are :");
       System.out.println(primeNumbers);
   }

}




OUTPUT


































PROBLEM 7


Write a program to print the largest of three numbers.

solution

class Largest
{
  public static void main(int num1,int num2,int num3)
  {
      System.out.println("Enter three integers");
      System.out.println(num1);
      System.out.println(num2);
      System.out.println(num3);

      if( num1 >= num2 && num1 >= num3)
          System.out.println(num1+" is the largest Number");

      else if (num2 >= num1 && num2 >= num3)
          System.out.println(num2+" is the largest Number");

      else
          System.out.println(num3+" is the largest Number");
  }
}



OUTPUT











































PROBLEM 8


Write  a java program using the Switch case to print the corresponding days of numbers.

solution




class WeekDays
{
    public static void main(int day)
    {
         switch(day)
        {
            case 1:
                System.out.println("Monday");
                break;
            case 2:
                System.out.println("Tuesday");
                break;
            case 3:
                System.out.println("Wednesday");
                break;
            case 4:
                System.out.println("Thursday");
                break;
            case 5:
                System.out.println("Friday");
                break;
            case 6:
                System.out.println("Friday");
                break;
             case 7:
                System.out.println("Sunday");
                break;
        }
    }
}

PROBLEM 9


Write a java program to display whether the entered character is uppercase or lowercase

solution


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Alphabet_Check 
{
    public static void main(String args[]) throws IOException
    {
        char m;
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter any alphabet:");
        m = (char) bf.read();
        if(m >= 97 && m <= 123)
        {
            System.out.println("Lower Case");
        }
        else if(m >= 65 && m <= 96)
        {
            System.out.println("Upper Case");
        }
        else if(m >= 48 && m <= 57)
        {
            System.out.println("Digit");
        }
    }

}



PROBLEM  10


Write a java program to display series of 1, 8, 25, 64, 125 upto 10th terms.


solution







public class series {

    public static void main(String[] args) {

        int n = 10,d;
        

        for (int i = 1; i <= n; ++i)
        {
            d=i*i*i;
            System.out.print(d + " , ");         
        }
    }

}


PROBLEM  11


Write a java program to display series of 0, 3, 8 , 15 ,24 ,35 upto 10th terms.



solution



public class series {

    public static void main(String[] args)
    {            
int n;
int x = 3;
int s = 0;
for (n=0;n<10;n++)
{
System.out.print(s+" ");
s = s + x;
x = x + 2;
}
    }
}


PROBLEM  12


Write a java program to display series of  1, 4 , 7, 10 upto 10th terms.



solution


public class series {

    public static void main(String[] args)
    {            
int n;
int x = 3;
int s = 1;
for (n=0;n<10;n++)
{
System.out.print(s+" ");
s = s + x;

}
    }
}



Class XI Computer Application Wbchse

 Class XI Computer Application Wbchse