Posts

Showing posts with the label Java coding problems

SWAPPING OF TWO NUMBER WITHOUT USING THIRD VARIABLE (XOR Bitwise operator)

 SWAPPING OF TWO NUMBER WITHOUT USING THIRD VARIABLE  (XOR Bitwise operator)  with the use of XOR bitwise operator we can swap two number without using third variable XOR(^) : help to inverse the binary or decimal number         ALGORITHM  Take two variable a and b assign value of a and b  (as we assign a=10 and b=15) then a = a^b ( value of a will change to 5 )           because   a  =10 =1 0 1 0  |    only one bit should be 1 so                           b =15 = 1 1 1 1  |    answer will be 1 or true                          a ^ b  =  0 1 0 1    .i.e. 5 now b = a^b (here a = 5 and b = 15) then, value of b =10           because    a  =5   =  0 1 0 1                               b =15 =  1 1 1 1                          a ^ b  =   1 0 1 0     a = a^b ( value of a will swap to 15)           because    a  =5   =  0 1 0 1                               b =10 =  1 0 1 0                         a ^ b  =   1 1 1 1   .i.e. 15   result a =15 and b=10 Java code    package com.company ; import

BITWISE OPERATOR

BITWISE OPERATOR package com.company ; public class Main { public static void main ( String [] args ) { //AND operator int x = 10 , y = 6 , z ; z = x & y ; //both bits should be 1 /* x y x&y 0 0 0 0 1 0 1 0 0 1 1 1 */ System . out . println ( "z=x&y : " + z ); //OR bitwise operator z = x | y ; //one of bits should be 1 /* x y x|y 0 0 0 0 1 1 1 0 1 1 1 1 */ System . out . println ( "z=x|y : " + z ); //XOR bitwise operator z = x ^ y ; //only one bits should be 1 /* x y x^y 0 0 0 0 1 0 1 0 0 1 1 0 */ System . out . println ( "z=x^y : " + z ); /*LEFT SHIFT operator * It will double the number by moving 1 place * Automati

Temperature Conversion System

Image
Temperature Conversion System   Temperature of city in Fahrenheit degrees is input through the keyboard. wr ite a program to convert this temperature into centigrade. package com.company ; import java.util.Scanner ; public class Main { public static void main ( String [] args ) { // write your code here float fahrenheit , centigrade , temp ; int choice ; System . out . println ( "Temperature conversion system " ); System . out . println ( "1. Fahrenheit to Centigrade" ); System . out . println ( "2. Centigrade to Fahrenheit" ); System . out . print ( "your choice : " ); Scanner sc = new Scanner ( System . in ); choice = sc . nextInt (); if ( choice == 1 ) { System . out . print ( "Fahrenheit : " ); fahrenheit = sc . nextFloat (); centigrade = ( float ) ( - 17.2222222 * fahrenheit ); System . out . printl

Java coding question.

12. If  the marks obtained by student in five subject are input through the keyboard , write a program to find out the aggregate marks and percentage marks obtained  by a student in each subject is 100. package com.company ; import java.util.Scanner ; public class Main { public static void main ( String [] args ) { // write your code here Scanner sc = new Scanner ( System . in ); int operating_System , DSA , DBMS , java , IOT , total ; float percentage ; System . out . println ( "Enter your marks " ); System . out . print ( "operating system : " ); operating_System = sc . nextInt (); System . out . print ( "DSA : " ); DSA = sc . nextInt (); System . out . print ( "DBMS : " ); DBMS = sc . nextInt (); System . out . print ( "Java : " ); java = sc . nextInt (); System . out . print ( "IOT : " ); IOT = sc . nextI

the distance between two cities (in km is input through the keyboard. write a program to convert and print this distance in meter ,feet inches and centimeter.

11 . The distance between two cities (in km is input through the keyboard. write a program to convert and print this distance in meter ,feet inches and centimeter. package com.company ; import java.util.Scanner ; public class Main { public static void main ( String [] args ) { // write your code here System . out . print ( "Enter distance between two cities : " ); Scanner sc = new Scanner ( System . in ); //sc is scanner name double distance , meter , feet , inches , centimetres ; //variable is declared distance = sc . nextDouble (); //taking input from user (in km) meter = distance * 1000 ; //1km = 1000m feet = distance * 3280.8399 ; //1km = 3280.8399; inches = distance * 39370.0787 ; //1km = 39370.0787; centimetres = distance * 100000 ; //1km = 100000; System . out . println ( distance + " kilometer = " + meter + " meter" ); System . out . println ( distance + " k

Check vowel or constant in java

10. Check vowel or constant in java package com.company ; import java.util.Scanner ; public class Main { public static void main ( String [] args ) { // write your code here Scanner sc = new Scanner ( System . in ); System . out . println ( "Enter alphabet to check alphabet is vowel or constant" ); char alphabet = sc . next (). charAt ( 0 ); if ( alphabet == 'a' || alphabet == 'e' || alphabet == 'i' || alphabet == 'o' || alphabet == 'u' ) { System . out . println ( alphabet + " is vowel" ); } else System . out . println ( alphabet + " is constant" ); } } output:- h is constant Process finished with exit code 0

convert int to char in java

 9. convert int to char in java package com.company ; import java.util.Scanner ; public class Main { public static void main ( String [] args ) { // write your code here int a ; Scanner sc = new Scanner ( System . in ); System . out . println ( "Enter a number : " ); a = sc . nextInt (); char b = ( char ) a ; /*there might be a loss of data while converting from int to car. so ,we are using (char) to specify that a should be converted to character*/ System . out . println ( b ); } } output:- Enter a number : 80 P Process finished with exit code 0

Number is even or odd in java

  8 . Number is even or odd in java package com.company ; import java.util.Scanner ; public class Main { public static void main ( String [] args ) { // write your code here int a ; Scanner sc = new Scanner ( System . in ); System . out . println ( "Find number is even or odd" ); System . out . print ( "Enter a number : " ); a = sc . nextInt (); if ( a % 2 == 0 ) { System . out . println ( "Even" ); } else System . out . println ( "Odd" ); } } output:- Find number is even or odd Enter a number : 23 Odd Process finished with exit code 0

Typecasting

  Typecasting Typecasting:- typecasting help to convert one data type to another data type //Typecasting package com.company ; public class Main { public static void main ( String [] args ) { // write your code here //int type variable int a = 33 ; System . out . println ( "int variable : " + a ); //convert int into double double c = a ; System . out . println ( "double variable is : " + c ); } } output:- int variable : 33 double variable is : 33.0 Process finished with exit code 0

find quotient and remainder in java

  find quotient and remainder //find quotient and remainder package com.company ; import java.util.Scanner ; public class Main { public static void main ( String [] args ) { int dividend , divisor , quotient , remainder ; Scanner ddqr = new Scanner( System . in ) ; System . out .print( "Enter dividend : " ) ; dividend = ddqr .nextInt() ; System . out .print( "Enter divisor : " ) ; divisor = ddqr .nextInt() ; quotient = dividend / divisor ; System . out .println( "Quotient is : " + quotient ) ; remainder = dividend % divisor ; System . out .println( "Remainder is : " + remainder ) ; } } output:- Enter dividend : 15 Enter divisor : 4 Quotient is : 3 Remainder is : 3 Process finished with exit code 0

Round Number to n Decimal Place

2. Round Number to n Decimal Place //here we used format() method to print the given decimal number to 3 decimal places //The format string %.3f specifies to print the number to 3 decimal places. //Round number to n decimal place package com.company ; import java.util.Stack ; public class Main { public static void main ( String [] args ) { //dicimal number double num = 5.873324 ; //print number to 3 decimal places System . out .format( "%.5f" , num ) ; } }

swapping of two number in java with algorithm.

Image
  swapping of two number.   algorithm for swapping of two number. first take two input from user as (a ,b) then create one temporary variable as (temp) and then store the value of a into temporary variable as (temp=a) so that value of a not change and then assign the value as (a=b; and b=temp) and lastly print the value of a and b   program in java    //swapping of two number package com.company ; import java.util.Scanner ; import java.io. * ; public class Main { public static void main ( String [] args ) { Scanner input = new Scanner ( System . in ); System . out . println ( "Enter two number " ); int a,b; int temp; // here we use temporary varaiable to store data System . out . println ( "first : " ); a = input. nextInt (); System . out . println ( "second : " ); b = input. nextInt (); temp = a; a = b; b = temp; System . out . println ( "After swappi

AREA AND PERIMETER OF CIRCLE

Image
     AREA AND PERIMETER OF CIRCLE //find area of circle and perimeter of circle. package com.company ; import java.util.Scanner ; import java.io. * ; public class Main { public static void main ( String [] args ) { float radius ; Scanner input = new Scanner ( System . in ); System . out . print ( "Enter radius of circle : " ); radius = input . nextFloat (); double area , perimeter ; area = Math . PI * ( radius * radius ); perimeter = 2 * Math . PI * radius ; System . out . println ( "Area of circle is : " + area ); System . out . println ( "Perimeter of circle is :" + perimeter ); } } Enter radius of circle : 12 12.0 Area of circle is : 452.3893421169302 Perimeter of circle is :75.39822368615503 Process finished with exit code 0 Sharaddha Nand Pandey Welcome You
  Write a Java program to display the following pattern. Sample Pattern : J a v v a J a a v v a a J J aaaaa V V aaaaa JJ a a V a a package com.company ; import java.util.Scanner ; public class Main { public static void main ( String [] args ) { // write your code here System . out . println ( " J A V V A" ); System . out . println ( " J A A V V A A" ); System . out . println ( "J J AAAAAA V V AAAAAAA" ); System . out . println ( " JJ A A V A A" ); } } J A V V A J A A V V A A J J AAAAAA V V AAAAAAA JJ A A V A A Process finished with exit code 0

Addition ,multiplication ,division ,subtraction of a number.

Addition ,multiplication ,division ,subtraction of a number. //Addition ,multiplication ,division ,subtraction of a number. package com.company ; import java.util.Scanner ; public class Main { public static void main ( String [] args ) { // write your code here float a , b , sum , sub , mul , div , mod ; Scanner input = new Scanner ( System . in ); System . out . println ( "Enter two number " ); System . out . print ( "fist number : " ); a = input . nextFloat (); System . out . print ( "second number : " ); b = input . nextFloat (); sum = a + b ; sub = a - b ; mul = a * b ; div = a / b ; mod = a % b ; System . out . println ( "Sum of two number is : " + sum ); System . out . println ( "Product of two number is : " + mul ); System . out . println ( "Difference of two number : " + sub ); System .