• Java

    Write a Java program to get particulars of his/her birthday and display it as shown below. Use 3 variables to hold date, month and year.

    In this we are taking date, month and year with three different variable and then check whether the date is valid or not. The below program shows how to check whether the data value is valid or not. Example import java.util.*; class GetBirthDate { //get the birthdate of his or her public static void main(String args[]) { int dt,month,year,leap=0,noOfDay=0; Scanner sc=new Scanner(System.in); System.out.println("Enter Birth Year"); year=sc.nextInt(); if(year>2019) { System.out.println("Year Is Invalid Please Enter Proper Year"); } else { if(year%4==0) { leap=1; } System.out.println("Enter Birth Month"); month=sc.nextInt(); switch(month) { case 1: noOfDay=31; break; case 2: if(leap==1) noOfDay=29; else noOfDay=28; break; case…

  • Java

    Write a Java program to print the ASCII values for characters entered by the user.

    Character is stored in its ASCII format in java. The standard set of character known as ASCII still ranges from 0 to 127 as always. Example: import java.util.Scanner; class FindASCII { //get ASCII value of character public static void main(String args[]) { char c,con; int i; Scanner sc; do { sc=new Scanner(System.in); System.out.println("Enter Any Character"); c=sc.next().charAt(0); i=c; System.out.println("ASCII Value Of "+c+" Is "+i); System.out.println("Do You Want To Continue (y/n) "); con=sc.next().charAt(0); }while(con=='y' || con=='Y'); } } OutPut Enter Any Character T ASCII Value Of T Is 84 Do You Want To Continue (y/n) n  

  • Java

    Justify the following statement in the context of Java. “ boolean can be true (Non-zero) or false(Zero)”.

    In java boolean datatype is used to take value true or false.  This is the type that is returned by all relational operators, as in the case a<b. boolean is also required by conditional expressions that govern the control statement such as if and for. In previous we learn about primitive datatype which give introduction of basic java’s data type. We know that boolean only take the value either true or false. Unlike in C where the boolean also takes 0 and 1 to determine false and, true  this is the not case in java. Example The below example shows…

  • Java

    Write a Java program to display all primitive type variables. Also display your name in the last line.

    The basic primitive datatypes are listed below. Integer Floating Point Character Boolean Here this types also categorized in other datatypes as follow. Integer This all stores signed, positive and negative values. byte short int long Floating Point Floating Point also known as real number are used when evaluating expression that require fraction precision. float double Character take ASCII value. In java char is 16 bit type. The range of char is 0 to 65535. There is no negative char. In java, boolean data type we cannot assign 0 or 1 or any numeric value to determines true or false. It…

  • Java

    Find The Area Of Circle And Triangle

    The formula to find area of circle :: 3.14 * radius * radius The formula to find area of triangle :: 0.5 * base * height Here, We are using two classes, one which contains main method and another one which contains the sub code. The area class contains two method one is circle() that is used to find the area of circle. It takes the radius from user using Scanner class, and the find the area as per the formula giver above. It takes value of PI is defined globally as Constant using final keyword. This method is called…

  • Java

    Find Reverse Number

    Here, First we take one number from user and assign it to temporary variable called temp. Now we execute on while loop until number is zero. Inside loop we get the last digit of the number by finding the reminder of the number by performing the modulus by 10, after that we append the digits with the formula rev=(rev*10)+reminder; first time the value of rev is 0. Now we remove last digit which is appended by performing division by 10. Now  when loop completely executed the variable rev gives use reverse number. import java.util.*; public class reverse { public static void…

  • Java

    Find Prime Number From 1 to n

    A Prime Number is a whole number greater than 1 whose only factors are 1 and itself. A factor is a whole numbers that can be divided evenly into another number. The first few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23 and 29. Numbers that have more than two factors are called composite numbers. Let’s take the last range for find prime numbers. We start one loop for get numbers from 1 to length entered by user, and one another loop inside this parent loop which is stared from 2. The child loop is used to divide the parent number with all the lesser number…

  • Java

    Check Number Is Palindrome Or Not

    A Palindromic Number is a number that is the same when written forwards or backwards.The first few palindromic numbers are therefore are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, … etc.   Let’s take one number from user. Then assign it to another temporary variable for performing operation purpose. Now take this temporary variable and execute the loop till this temp variable is not zero. Inside this loop take last digit of the number by finding modulus of the number and store to another variable,…

  • Java

    Find Maximum From Three Number By Using Ternary Operator

    The Ternary Operator is an operator that takes three arguments. The first argument is a comparison argument, the second is the result upon a true comparison, and the third is the result upon a false comparison. If it helps you can think of the operator as shortened way of writing an if-else statement.   Let’s take three numbers from user. If the numbers are two then find maximum from that numbers are much easier by using Ternary Operator as formula int max = num1 > num2 ? num1 : num2; But we have three numbers, so that it is a…

  • Java

    Find Fibonacci Series Of 1 to n

    A series of numbers in which each number ( Fibonacci number ) is the sum of the two preceding numbers. The simplest is the series 1, 1, 2, 3, 5, 8,…. etc. Here first two numbers are 0 and 1 taking by us by default. These two numbers are printed manually as part of series. After that the for loop will be executed until the length entered by user minus two, so that the two numbers that we use manually are subtracted from length. Then it will perform addition of these two numbers and print that number as part of…