Basic Programs in Java
Basic Programs in Java
Basic programs
int x = 5;
int y = 6;
int sum = x + y;
int n1 = 4, n2 = 3, n3 = 2;
else
System.out.println(n3 + " is the largest number.");
}
}
// Main class
class Evenodd {
else {
If else in JAVA
public class IfElse {
int n = 10;
if (n > 5) {
System.out.println("The number is greater than 5.");
} else {
System.out.println("The number is 5 or less.");
}
}
Nested If in JAVA
public class NestedIf {
int a = 25;
double w = 65.5;
if (a >= 18) {
if (w >= 50.0) {
System.out.println("You are eligible to donate blood.");
} else {
System.out.println("You must weigh at least 50 kilograms to donate blood.");
}
} else {
System.out.println("You must be at least 18 years old to donate blood.");
}
}
}
}}
class Naturalnumberssum {
// main function
public static void main(String[] args)
{
int N = 10;
int sum = 0;
System.out.println("Finding Sum of " + N + " numbers using for loop");
if ((i & 1) == 0)
evenSum += i;
else
oddSum += i;
}
System.out.println("Sum of First " + n
+ " Even numbers = " + evenSum);
There are two ways to write the fibonacci series program in java:
1. class FibonacciExample1{
2. public static void main(String args[])
3. {
4. int n1=0,n2=1,n3,i,count=10;
5. System.out.print(n1+" "+n2);//printing 0 and 1
6.
7. for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already printed
8. {
9. n3=n1+n2;
10. System.out.print(" "+n3);
11. n1=n2;
12. n2=n3;
13. }
14.
15. }}
Test it Now
Output:
0 1 1 2 3 5 8 13 21 34
Note: 0 and 1 are not prime numbers. The 2 is the only even prime number because all the
other even numbers can be divided by 2.
Let's see the prime number program in java. In this java program, we will take a number
variable and check whether the number is prime or not.
14. }
15. }
16. if(flag==0) { System.out.println(n+" is prime number"); }
17. }//end of else
18. }
19. }
Test it Now
Output:
3 is prime number
1. class PalindromeExample{
2. public static void main(String args[]){
3. int r,sum=0,temp;
4. int n=454;//It is the number variable to be checked for palindrome
5.
6. temp=n;
7. while(n>0){
Or
1. import java.util.*;
2. class PalindromeExample2
3. {
4. public static void main(String args[])
5. {
6. String original, reverse = ""; // Objects of String class
7. Scanner in = new Scanner(System.in);
8. System.out.println("Enter a string/number to check if it is a palindrome");
9. original = in.nextLine();
10. int length = original.length();
11. for ( int i = length - 1; i >= 0; i-- )
12. reverse = reverse + original.charAt(i);
13. if (original.equals(reverse))
14. System.out.println("Entered string/number is a palindrome.");
15. else
16. System.out.println("Entered string/number isn't a palindrome.");
17. }
18. }
1. 4! = 4*3*2*1 = 24
2. 5! = 5*4*3*2*1 = 120
Here, 4! is pronounced as "4 factorial", it is also called "4 bang" or "4 shriek".
There are many ways to write the factorial program in java language. Let's see the 2 ways
to write the factorial program in java.
1. class FactorialExample{
2. public static void main(String args[]){
3. int i,fact=1;
4. int number=5;//It is the number to calculate factorial
5. for(i=1;i<=number;i++){
6. fact=fact*i;
7. }
8. System.out.println("Factorial of "+number+" is: "+fact);
9. }
10. }
Output:
Armstrong Number
An Armstrong number is a positive m-digit number that is equal to the sum of the
mth powers of their digits. It is also known as pluperfect, or Plus Perfect,
or Narcissistic number. It is an OEIS sequence A005188. Let’s understand it through an
example.
2: 21 = 2
3: 31 = 3
The first few Armstrong numbers between 0 to 999 are 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370,
371, 407. Some other Armstrong numbers are 1634, 8208, 9474, 54748, 92727, 93084,
548834, 1741725, 4210818, 9800817, 9926315, 24678050, 24678051, 88593477,
146511208, 472335975, 534494836, 912985153, 4679307774, 32164049650,
32164049651.
ArmstrongNumberExample1.java
1. import java.util.Scanner;
2. import java.lang.Math;
3. public class ArmstsrongNumberExample
4. {
5. //function to check if the number is Armstrong or not
6. static boolean isArmstrong(int n)
7. {
8. int temp, digits=0, last=0, sum=0;
9. //assigning n into a temp variable
10. temp=n;
11. //loop execute until the condition becomes false
12. while(temp>0)
13. {
14. temp = temp/10;
15. digits++;
16. }
17. temp = n;
18. while(temp>0)
19. {
20. //determines the last digit from the number
21. last = temp % 10;
22. //calculates the power of a number up to digit times and add the resultant to the sum v
ariable
23. sum += (Math.pow(last, digits));
24. //removes the last digit
25. temp = temp/10;
26. }
27. //compares the sum with n
28. if(n==sum)
29. //returns if sum and n are equal
30. return true;
31. //returns false if sum and n are not equal
32. else return false;
33. }
34. //driver code
35. public static void main(String args[])
36. {
37. int num;
38. Scanner sc= new Scanner(System.in);
39. System.out.print("Enter the limit: ");
40. //reads the limit from the user
41. num=sc.nextInt();
42. System.out.println("Armstrong Number up to "+ num + " are: ");
43. for(int i=0; i<=num; i++)
44. //function calling
45. if(isArmstrong(i))
46. //prints the armstrong numbers
47. System.out.print(i+ ", ");
48. }
49. }
Output: