Posts

Showing posts from June, 2018

WAP in Java To Check Whether a number is Duck No.or not.

A Duck number is a number which has zeroes present in it,  but there should be no zero present in the beginning of the number. For example 3210, 7056, 8430709 are all duck numbers whereas 08237, 04309 are not. class duck { public static void main(String args[]) {     int n=50603;  int rem=0,p,x=0; while(n>0) { rem=n%10; if(rem==0) { x++; } else { p=rem; } n=n/10; } if(x>0) System.out.println("duck number "); else System.out.println("not duck number "); } }

WAP IN JAVA TO DECIDE WHETHER THE GIVEN NUMBER IS PRIME OR NOT

import java.util.*; class loop { public static void main(String args[]) { int n,c=0; Scanner ob=new Scanner(System.in); System.out.println("enter the no:"); n=ob.nextInt(); for(int i=1;i<=n;i++) { if(n%i==0) c++; } if(c==2) System.out.println("prime"); else System.out.println("Not prime"); } }

Basic program for loop

class loop { public static void main(String args[]) { for(int i=1;i<=10;i++) { System.out.println(i); } } }

A number is said to be Buzz Number if it ends with 7 or is divisible by 7. Example- 1007 is a Buzz Number as it end with 7.343 is also a Buzz Number as it is divisible by 7 and 77777 is also a Buzz Number as it ends with 7 and also it is divisible by 7.

import java.util.*; class if_else11{ public static void main(String args[]) { int n; Scanner ob=new Scanner(System.in); System.out.println("enter the no:"); n=ob.nextInt(); if(n%10==7 || n%100==7 || n%1000==7 && n%7==0) System.out.println("Buzz number"); else System.out.println("not Buzz number"); } }

WAP in Java to decide wether a number is special two digit no.or not

Write a  program in java  to accept a  number  and check whether the  number  is a special two digit  or not. A  special two - digit number  is such that when the sum of the  digits  is added to the product of its  digits , the result is equal to the original  two - digit number . import java.util.*; class if_else9{ public static void main(String args[]) { int n; Scanner ob=new Scanner(System.in); System.out.println("enter the number:"); n=ob.nextInt(); if((((n/10)+(n%10))+((n/10)*(n%10)))==n) System.out.println("special two digit"); else System.out.println("NOt spacial two digit"); } }

WAP in JAVA to decide whether a year is leap year or not.

import java.util.*; class leapyear{ public static void main(String args[]) { int y; Scanner ob=new Scanner(System.in); System.out.println("enter the year"); y=ob.nextInt(); if((y%4==0 && y%100!=0)|| y%400==0) System.out.println("leap year");  else  System.out.println("not leap year"); }

Few more Operators in Java

Prefix   :When increment or decrement operators are applied before the operant ,it is known as prefix operators. Postfix   : When increment or decrement operators are applied after the operant ,it is known as prefix operators. Increment Operator   :Increment operator increases the value of operand by one. Decrement Operator   : Decrement operator decreases the value of operand by one. Erythematic Operator   :An Operator which performs arithmetic functions is known as Erythematic Operator. Logical Operator   :These Operator yield results in 0 and 1 depending upon the outcome of the result. They check between two variables or values. Relational Operators   : Operators which are used to show or check relationship between operands are known as relational operators. Precedence of operators   : Specific  order in which the operators in an expression are evaluated when the expression has several . Bitwise Operator   : The Bitwise operators calculate each bit of their result

Operator and their Types in Java

Operator   :A symbol or a token which performs arthematicla or logical operations and yield a result. Operand   : An operator acts on different data items/entities called operands. Statement   : An expression which is assigned to a variable is completely known as statement. Unary Operator   :An arithmetical operator which is applied with a single operand is known as Unary operator. Binary Operator   :An arithmetic operator which deals with two operands, is known as Binary Operator. Ternary Operator  (or) Conditional Assignment Operator : :Ternary operator deals with three operands. The value assigned to a variable depends upon a logical expression.

Type Conversion In Java Programming

Type Conversion   : The process of converting one predefined type into another is called Type Conversion. Type Casting :  The explicit conversion of an operand to a specific type is called type casting. The operator that converts its operand to a specified type is called the typecast operator. Explicit Conversion :  an explicit type conversion is user defined that forces an expression to be of specific type. Coercion :  A implicit type conversion is a conversion performed by the compiler. The Java compiler converts all operands up to the type of the largest operand. This is also known as type promotion.

JVM Process During Complilation

Compiler   :A software which converts high level language to machine level language. It complies the instructions at once and lists all the errors. Interpreter   : A software which converts High level language to machine level language line by line .It does not move to next line until the error is corrected. Source Code   : A set of instructions written in high level language which is taken as an input from the user . Machine code   : The source code which is Complied/Interpreted is known as Machine code . Byte Code   : Byte code is the simplest Form of Source code. Source code is compiled to an intermediate code called Byte code. It can easily pass from one computer to another. Object Code   :The conversion of high-level language to a machine code with help of a translator known as Object code. Java Virtual Machine (JVM)   :It is a virtual processor which processes byte code to machine code for various platforms. So ,Java Interpreter is known as JVM. Compilation   :The

Write Java program to allow the user to input his/her age. Then the program will show if the person is eligible to vote. A person who is eligible to vote must be older than or equal to 18 years old.

import java.util.*;  class Vote { public static void main(String args[]) { int age; Scanner sc=new Scanner(System.in); System.out.print("What is your age?"); age=sc.nextInt(); if(age>=18) System.out.println("You are eligible to vote."); else System.out.println("You are not eligible to vote."); } } Output:What is your age? 18 You are eligible to vote.

A school has following rules for grading system: a. Below 25 - F b. 25 to 45 - E c. 45 to 50 - D d. 50 to 60 - C e. 60 to 80 - B f. Above 80 - A Ask user to enter marks and print the corresponding grade.

A school has following rules for grading system: a. Below 25 - F b. 25 to 45 - E c. 45 to 50 - D d. 50 to 60 - C e. 60 to 80 - B f. Above 80 - A Ask user to enter marks and print the corresponding grade. import java.util.Scanner ; class Ans { public static void main ( String [] args ){ Scanner s = new Scanner ( System . in ); System . out . println ( "Enter your marks" ); int x = s . nextInt (); if ( x < 25 ){ System . out . println ( "F" ); } else if (( x >= 25 )&&( x < 45 )){ System . out . println ( "E" ); } else if (( x >= 45 )&&( x < 50 )){ System . out . println ( "D" ); } else if (( x >= 50 )&&( x < 60 )){ System . out . println ( "C" ); } else if (( x >= 60 )&&( x < 80 )){ System . out . println ( "B" ); } else if (( x >= 80

WAP in Java and take values of length and breadth of a rectangle from user and check if it is square or not.

import java.util.Scanner ; class Ans { public static void main ( String [] args ){ Scanner s = new Scanner ( System . in ); System . out . println ( "Enter length" ); int x = s . nextInt (); System . out . println ( "Enter breadth" ); int y = s . nextInt (); if ( x == y ){ System . out . println ( "Square" ); } else { System . out . println ( "Rectangle" ); } } }

NETBEANS PROCESS OF WRITING THE CODE

WAP in JAVA to calculate factorial

Image
Q > So , basically what is factorial ? Def :  A factorial is a function that multiplies  number by every number. For example 4!= 4*3*2*1=24. The function is used, among other things, to find the number of ways “n” objects can be arranged. In mathematics, there are n! ( Factorial ways to arrange N Objects ) in sequence. Q > How to calculate ? For example : Consider : 2! = 2 x 1 = 2 The possibility of 2! is two ways like {2,1}, { 1,2 }. Same as like : 4! = 4 x 3 x 2 x 1 = 24. 24 = the arrangement of 4! is {1,2,3,4}, {2,1,3,4}, {2,3,1,4}, {2,3,4,1}, {1,3,2,4}, etc. Same as like 5! , 10! , N!. class Factoral { public static void main ( String arg [ ] ) {              int n = 5 , fact = 1 ;          for ( int i = 1 ; i <= n ; i ++ )       {         fact = fact * i ;    }          System . out . println ( "factoral=" + fact ) ; } } output: factorial = 120

Java Program To Calculate Area Of Triangle

import java . util . Scanner ; class AreaOfTriangle {    public static void main ( String args [ ] )      {                  Scanner s = new Scanner ( System . in ) ;                   System . out . println ( "Enter the width of the Triangle:" ) ;          double b = s . nextDouble ( ) ;          System . out . println ( "Enter the height of the Triangle:" ) ;            double h = s . nextDouble ( ) ;                    //Area = (width*height)/2        double area = ( b * h ) / 2 ;        System . out . println ( "Area of Triangle is: " + area ) ;           } } Enter the width of the Triangle : 10 Enter the height of the Triangle : 20 Area of Triangle is : 100.0