Nothing Special   »   [go: up one dir, main page]

Example 1: Display Fibonacci Series Using For Loop

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Example 1: Display Fibonacci series using for loop

1. public class Fibonacci {


2.
3. public static void main(String[] args) {
4.
5. int n = 10, t1 = 0, t2 = 1;
6. System.out.print("First " + n + " terms: ");
7.
8. for (int i = 1; i <= n; ++i)
9. {
10. System.out.print(t1 + " + ");
11.
12. int sum = t1 + t2;
13. t1 = t2;
14. t2 = sum;
15. }
16. }
17. }

Example 2: Display Fibonacci series using while loop


1. public class Fibonacci {
2.
3. public static void main(String[] args) {
4.
5. int i = 1, n = 10, t1 = 0, t2 = 1;
6. System.out.print("First " + n + " terms: ");
7.
8. while (i <= n)
9. {
10. System.out.print(t1 + " + ");
11.
12. int sum = t1 + t2;
13. t1 = t2;
14. t2 = sum;
15.
16. i++;
17. }
18. }
19. }

Example 3: Display Fibonacci series upto a given number


(instead of terms)
1. public class Fibonacci {
2.
3. public static void main(String[] args) {
4.
5. int n = 100, t1 = 0, t2 = 1;
6.
7. System.out.print("Upto " + n + ": ");
8. while (t1 <= n)
9. {
10. System.out.print(t1 + " + ");
11.
12. int sum = t1 + t2;
13. t1 = t2;
14. t2 = sum;
15. }
16. }
17. }
Example 1: Display Uppercased A to Z using for loop
1. public class Characters {
2.
3. public static void main(String[] args) {
4.
5. char c;
6.
7. for(c = 'A'; c <= 'Z'; ++c)
8. System.out.print(c + " ");
9. }
10. }

Example 2: Display Lowercased a to z using for loop


1. public class Characters {
2.
3. public static void main(String[] args) {
4.
5. char c;
6.
7. for(c = 'a'; c <= 'z'; ++c)
8. System.out.print(c + " ");
9. }
10. }

Example 1: Generate Multiplication Table using for loop


1. public class MultiplicationTable {
2.
3. public static void main(String[] args) {
4.
5. int num = 5;
6. for(int i = 1; i <= 10; ++i)
7. {
8. System.out.printf("%d * %d = %d \n", num, i, num * i);
9. }
10. }
11. }

Example 2: Generate Multiplication Table using while loop


1. public class MultiplicationTable {
2.
3. public static void main(String[] args) {
4.
5. int num = 9, i = 1;
6. while(i <= 10)
7. {
8. System.out.printf("%d * %d = %d \n", num, i, num * i);
9. i++;
10. }
11. }
12. }

Example: Java Program to Check a Leap Year


1. public class LeapYear {
2.
3. public static void main(String[] args) {
4.
5. int year = 1900;
6. boolean leap = false;
7.
8. if(year % 4 == 0)
9. {
10. if( year % 100 == 0)
11. {
12. // year is divisible by 400, hence the year is a leap year
13. if ( year % 400 == 0)
14. leap = true;
15. else
16. leap = false;
17. }
18. else
19. leap = true;
20. }
21. else
22. leap = false;
23.
24. if(leap)
25. System.out.println(year + " is a leap year.");
26. else
27. System.out.println(year + " is not a leap year.");
28. }
29. }

Example: Check if a Number is Positive or Negative using if else


1. public class PositiveNegative {
2.
3. public static void main(String[] args) {
4.
5. double number = 12.3;
6.
7. // true if number is less than 0
8. if (number < 0.0)
9. System.out.println(number + " is a negative number.");
10.
11. // true if number is greater than 0
12. else if ( number > 0.0)
13. System.out.println(number + " is a positive number.");
14.
15. // if both test expression is evaluated to false
16. else
17. System.out.println(number + " is 0.");
18. }
19. }

Example: Java Program to Check Alphabet using if else


1. public class Alphabet {
2.
3. public static void main(String[] args) {
4.
5. char c = '*';
6.
7. if( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
8. System.out.println(c + " is an alphabet.");
9. else
10. System.out.println(c + " is not an alphabet.");
11. }
12. }
Example 1: Sum of Natural Numbers using for loop
1. public class SumNatural {
2.
3. public static void main(String[] args) {
4.
5. int num = 100, sum = 0;
6.
7. for(int i = 1; i <= num; ++i)
8. {
9. // sum = sum + i;
10. sum += i;
11. }
12.
13. System.out.println("Sum = " + sum);
14. }
15. }

Example 2: Sum of Natural Numbers using while loop


1. public class SumNatural {
2. public static void main(String[] args) {
3. int num = 50, i = 1, sum = 0;
4.
5. while(i <= num)
6. {
7. sum += i;
8. i++;
9. }
10. System.out.println("Sum = " + sum);
11. }
12. }

Example 1: Find Factorial of a number using for loop


1. public class Factorial {
2. public static void main(String[] args) {
3. int num = 10;
4. long factorial = 1;
5. for(int i = 1; i <= num; ++i)
6. {
7. // factorial = factorial * i;
8. factorial *= i;
9. }
10. System.out.printf("Factorial of %d = %d", num, factorial);
11. }
12. }

Example 2: Find Factorial of a number using BigInteger


1. import java.math.BigInteger;
2.
3. public class Factorial {
4.
5. public static void main(String[] args) {
6.
7. int num = 30;
8. BigInteger factorial = BigInteger.ONE;
9. for(int i = 1; i <= num; ++i)
10. {
11. // factorial = factorial * i;
12. factorial = factorial.multiply(BigInteger.valueOf(i));
13. }
14. System.out.printf("Factorial of %d = %d", num, factorial);
15. }
16. }
Example 3: Find Factorial of a number using while loop
1. public class Factorial {
2.
3. public static void main(String[] args) {
4.
5. int num = 5, i = 1;
6. long factorial = 1;
7. while(i <= num)
8. {
9. factorial *= i;
10. i++;
11. }
12. System.out.printf("Factorial of %d = %d", num, factorial);
13. }
14. }

Example 1: Program to Check Prime Number using a for loop


1. public class Prime {
2. public static void main(String[] args) {
3.
4. int num = 29;
5. boolean flag = false;
6. for(int i = 2; i <= num/2; ++i)
7. {
8. // condition for nonprime number
9. if(num % i == 0)
10. {
11. flag = true;
12. break;
13. }
14. }
15.
16. if (!flag)
17. System.out.println(num + " is a prime number.");
18. else
19. System.out.println(num + " is not a prime number.");
20. }
21. }

Example 2: Program to Check Prime Number using a while loop


1. public class Prime {
2. public static void main(String[] args) {
3.
4. int num = 33, i = 2;
5. boolean flag = false;
6. while(i <= num/2)
7. {
8. // condition for nonprime number
9. if(num % i == 0)
10. {
11. flag = true;
12. break;
13. }
14.
15. ++i;
16. }
17. if (!flag)
18. System.out.println(num + " is a prime number.");
19. else
20. System.out.println(num + " is not a prime number.");
21. }
22. }
Example 1: Check Armstrong Number for 3 digit number
1. public class Armstrong {
2. public static void main(String[] args) {
3.
4. int number = 371, originalNumber, remainder, result = 0;
5.
6. originalNumber = number;
7.
8. while (originalNumber != 0)
9. {
10. remainder = originalNumber % 10;
11. result += Math.pow(remainder, 3);
12. originalNumber /= 10;
13. }
14.
15. if(result == number)
16. System.out.println(number + " is an Armstrong number.");
17. else
18. System.out.println(number + " is not an Armstrong number.");
19. }
20. }

Example 2: Check Armstrong number for n digits


1. public class Armstrong {
2. public static void main(String[] args) {
3.
4. int number = 1634, originalNumber, remainder, result = 0, n = 0;
5.
6. originalNumber = number;
7.
8. for (;originalNumber != 0; originalNumber /= 10, ++n);
9.
10. originalNumber = number;
11.
12. for (;originalNumber != 0; originalNumber /= 10)
13. {
14. remainder = originalNumber % 10;
15. result += Math.pow(remainder, n);
16. }
17. if(result == number)
18. System.out.println(number + " is an Armstrong number.");
19. else
20. System.out.println(number + " is not an Armstrong number.");
21. }
22. }

Example 1: Count Number of Digits in an Integer using while loop


1. public class NumberDigits {
2. public static void main(String[] args) {
3.
4. int count = 0, num = 3452;
5.
6. while(num != 0)
7. {
8. // num = num/10
9. num /= 10;
10. ++count;
11. }
12.
13. System.out.println("Number of digits: " + count);
14. }
15. }
Example 2: Count Number of Digits in an Integer using for loop
1. public class NumberDigits {
2.
3. public static void main(String[] args) {
4.
5. int count = 0, num = 123456;
6.
7. for(; num != 0; num/=10, ++count) {
8. }
9.
10. System.out.println("Number of digits: " + count);
11. }
12. }

Example: Program to count vowels, consonants, digits and spaces


1. public class Count {
2.
3. public static void main(String[] args) {
4. String line = "This website is aw3som3.";
5. int vowels = 0, consonants = 0, digits = 0, spaces = 0;
6.
7. line = line.toLowerCase();
8. for(int i = 0; i < line.length(); ++i)
9. {
10. char ch = line.charAt(i);
11. if(ch == 'a' || ch == 'e' || ch == 'i'
12. || ch == 'o' || ch == 'u') {
13. ++vowels;
14. }
15. else if((ch >= 'a'&& ch <= 'z')) {
16. ++consonants;
17. }
18. else if( ch >= '0' && ch <= '9')
19. {
20. ++digits;
21. }
22. else if (ch ==' ')
23. {
24. ++spaces;
25. }
26. }
27.
28. System.out.println("Vowels: " + vowels);
29. System.out.println("Consonants: " + consonants);
30. System.out.println("Digits: " + digits);
31. System.out.println("White spaces: " + spaces);
32. }
33. }

Example 1: Convert char to String


1. public class CharString {
2.
3. public static void main(String[] args) {
4. char ch = 'c';
5. String st = Character.toString(ch);
6. // Alternatively
7. // st = String.valueOf(ch);
8.
9. System.out.println("The string is: " + st);
10. }
11. }
Example 2: Convert char array to String

If you have a char array instead of just a char, we can easily convert it to String using String
methods as follows:

1. public class CharString {


2.
3. public static void main(String[] args) {
4. char[] ch = {'a', 'e', 'i', 'o', 'u'};
5.
6. String st = String.valueOf(ch);
7. String st2 = new String(ch);
8.
9. System.out.println(st);
10. System.out.println(st2);
11. }
12. }

Example 3: Convert String to char array

We can also convert a string to char array (but not char) using String's method toCharArray().

1. import java.util.Arrays;
2.
3. public class StringChar {
4.
5. public static void main(String[] args) {
6. String st = "This is great";
7.
8. char[] chars = st.toCharArray();
9. System.out.println(Arrays.toString(chars));
10. }
11. }

Example: Find Frequency of Character


1. public class Frequency {
2.
3. public static void main(String[] args) {
4. String str = "This website is awesome.";
5. char ch = 'e';
6. int frequency = 0;
7.
8. for(int i = 0; i < str.length(); i++) {
9. if(ch == str.charAt(i)) {
10. ++frequency;
11. }
12. }
13.
14. System.out.println("Frequency of " + ch + " = " + frequency);
15. }
16. }

// Frequency of e = 4
Example: Program to Remove All Whitespaces
1. public class Whitespaces {
2.
3. public static void main(String[] args) {
4. String sentence = "T his is b ett er.";
5. System.out.println("Original sentence: " + sentence);
6.
7. sentence = sentence.replaceAll("\\s", "");
8. System.out.println("After replacement: " + sentence);
9. }
10. }

11. Original sentence: T his is b ett er.


12.After replacement: Thisisbetter.

Example: Find ASCII value of a character


1. public class AsciiValue {
2.
3. public static void main(String[] args) {
4.
5. char ch = 'a';
6. int ascii = ch;
7. // You can also cast char to int
8. int castAscii = (int) ch;
9.
10. System.out.println("The ASCII value of " + ch + " is: " + ascii);
11. System.out.println("The ASCII value of " + ch + " is: " + castAscii);
12. }
13. }

14. The ASCII value of a is: 97


15.The ASCII value of a is: 97

Example 1: Swap two numbers using temporary variable


1. public class SwapNumbers {
2. public static void main(String[] args) {
3.
4. float first = 1.20f, second = 2.45f;
5.
6. System.out.println("--Before swap--");
7. System.out.println("First number = " + first);
8. System.out.println("Second number = " + second);
9. // Value of first is assigned to temporary
10. float temporary = first;
11.
12. // Value of second is assigned to first
13. first = second;
14. // Value of temporary (which contains the initial value of first) is assigned
to second
15. second = temporary;
16.
17. System.out.println("--After swap--");
18. System.out.println("First number = " + first);
19. System.out.println("Second number = " + second);
20. }
21. }

When you run the program, the output will be:


--Before swap--
First number = 1.2
Second number = 2.45
--After swap--
First number = 2.45
Second number = 1.2

Example 2: Swap two numbers without using temporary variable


1. public class SwapNumbers {
2.
3. public static void main(String[] args) {
4.
5. float first = 12.0f, second = 24.5f;
6.
7. System.out.println("--Before swap--");
8. System.out.println("First number = " + first);
9. System.out.println("Second number = " + second);
10.
11. first = first - second;
12. second = first + second;
13. first = second - first;
14.
15. System.out.println("--After swap--");
16. System.out.println("First number = " + first);
17. System.out.println("Second number = " + second);
18. }
19. }

When you run the program, the output will be:

--Before swap--
First number = 12.0
Second number = 24.5
--After swap--
First number = 24.5
Second number = 12.0

Example 1: Program to Check Prime Number using a for loop


1. public class Prime {
2.
3. public static void main(String[] args) {
4.
5. int num = 29;
6. boolean flag = false;
7. for(int i = 2; i <= num/2; ++i)
8. {
9. // condition for nonprime number
10. if(num % i == 0)
11. {
12. flag = true;
13. break;
14. }
15. }
16.
17. if (!flag)
18. System.out.println(num + " is a prime number.");
19. else
20. System.out.println(num + " is not a prime number.");
21. }
22. }

When you run the program, the output will be:

29 is a prime number.

Example 2: Program to Check Prime Number using a while loop


1. public class Prime {
2.
3. public static void main(String[] args) {
4.
5. int num = 33, i = 2;
6. boolean flag = false;
7. while(i <= num/2)
8. {
9. // condition for nonprime number
10. if(num % i == 0)
11. {
12. flag = true;
13. break;
14. }
15.
16. ++i;
17. }
18.
19. if (!flag)
20. System.out.println(num + " is a prime number.");
21. else
22. System.out.println(num + " is not a prime number.");
23. }
24. }
When you run the program, the output will be:

33 is not a prime number.

Example: Integer as a Sum of Two Prime Numbers


1. public class CheckPrime {
2.
3. public static void main(String[] args) {
4. int number = 34;
5. boolean flag = false;
6. for (int i = 2; i <= number / 2; ++i) {
7.
8. // condition for i to be a prime number
9. if (checkPrime(i)) {
10.
11. // condition for n-i to be a prime number
12. if (checkPrime(number - i)) {
13.
14. // n = primeNumber1 + primeNumber2
15. System.out.printf("%d = %d + %d\n", number, i, number - i);
16. flag = true;
17. }
18.
19. }
20. }
21.
22. if (!flag)
23. System.out.println(number + " cannot be expressed as the sum of two prime
numbers.");
24. }
25.
26. // Function to check prime number
27. static boolean checkPrime(int num) {
28. boolean isPrime = true;
29.
30. for (int i = 2; i <= num / 2; ++i) {
31. if (num % i == 0) {
32. isPrime = false;
33. break;
34. }
35. }
36.
37. return isPrime;
38. }
39. }
40. When you run the program, the output will be:
41. 34 = 3 + 31
42. 34 = 5 + 29
43. 34 = 11 + 23
44.34 = 17 + 17

Example: Prime Numbers Between Two Integers


1. public class Prime {
2. public static void main(String[] args) {
3. int low = 20, high = 50;
4.
5. while (low < high) {
6. if(checkPrimeNumber(low))
7. System.out.print(low + " ");
8. ++low;
9. }
10. }
11.
12. public static boolean checkPrimeNumber(int num) {
13. boolean flag = true;
14.
15. for(int i = 2; i <= num/2; ++i) {
16.
17. if(num % i == 0) {
18. flag = false;
19. break;
20. }
21. }
22. return flag;
23. }
24. }
When you run the program, the output will be:
23 29 31 37 41 43 47

You might also like