Tuesday, 26 December 2017

Basic Java Programs For Beginners

Write a Java program to print Welcome To My World.

public class LearningPoint92{

public static void main(String[] args) {

System.out.println("Welcome To My  World\n");

}
}
Addition of Two Numbers
import java.util.Scanner;
public class Add {
public static void main(String[] args) {

  Scanner sc = new Scanner(System.in);
  
  System.out.print("Input first number:\n ");
  int num1 = sc.nextInt();
  
  System.out.print("Input second number:\n ");
  int num2 = sc.nextInt();

  System.out.println("sum is:"+ (num1 + num2));
  }
 }

Program to find Quotient and Remainder


import java.util.Scanner;

public class Check {

 public static void main(String[] args) {

 Scanner in = new Scanner(System.in);
  
  System.out.print("Input first number:\n ");
  int num1 = in.nextInt();
  
  System.out.print("Input second number:\n ");
  int num2 = in.nextInt();
  
 System.out.println("Quotient is:"+ (num1/num2));
  System.out.println("Remainder is:"+ (num1%num2));

  }

 }

Write a Java program to print the area of Circle

import java.util.Scanner;

public class Area {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.print("Enter radius:\n ");
  double r = in.nextDouble();

 System.out.println("Area of circle is"+ (3.14*r*r));

  }
 }

Compute Area of Square and Rectangle

import java.util.Scanner;

public class A
 {
public static void main(String[] args) {

 Scanner sc = new Scanner(System.in);

  System.out.print("Enter radius:\n ");
  int  a = sc.nextInt();

System.out.println("Area of square is"+ (a*a));

  System.out.print("Enter l and b \n ");
  int  l = sc.nextInt();
  int  b = sc.nextInt();

 System.out.println("Area of rectangle is"+ (l*b));
  }
 }
Compute Area of triangle

import java.util.Scanner;

public class A
 {
public static void main(String[] args) {

 Scanner sc = new Scanner(System.in);

  System.out.print("Enter base:\n ");
  int  b = sc.nextInt();

 System.out.print("Enter height:\n ");

  int  h= sc.nextInt();


 System.out.println("Area of triangle is"+ (0.5*b*h));
  }

 }
Calculate Simple Interest

import java.util.Scanner;

public class SI {

 public static void main(String[] args) {

  Scanner sc = new Scanner(System.in);

  System.out.print("Enter P,T and R \n");
  int  P = sc.nextInt();
  int  T = sc.nextInt();
  int  R = sc.nextInt();

  System.out.println("Area of rectangle is"+ ((P*T*R)/100));
  }
 }

Calculate total and average marks of 4 subjects

import java.util.Scanner;

public class B {

 public static void main(String[] args) {

 Scanner sc = new Scanner(System.in);

  System.out.print("Enter marks of 4 subjects \n");
  int  m1 = sc.nextInt();
  int  m2 = sc.nextInt();
  int  m3 = sc.nextInt();
   int  m4 = sc.nextInt();

  System.out.println("Total is"+ (m1+m2+m3+m4));

  System.out.println("Average is"+ (m1+m2+m3+m4)/4);
  }
 }

Print Student Details

import java.util.Scanner;

public class Student {

   public static void main(String[] args) {

   Scanner sc = new Scanner(System.in);
  
  System.out.print("Enter Name \n");
  String name = sc.next();

  System.out.print("Enter Age \n");
  int  age = sc.nextInt();

  System.out.print("Enter Mark \n");
  double marks = sc.nextDouble();

 System.out.println("Student information\n"+"name :"+name+"\nage:"+age+"\nmarks"+marks);
  }
 }

Write a Java program to swap two variables.

import java.util.Scanner;

public class Swap
{

public static void main(String[] args)
 {

int temp;
Scanner sc = new Scanner(System.in);

  System.out.print("Enter number1 \n");
  int  num1 = sc.nextInt();

  System.out.print("Enter number2 \n");
  int num2 = sc.nextInt();

 System.out.println("Before swapping : num1, num2 = "+num1+", "+ + num2);
   
  temp = num1;
   num1 = num2;
   num2 = temp; 
 
 System.out.println("After swapping : num1, num2 = "+num1+", "+ + num2);

  }
 }


No comments:

Post a Comment

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...