Saturday 30 December 2017

Control Statements Programs In Java

To check a number is Odd or Even?

import java.util.Scanner;
public class Check{

public static void main(String[] args) {

 Scanner sc = new Scanner(System.in);

 System.out.println("Enter an number\n");
   int n=sc.nextInt();

 if (n%2 == 0)
{
   System.out.println("Even\n");
}
   else
{
     System.out.println("Odd\n");
}
}
}

To check  whether a year is leap year or Not?
import java.util.Scanner;
public class A {

public static void main(String[] args) {

 Scanner sc = new Scanner(System.in);

 System.out.println("Enter year\n");
  int year=sc.nextInt();

 if (year%4 == 0)
{
   System.out.println("Leap Year\n");
 }
   else{
     System.out.println("Non Leap Year\n");
 }

}
}

Calculate sum of digits of a number?

import java.util.Scanner;
public class Digit {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
int  t, sum = 0, remainder;

System.out.println("Enter a Number\n");
  int num=sc.nextInt();
   t = num;

  while (t != 0)
   {
      remainder = t % 10;
      sum = sum + remainder;
      t= t / 10;
   }
  System.out.println("Sum of digits \n"+(sum));
}
}

Calculate Factorial Number?

import java.util.Scanner;
public class Facto {

public static void main(String[] args) {

 Scanner sc = new Scanner(System.in);
  long fact = 1;

 System.out.println("Enter a number\n");
  int n=sc.nextInt();

 while(n>0)
  {
      fact=fact*n;
      n--;
  }
System.out.println("Factorial is \n"+(fact));
}
}
Swapping of two numbers?

import java.util.Scanner;
public class B {

public static void main(String[] args) {

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

System.out.println("Enter the value of x and y\n");
 int x=sc.nextInt();
 int y=sc.nextInt();

  System.out.println("Before Swapping x \n and y \n"+ x +"\t "+y);
   temp = x;
   x = y;
   y = temp;
System.out.println("After Swapping  x \n and y \n "+x+"\t" +y);
}
}

To find whether a number is Armstrong number or not?

import java.util.Scanner;
public class Arms {

public static void main(String[] args) {

 Scanner sc = new Scanner(System.in);
int n, sum = 0, t, remainder;
System.out.println("Enter number \n");
n=sc.nextInt();
 t = n;

  while( t != 0 )  {
      remainder = t%10;
      sum = sum + remainder*remainder*remainder;
      t = t/10;
   }

   if ( n == sum )
      System.out.println("The number is an Armstrong number ");
   else
      System.out.println("The number  is not an Armstrong number");
}
}

Find the Largest Number Among Three Numbers

import java.util.Scanner;
public class Large {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter a \n");
int a=sc.nextInt();
System.out.println("Enter b \n");
int b=sc.nextInt();
System.out.println("Enter c \n");
int c=sc.nextInt();

  if(a>=b && a>=c)
        System.out.println("\nThe largest number is\n"+a);
      else if(b>=a && b>=c)
         System.out.println("\nThe largest number is \n"+b);
      else
         System.out.println("\nThe largest number is \n"+c);
}
}

SWAPPING OF 2 NUMBERS WITHOUT THIRD VARIABLE

import java.util.Scanner;
public class Swap {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of x and y\n");

int x=sc.nextInt();
int y=sc.nextInt();

   System.out.println("Before Swapping x\n and y\n"+ x +"\t" +y);
   x=x-y;
   y=x+y;
   x=y-x;
System.out.println("After Swapping  x \n and y\n "+x+"\t" +y);
}
}

Find square and cube of a given number

import java.util.Scanner;
public class A {

public static void main(String[] args) {

 Scanner sc = new Scanner(System.in);

System.out.println("Enter no \n");
int n=sc.nextInt();

 System.out.println("Square of the number"+(n*n));

 System.out.println("Cube of the number"+(n*n*n));
}
}

To Check Whether a Number is Prime or Not

import java.util.Scanner;
public class Prm {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
int i,pm = 0;
System.out.println("Please enter a number: \n");
int num=sc.nextInt();

   for(i=1; i<=num; i++)
   {
      if(num%i==0)
      {
         pm++;
      }
   }
   if(pm==2)
   {
      System.out.println("it is a prime number.");
   }
   else
   {
      System.out.println("it is not a prime number.");
   }
}
}

Write a program to find reverse of a number

import java.util.Scanner;
public class R {

public static void main(String[] args) {

 Scanner sc = new Scanner(System.in);
int reverse = 0;
System.out.println("Please enter a number: \n");
int n=sc.nextInt();

   while (n != 0)
   {
      reverse = reverse * 10;
      reverse = reverse + n%10;
      n  = n/10;
   }

System.out.println("Reverse of entered number is =\n"+reverse);
}
}
To check whether a number is palindrome or Not
import java.util.Scanner;
public class Palnd {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
int temp,reverse = 0;
System.out.println("Please enter a number: \n");
int n=sc.nextInt();
temp=n;

   while (temp != 0)
   {
      reverse = reverse * 10;
      reverse = reverse + temp%10;
      temp  = temp/10;
   }

if ( n == reverse )
      System.out.println("it is a palindrome number.\n");
   else
      System.out.println("it is not a palindrome number.\n");
}

}

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...