The below program performs arithmetic and bitwise operations based on number entered by user. Scanner class is used to take input from the user, which is inside java.util.Scanner class. System.in is used to use input classes. Example import java.util.Scanner; class ArithmeticOperation { //arithmetic operation on float data public static void main(String args[]) { int n1,n2; int fChoice; Scanner sc=new Scanner(System.in); System.out.println("Select Choice\n"); System.out.println("1. Arithmetic Operation"); System.out.println("2. Bitwise Operation"); System.out.println("Enter Your Choice"); fChoice=sc.nextInt(); System.out.println("\nEnter First Value"); n1=sc.nextInt(); System.out.println("Enter Second Value"); n2=sc.nextInt(); switch(fChoice) { case 1: ArithmeticOp(n1,n2); break; case 2: BitwiseOp(n1,n2); break; default: System.out.println("Invalid Choice"); break; } } public static void ArithmeticOp(int…
-
-
In this program, we iterate the loop and print Fizz when the current number is multiple of 3, and print Bizz when the current number is multiple of 5, and print Fizz-Bizz when the current number is multiple of 3 and 5. The below example illustrate this. Example class FizzBizzDemo { //print fizz when number is multiply by 3 and bizz when number is multiply by 5 and for both print fizz-bizz public static void main(String args[]) { int i; for(i=0;i<=50;i++) { if(i%3==0 && i%5==0) { System.out.println(i+" - Fizz-Bizz"); } else { if(i%3==0) { System.out.println(i+" - Fizz"); } else if(i%5==0)…
-
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…
-
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
-
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…
-
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…
-
[pdf-embedder url=”http://tejsumeru.com/wp-content/uploads/2019/07/unit-1.pdf” title=”CNS Unit 1″]
-
In if…else statement, if the condition is true then the statements inside if statements are executed, and if the condition is false then statements inside else are executed. Syntax if(Condition or Expression) { //Statements } else { //Statements } Example #include <iostream> using namespace std; int main() { int number; cout << "Enter an integer value: "; cin >> number; if ( number >= 0) { cout << "You entered a positive integer: " << number << endl; } else { cout << "You entered a negative integer: " << number << endl; } cout << "This line is always…
-
In simple if statement the line of code inside if statement are executed if the condition or expression is true. Syntax if (Expression or Condition) { // statements } Example #include <iostream> using namespace std; int main() { int number; cout << "Enter an integer: "; cin >> number; //if number is positive then statements inside if statements are executed. if ( number > 0) { cout << "You entered a positive integer: " << number << endl; } cout << "This statement is always executed."; return 0; } OutPut Enter an integer: 5 You entered a positive number: 5…
-
# include <iostream> using namespace std; int main() { char operation; float no1, no2; cout << "Enter operationerator either + or - or * or /: "; cin >> operation; cout << "Enter two operationerands: "; cin >> no1 >> no2; switch(operation) { case '+': cout << no1+no2; break; case '-': cout << no1-no2; break; case '*': cout << no1*no2; break; case '/': cout << no1/no2; break; default: cout << "Error! operation is not correct"; break; } return 0; } OutPut Enter operator either + or - or * or divide : + Enter two operands: 3.4 8.4 3.4 -…