Skip to main content

Posts

Showing posts from September, 2021

Even_odd.java #Program to find Odd and Even

// Write a program to print whether a number is even or odd, also take input. import   java . util . Scanner ; public   class   Odd_Even  {      public   static   void   main ( String []  args ) {                   Scanner   in  =  new   Scanner ( System . in );          System . out . print ( "Enter the number to check even or odd :" );          int   num  =  in . nextInt ();          if  ( num  %  2  == 0 ) {              System . out . println ( num +  " is an even number." );         }          else {              System . out . println ( num +  " is a odd number." );         }          in . close ();     } }

Program to Print the table of the Number

// Take a number as input and print the multiplication table for it. import   java . util . Scanner ; public   class   Table  {      public   static   void   main ( String []  args ) {          Scanner   in  =  new   Scanner ( System . in );          System . out . print ( "Enter a number to multiply and get the table : " );          int   num  =  in . nextInt ();          for  ( int   i  =  1 ;  i  <= 10 ;  i ++) {              System . out . println ( num +  " x "  + i +  "= "  +( i * num ));         }          in . close ();     } }

Leap year Program in Java

import   java . util . Scanner ; // Input a year and find whether it is a leap year or not. public   class   Leap_year {      public   static   void   main ( String []  args ) {                   Scanner   in  =  new   Scanner ( System . in );          System . out . print ( "Please enter a year : " );          int   year  =  in . nextInt ();                   boolean   ans  =  checkyear ( year );          if  ( ans  ==  true ) {              System . out . println ( year  +  " is a Leap Year." );         }          else {              System . out . println ( year   + " is not a leap year." );         }          in . close ();     }      static   boolean   checkyear ( int   year ){          if  ( year  %  100  ==  0 ) {              if  ( year  %  400  ==  0 ) {                  return   true ;             }         }          else {              if  ( year  %  4  ==  0 ) {                  return   true ;             }         }          return   false ;

16.Conditional Statements in Java (If , If-else , else- if )

                                                           CONDITIONAL STATEMENTS A conditional statement  consists of a condition and a task . When the condition is true, the application performs the task. The condition portion of a conditional statement is also called an expression. There are four types of conditional statements in java :  If Condition. If else condition. If else If condition. Nested If condition. If condition :  The Java if statement tests the condition. It executes the  if block  if condition is true. if (condition){   //code to be executed    }   If else Condition :  The  Java   if statement  is used to test the condition. It checks  boolean  condition:  true  or  false . There are various types of if statement in Java. if (condition){   //code if condition is true    } else {   //code if condition is false    }    If Else If  Conditions:  The if-else-if ladder statement executes one condition from multiple statements. if (condition1){   //code to be executed if c