Programming Notes
Programming Notes
Relational Operators
class RelOp
{
public static void main (String args[])
{
[Link](4<3);
[Link](4>3);
[Link](4<=3);
[Link](4>=3);
[Link](4==3);
[Link](4!=3);
}
}
Logical Operators
AND (&&)
Input 1 Input 2 Output
OR (||)
Input 1 Input 2 Output
NOT (!)
Input 1 Output
True false
False true
Ex
[Link](3>2 && 7<5);//false
[Link](3>2 || 7<5);//true
J. SIVA NAGESWARARAO|NIST|JAVA
2
[Link](!(5!=5));//true
Assignment Operators
It is used to assign the values from right side to left side.
Ex
int x=10;
int y=20;
x=y;
[Link](x);//20
[Link](y);//20
Conditional Operators
Syntax: Condition ? expression1:expression2;
Ex
int x=5;
int y=(x<19) ? 78:76;
[Link](y);//78
Write a program to check given number is even or not?
4 Even
7Odd
Sol 1
int x=7;
String y=((x%2)==0)?"Even":"Odd";
[Link](y);//odd
Sol 2
int x=4;
String y=((x%2)==0)?"Even":"Odd";
[Link](y);//Even
Increment/Decrement Operators
++a Pre-Increment --a Pre-Decrement
a++ post-increment a-- post-decrement
int x=4;
J. SIVA NAGESWARARAO|NIST|JAVA
3
++x;
[Link](x);//5
int x=4;
x++;
[Link](x);//5
int x=4;
x--;
[Link](x);//3
int x=4;
--x;
[Link](x);//3
int x=4;
[Link](++x);//5
int x=4;
[Link](x++);//4
int x=4;
int y=++x;
[Link](x);//5
[Link](y);//5
int x=4;
int y=--x;
[Link](x);//3
[Link](y);//3
int x=4;
int y=x++;
[Link](x);//5
[Link](y);//4
Scanner Programs
These are used to read the inputs during the execution of a program.
Syntax
import [Link];
class ClassName
{
public static void main(String args[])
{
scanner sc=new scanner([Link]);
}
}
Syntax to read inputs
boolean a=[Link]();
byte a =[Link]();
short a= [Link]();
J. SIVA NAGESWARARAO|NIST|JAVA
4
int a = [Link]();
long a = [Link]();
float a= [Link]();
double a= [Link]();
String a=[Link]();
Char a=[Link]().charAt(0);
Write a program to sum of given numbers.
import [Link];
class Example
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter First Number:");
int n1 = [Link]();
[Link]("Enter Second Number:");
int n2 = [Link]();
[Link]("the Sum is :"+(n1+n2));
}
}
Write a program to print area of a circle for the given radius.
import [Link];
class Example
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter Radius of Circle : ");
double radius = [Link]();
[Link]("Area of Circle is :"+3.14*radius*radius);
}
}
Write a program to print simple interest for the given principle, rate of
interest and time.
J. SIVA NAGESWARARAO|NIST|JAVA
5
import [Link];
class Example
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("====================================
========");
[Link](" Simple Interest Calculator" );
[Link]("====================================
========");
[Link]("Simple Principle : ");
double principle=[Link]();
[Link]("Rate of Interest(p.a) : ");
double rate=[Link]();
[Link]("Time Period : ");
double time=[Link]();
double solution=(principle*time*rate)/100;
[Link]("====================================
========");
[Link]("Principle Amount : "+principle);
[Link]("Total Interest : "+solution);
[Link]("Total Amount : "+
(principle+solution));
[Link]("====================================
========");
[Link]("Thank you...");
}
}
Write a program to print volume of a cylinder
J. SIVA NAGESWARARAO|NIST|JAVA
6
import [Link];
class Example
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Radius of Cylinder: ");
double radius=[Link]();
[Link]("Height of Cylinder : ");
double height=[Link]();
[Link]("Volume of Cylinder is :
"+3.14*radius*radius*height);
}
}
Control Statements
These are the pre-defined statements used to control the execution flow of a
program.
1. Decision making statements /conditional statements
a. If
b. If-else
c. If-else-if
d. Switch
2. Looping statements
a. For loop
b. While loop
c. Do-while loop
3. Jumping statements
a. Break
b. Continue
If-else
Syntax
if(condition)
{
----------
-------
}
else
{
J. SIVA NAGESWARARAO|NIST|JAVA
7
--------
-----
}
Example
import [Link];
class Example
{
public static void main(String[] args)
{
int a=5;
if(a<10)
{
[Link]("HI");
}
else
{
[Link]("Hello");
}
}
}
import [Link];
class Example
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a Number:: ");
int number=[Link]();
if (number%2==0)
{
[Link]("It is even number");
}
else
J. SIVA NAGESWARARAO|NIST|JAVA
8
{
[Link]("It is Odd Number");
}
}
}
Write a program to check is a person is eligible for voting or not
import [Link];
class VotingEligibility
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter your age : ");
int age=[Link]();
if(age<18)
{
[Link]("You are not eligible for voting");
}
else
{
[Link]("You are eligible for voting");
}
}
}
Write a program to check is eligible to write a IAS exam or
note(age>=21&&age<=32)
import [Link];
class RelOp
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter your age : ");
int age=[Link]();
J. SIVA NAGESWARARAO|NIST|JAVA
9
if(age>=21&&age<=32)
{
[Link]("Eligible for Write an IAS Exam");
}
else
{
[Link]("Not Eligible For Write an IAS Exam");
}
}
}
Write a program to check final bill. If the bill is more than or equals to
5000, give a discount of 18% else 8%
import [Link];
class FinalBill
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter your age : ");
int bill=[Link]();
if(bill>=5000)
{
[Link]("You got 18% of discount on the bill");
[Link]("Pay :: "+0.82*bill);
}
else
{
[Link]("You got 8% of discount on the bill");
[Link]("Pay :: "+0.92*bill);
}
}
}
If-eles-if
If(condition_1)
J. SIVA NAGESWARARAO|NIST|JAVA
10
}
Else if(condition_2)
{
}
Else
{
}
Ex: write a program to check the given number is
positive/negative/neutral
import [Link];
class CheckNumberStatus
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the Number :: ");
int number=[Link]();
if(number>0)
{
[Link]("Positive Number!!!");
}
else if(number<0)
{
[Link]("Negative Number!!!");
}
else
{
[Link]("Neutral Number!!!");
}
}
}
Write a program to print simple calculator which performs +,-,*,/
J. SIVA NAGESWARARAO|NIST|JAVA
11
import [Link];
class SimpleCalculator
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("====================================
====================================");
[Link](" Simple Calculator
");
[Link]("====================================
====================================");
[Link]("Enter First Number :: ");
int firstnumber=[Link]();
[Link]("Enter Second Number :: ");
int secondnumber=[Link]();
[Link]("Enter the Arithmetic Operator for performing
operation::");
char operator=[Link]().charAt(0);
if(operator=='+')
{
[Link]("Sum :: "+(firstnumber+secondnumber));
}
else if(operator=='-')
{
[Link]("Sum :: "+(firstnumber-secondnumber));
}
else if(operator=='*')
{
[Link]("Sum :: "+(firstnumber*secondnumber));
}
else if(operator=='/')
{
J. SIVA NAGESWARARAO|NIST|JAVA
12
[Link]("Sum :: "+(firstnumber/secondnumber));
}
else if(operator=='%')
{
[Link]("Sum :: "+(firstnumber%secondnumber));
}
else
{
[Link]("Please Give an operator from +,-,*,/,%
only!!!!!!!");
}
}
}
Switch
Syntax
switch (Example)
{
case case_value:Operation 1;
case case_value:Operation 2;
}
import [Link];
class SpellIt
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Please enter the value to know the spell of that
Number :: (1-10 only)");
int input=[Link]();
switch (input)
{
case 1:[Link]("One");break;
case 2:[Link]("Two");break;
case 3:
J. SIVA NAGESWARARAO|NIST|JAVA
13
[Link]("Three");break;
case 4:
[Link]("Four");break;
case 5:
[Link]("Five");break;
case 6:
[Link]("Six");break;
case 7:
[Link]("Seven");break;
case 8:
[Link]("Eight");break;
case 9:
[Link]("Nine");break;
case 10:
[Link]("Ten");break;
default:
[Link]("Please enter the input from 1 to 10
only");
}
}
}
Imp Points on Switch:
1. There can be any number of cases in a switch
2. There should be only one default case in a switch
3. Case values can be integers, chars, and strings but not decimal numbers
and Boolean values.
4. All the case values must be of same datatype.
5. No duplicate case values.
Wite a program to print tha grade value of according their grade.
class Example
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Please Entet The Grade:: ");
char grade=[Link]().charAt(0);
J. SIVA NAGESWARARAO|NIST|JAVA
14
switch (grade)
{
case 'A':case 'a':
[Link]("You got First Rank");
break;
case 'B':case 'b':
[Link]("You got Second Rank");
break;
case 'C':case 'c':
[Link]("You got First Class");
break;
case 'D':case 'd':
[Link]("You got Second Class");
break;
case 'E':case 'e':
[Link]("You got just pass");
break;
case 'F': case 'f':
[Link]("You got fired in the exam");
break;
default:
[Link]("Please enter the grade A-F only..");
}
}
}
Loops
Loop is a pre-defined statement used to execute same set of lines repeatedly.
Syntax
for (variable_declaration & inilization; condition; expression)
{
}
import [Link];
class Example
J. SIVA NAGESWARARAO|NIST|JAVA
15
{
public static void main(String[] args)
{
for (int i=1;i<=5 ;i++ )
{
[Link]("Siva Nageswararao Jeedimalla");
}
}
}
Write a program to print from 1-10 numbers.
(((((((((((((((((((((Adukko)))))))))))))))))))))))
import [Link];
class Example
{
public static void main(String[] args)
{
for (int i=1;i<=10 ;i++ )
{
[Link](i);
}
}
}
Write a program to print sum of the numbers from 1 to 10.
import [Link];
class RelOp
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
int sum=0;
[Link]("Enter the Lower Range::");
int low=[Link]();
[Link]("Enter the High Range ::");
int high=[Link]();
J. SIVA NAGESWARARAO|NIST|JAVA
16
J. SIVA NAGESWARARAO|NIST|JAVA
17
J. SIVA NAGESWARARAO|NIST|JAVA
18
import [Link];
class RelOp
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
int count=0;
[Link]("Enter the Range::");
int number=[Link]();
for (int i=1;i<=number;i++)
{
if (number%i==0)
{
count=count+1;
}
}
[Link]("Number of Factors ::"+count);
}
}
Write a program to check prime number or not.
import [Link];
class RelOp
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
int count=0;
[Link]("Enter the Number::");
int number=[Link]();
for (int i=1;i<=number ;i++ )
{
if(number%i==0)
{
J. SIVA NAGESWARARAO|NIST|JAVA
19
count++;
}
}
if(count==2)
{
[Link]("Prime Number!!");
}
else
{
[Link]("Not Prime Number!!");
}
}
}
Write a program to print all the prime numbers present from 1-100.
import [Link];
class RelOp
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the range::");
int high=[Link]();
for (int i=1;i<=high ;i++ )
{
int count=0;
for (int j=1;j<=i ;j++ )
{
if (i%j==0)
{
count++;
}
}
if (count==2)
J. SIVA NAGESWARARAO|NIST|JAVA
20
{
[Link](i);
}
}
}
}
Write a program to check prime number or not.
import [Link];
class RelOp
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number to check prime or not");
int number=[Link]();
int flag=0;
for (int i=2;i<number ;i++ )
{
if(number%i==0)
{
flag++;
break;
}
}
if (flag==0)
{
[Link]("Prime Number");
}
else
{
[Link]("Not Prime Number");
}
}
J. SIVA NAGESWARARAO|NIST|JAVA
21
While Loop
Syntax
Variable declaration & Initialization;
While(condition)
{
Operations;
Expression;
}
Write a program to print my name n times
import [Link];
class RelOp
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter Low value::");
int low=[Link]();
[Link]("Enter High value::");
int high=[Link]();
while (low<=high)
{
[Link]("Hi, Siva..!");
low++;
}
}
}
Write a program to count number of digits in given number.
import [Link];
class RelOp
{
public static void main(String[] args)
{
J. SIVA NAGESWARARAO|NIST|JAVA
22
J. SIVA NAGESWARARAO|NIST|JAVA
23
class RelOp
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the Input value:: ");
int input=[Link]();
int product=1;
while (input!=0)
{
int LastDigit=input%10;
product=product*LastDigit;
input=input/10;
}
[Link]("the product of digit:: "+product);
}
}
Write a program to check given number is spy number or not
import [Link];
class RelOp
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the input value:: ");
int input=[Link]();
int sum=0;
int product=1;
while (input!=0)
{
int LastDigit=input%10;
sum=sum+LastDigit;
product=product*LastDigit;
J. SIVA NAGESWARARAO|NIST|JAVA
24
input=input/10;
}
if (sum==product)
{
[Link]("SPY NUMBER");
}
else
{
[Link]("Not SPY NUMBER");
}
}
}
Write a program to check the given number is Duc k number or not.
import [Link];
class DuckNumber
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
int flag=0;
[Link]("Enter a Number to Know the Duck number or
not:: ");
int number=[Link]();
int RealValue=number;
while (number!=0)
{
if (number%10==0)
{
flag++;
}
number=number/10;
}
if(flag==0)
{
J. SIVA NAGESWARARAO|NIST|JAVA
25
J. SIVA NAGESWARARAO|NIST|JAVA
26
J. SIVA NAGESWARARAO|NIST|JAVA
27
{
[Link](reverse);
}
}
}
}
Write a program to check given number is Strong number or not.
import [Link];
class StrongNumber
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a Number::");
int number=[Link]();
int sum=0;
int TempValue=number;
while (TempValue!=0)
{
int LastDigit=TempValue%10;
int factorial=1;
for (int i=1;i<=LastDigit ;i++ )
{
factorial=factorial*i;
}
sum=sum+factorial;
TempValue=TempValue/10;
}
if(number==sum)
{
[Link]("The given number is Strong number");
}
else
J. SIVA NAGESWARARAO|NIST|JAVA
28
{
[Link]("The given number is not Strong
Number");
}
}
}
Write a program to print all the Strong Numbers.
import [Link];
class StrongNumbers
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the Range::");
int range=[Link]();
for (int j=1;j<=range ;j++ )
{
int sum=0;
int TempValue=j;
while (TempValue!=0)
{
int LastDigit=TempValue%10;
int factorial=1;
for (int i=1;i<=LastDigit ;i++ )
{
factorial=factorial*i;
}
sum=sum+factorial;
TempValue=TempValue/10;
}
if(j==sum)
{
[Link](j);
}
J. SIVA NAGESWARARAO|NIST|JAVA
29
}
}
}
Write a program to check Armstrong number or not.
import [Link];
class ArmStrong
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the Number::");
int number=[Link]();;
int count=0;
int TempValue1=number;
while (TempValue1!=0)
{
count++;
TempValue1=TempValue1/10;
}
int sum=0;
int TempValue2=number;
while (TempValue2!=0)
{
int product=1;
int LastDigit=TempValue2%10;
for (int i=1;i<=count ;i++ )
{
product=product*LastDigit;
}
sum=sum+product;
TempValue2=TempValue2/10;
}
if(sum==number)
J. SIVA NAGESWARARAO|NIST|JAVA
30
{
[Link]("The given number is Armstrong
Number");
}
else
{
[Link]("The Given number is Not Armstrong
Number");
}
}
}
Write a program to print LCF of given two numbers.
import [Link];
class LCF
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a first Number::");
int first=[Link]();
[Link]("Enter a second number::");
int second=[Link]();
for (int i=1;true ;i++ )
{
if ((first*i)%second==0)
{
[Link]("The LCM is "+(first*i));
break;
}
}
}
}
Write a program to print HCM.
import [Link];
J. SIVA NAGESWARARAO|NIST|JAVA
31
class HCM
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a First Number::");
int first=[Link]();
[Link]("Enter a Second Number::");
int second=[Link]();
int hcf=0;
for (int i=1;i<=first ;i++ )
{
if(first%i==0 && second%i==0)
{
hcf=i;
}
}
[Link]("The HCF is "+hcf);
}
}
Write a program to print first 10 terms Fibonacci series.
import [Link];
class Fibonacci
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter The range:: ");
int range=[Link]();
int first=0;
int second=1;
int sum=0;
J. SIVA NAGESWARARAO|NIST|JAVA
32
[Link]();
[Link]("-----------Fibonacci Series-----------");
for (int i=1;i<=range ;i++ )
{
[Link](sum +" ");
first=second;
second=sum;
sum=first+second;
}
}
}
Write a program to print largest digit in given number.
import [Link];
class LargestNumber
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a Number::");
int number=[Link]();
int max=0;
while(number!=0)
{
int LastDigit=number%10;
if(LastDigit>max)
{
max=LastDigit;
}
number=number/10;
}
[Link]("the Largest Digit is "+max);
}
}
J. SIVA NAGESWARARAO|NIST|JAVA
33
J. SIVA NAGESWARARAO|NIST|JAVA
34
{
for (int j=1;j<=side ;j++ )
{
[Link](" *");
}
[Link]();
}
}
}
Op:
Enter the side value::
5
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Rectangle Pattern
import [Link];
class RectPattern
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the length::");
int length=[Link]();
[Link]("Enter breadth::");
int breadth=[Link]();
for (int i=1;i<=length ;i++ )
{
for (int j=1;j<=breadth ;j++ )
{
[Link](" *");
J. SIVA NAGESWARARAO|NIST|JAVA
35
}
[Link]();
}
}
}
Op
Enter the length::
5
Enter breadth::
10
**********
**********
**********
**********
**********
import [Link];
class PutPlace
{
public static void main(String[] args)
{
for (int i=1;i<=5 ;i++ )
{
for (int j=1;j<=5 ;j++ )
{
if (i==4 && j==4)
{
[Link](" @ ");
}
else
{
[Link](" * ");
}
}
J. SIVA NAGESWARARAO|NIST|JAVA
36
[Link]();
}
}
}
Op
* * * * *
* * * * *
* * * * *
* * * @ *
* * * * *
import [Link];
class SeqNumPattern
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the no. of Rows::");
int row=[Link]();
[Link]("Enter the no. of Column::");
int column=[Link]();
for (int i=1;i<=row ;i++ )
{
for (int j=1;j<=column ;j++ )
{
[Link](" "+i+" ");
}
[Link]();
[Link]();
}
}
}
Op
Enter the no. of Rows::
J. SIVA NAGESWARARAO|NIST|JAVA
37
5
Enter the no. of Column::
5
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
import [Link];
class Pattern
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the no. of Rows::");
int row=[Link]();
[Link]("Enter the no. of Columns::");
int column=[Link]();
for (int i=1;i<=row ;i++ )
{
for (int j=1;j<=column ;j++ )
{
[Link](" "+j+" ");
}
[Link]();
}
}
}
J. SIVA NAGESWARARAO|NIST|JAVA
38
Op
Enter the no. of Rows::
5
Enter the no. of Columns::
5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
J. SIVA NAGESWARARAO|NIST|JAVA
39
import [Link];
class WithArgumentWithReturnType
{
public static int reverse(int number)
{
int rev=0;
while (number!=0)
{
int LastDigit=number%10;
rev=rev*10+LastDigit;
number=number/10;
}
return rev;
}
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter The number:::");
int num=[Link]();
[Link]("The reverse number is ::"+reverse(num));
}
}
Write a method to return true if the number is prime otherwise false.
import [Link];
class PrimeMethod
{
public static boolean isPrime(int number)
{
int count=0;
for (int i=1;i<=number ;i++ )
{
if(number%i==0)
J. SIVA NAGESWARARAO|NIST|JAVA
40
{
count++;
}
}
if (count==2)
{
return true;
}
else
{
return false;
}
}
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a Number");
int num=[Link]();
[Link](isPrime(num));
}
}
Write program to print even numbers from 1 to 10;
import [Link];
class Even
{
public static boolean isEven(int number)
{
return number%2==0;
}
public static void seriesEven()
{
Scanner sc=new Scanner([Link]);
J. SIVA NAGESWARARAO|NIST|JAVA
41
J. SIVA NAGESWARARAO|NIST|JAVA
42
}
return real==rev;
}
public static void seriesPalindrome()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the Range::");
int range=[Link]();
for (int i=1;i<=range ;i++ )
{
if (isPalindrome(i))
{
[Link](i);
}
}
}
public static void main(String[] args)
{
seriesPalindrome();
}
}
Prime
import [Link];
class Prime
{
public static boolean isPrime(int number)
{
int count=0;
for (int i=1;i<=number ;i++ )
{
if (number%i==0)
{
J. SIVA NAGESWARARAO|NIST|JAVA
43
count++;
}
}
return count==2;
}
public static void seriesPrime()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the Range::");
int range=[Link]();
for (int i=1;i<=range ;i++ )
{
if (isPrime(i))
{
[Link](i);
}
}
}
public static void main(String[] args)
{
seriesPrime();
}
}
J. SIVA NAGESWARARAO|NIST|JAVA