Java Fundamentals flow control statements



1. Write a program to check if a given integer number is Positive, Negative, or Zero.

 

1:  import java.util.Scanner;  
2:  public class Assignment01 {  
3:       public static void main(String[] args) {  
4:            // TODO Auto-generated method stub  
5:            Scanner scan = new Scanner(System.in);  
6:            System.out.print("Enter the number:");  
7:            int num = scan.nextInt();  
8:            if(num > 0) {  
9:                 System.out.println("Number is Positive");  
10:            }  
11:            else if(num < 0) {  
12:                 System.out.println("Number is Negative");  
13:            }  
14:            else {  
15:                 System.out.println("Number is Zero");  
16:            }  
17:       }  
18:  }  


2. Write a program to check if a given integer number is odd or even.


1:  import java.util.Scanner;  
2:  public class Assignment02 {  
3:       public static void main(String[] args) {  
4:            // TODO Auto-generated method stub  
5:            Scanner scan = new Scanner(System.in);  
6:            System.out.print("Enter the number:");  
7:            int num = scan.nextInt();  
8:            if(num % 2 != 0) {  
9:                 System.out.println("Number is Odd");  
10:            }  
11:            else {  
12:                 System.out.println("Number is Even");  
13:            }  
14:       }  

3. Write a program to check if the program has received command-line arguments or not.

If the program has not received arguments then print "No Values", else print all the values in a single line separated by,(comma)

Example1) java Example

O/P: No values

Example2) java Example Mumbai Bangalore

O/P: Mumbai, Bangalore

[Hint: You can use the length property of an array to check its length]


1:  public class Assignment03 {  
2:       public static void main(String[] args) {  
3:            // TODO Auto-generated method stub  
4:            if(args.length == 0) {  
5:                 System.out.println("No values");  
6:            }  
7:            else {  
8:                 for(String name : args) {  
9:                      System.out.print(name + " ");  
10:                 }  
11:            }  
12:       }  
13:  }  


4. Initialize two character variables in a program and display the characters in alphabetical order.

Example1: If the first character is 's' and the second character is 'e' then the output should be  e,s

Example2: If the first character is 'a' and the second character is 'e', then the output should be a,e


1:  public class Assignment04 {  
2:       public static void main(String[] args) {  
3:            // TODO Auto-generated method stub  
4:            char var1 = 's';  
5:            char var2 = 'e';  
6:            if (var1 > var2)  
7:        System.out.println(var2+" , "+var1);  
8:      else  
9:        System.out.println(var1+" , "+var2);  
10:       }  
11:  }  

5. Initialize a character variable in a program and 

print 'Alphabet' if the initialized value is an alphabet, 

print 'Digit' if the initialized value is a number, and 

print 'Special Character', if the initialized value is anything else.


1:  public class Assignment05 {  
2:       public static void main(String[] args) {  
3:            // TODO Auto-generated method stub  
4:            char var = '@';  
5:            if((var >= 65 && var <= 90) || (var >= 97 && var <= 122) ) {  
6:                 System.out.println("Alphabet");  
7:            }  
8:            else if(var >= 48 && var <= 57) {  
9:                 System.out.println("Number");  
10:            }  
11:            else {  
12:                 System.out.println("Special Character");  
13:            }  
14:       }  
15:  }  

6. Write a program to accept gender ("Male" or "Female") and age from command line arguments and print the percentage of interest-based on the given conditions.

If the gender is 'Female' and age is between 1 and 58, the percentage of interest is 8.2%.

If the gender is 'Female' and the age is between 59 and 100, the percentage of interest is 9.2%.

If the gender is 'Male' and age is between 1 and 58, the percentage of interest is 8.4%.

If the gender is 'Male' and age is between 59 and 100, the percentage of interest is 10.5%.


1:  public class Assignment06 {  
2:       public static void main(String[] args) {  
3:            // TODO Auto-generated method stub  
4:            int age = Integer.parseInt(args[1]);  
5:            if(args[0].equals("Female")) {  
6:                 if(age >= 1 && age <= 58) {  
7:                      System.out.println("the percentage of interest is 8.2%");  
8:                 }  
9:                 else {  
10:                      System.out.println("the percentage of interest is 9.2%");  
11:                 }  
12:            }  
13:            else {  
14:                 if(age >= 1 && age <= 58) {  
15:                      System.out.println("the percentage of interest is 8.4%");  
16:                 }  
17:                 else {  
18:                      System.out.println("the percentage of interest is 10.5%");  
19:                 }  
20:            }  
21:       }  

7. Initialize a character variable with an alphabet in a program.

If the character value is in lowercase, the output should be displayed in uppercase in the following format.

Example1:

i/p:a

o/p:a->A

If the character value is in uppercase, the output should be displayed in lowercase in the following format.

Example2:

i/p:A

o/p:A->a


1:  public class Assignment07 {  
2:       public static void main(String[] args) {  
3:            // TODO Auto-generated method stub  
4:            char var = 'a';  
5:            if (var >= 'a' && var <= 'z')  
6:                 System.out.println((char)(var-32));                    //lowercase to uppercase  
7:          else  
8:               System.out.println((char)(var+32));                //uppercase to lowercase  
9:       }  
10:  }  

8. Write a program to receive a color code from the user (an Alphabet). 

The program should then print the color name, based on the color code given. 

The following are the color codes and their corresponding color names.

R->Red, B->Blue, G->Green, O->Orange, Y->Yellow, W->White. 

If the color code provided by the user is not valid then print "Invalid Code".


1:  import java.util.Scanner;  
2:  public class Assignment08 {  
3:       public static void main(String[] args) {  
4:            // TODO Auto-generated method stub  
5:            Scanner scan = new Scanner(System.in);  
6:            System.out.print("Enter color code :");  
7:            char color = scan.next().charAt(0);  
8:            switch(color){  
9:                 case 'R' :  
10:                 case 'r' :  
11:                      System.out.println("Red");  
12:                      break;  
13:                 case 'B' :  
14:                 case 'b' :  
15:                      System.out.println("Blue");  
16:                      break;  
17:                 case 'G' :  
18:                 case 'g' :  
19:                      System.out.println("Green");  
20:                      break;  
21:                 case 'O' :  
22:                 case 'o' :  
23:                      System.out.println("Orange");  
24:                      break;  
25:                 case 'Y' :  
26:                 case 'y' :  
27:                      System.out.println("Yellow");  
28:                      break;  
29:                 case 'W' :  
30:                 case 'w' :  
31:                      System.out.println("White");  
32:                      break;  
33:                 default :  
34:                      System.out.println("Invalid Code");  
35:            }  
36:       }  
37:  }  

9. Write a program to receive a number and print the corresponding month name.

Example1:

C:\>java Sample 12

O/P Expected: December

Example2:

C:\>java Sample 

O/P Expected: Please enter the month in numbers

Example3:

C:\>java Sample 15

O/P Expected: Invalid month


1:  public class Assignment09 {  
2:       public static void main(String[] args) {  
3:            // TODO Auto-generated method stub  
4:            switch(args[0]) {  
5:            case "1":  
6:                 System.out.println("January");  
7:                 break;  
8:            case "2":  
9:                 System.out.println("February");  
10:                 break;  
11:            case "3":  
12:                 System.out.println("March");  
13:                 break;  
14:            case "4":  
15:                 System.out.println("April");  
16:                 break;  
17:            case "5":  
18:                 System.out.println("May");  
19:                 break;  
20:            case "6":  
21:                 System.out.println("June");  
22:                 break;  
23:            case "7":  
24:                 System.out.println("July");  
25:                 break;  
26:            case "8":  
27:                 System.out.println("August");  
28:                 break;  
29:            case "9":  
30:                 System.out.println("September");  
31:                 break;  
32:            case "10":  
33:                 System.out.println("October");  
34:                 break;  
35:            case "11":  
36:                 System.out.println("November");  
37:                 break;  
38:            case "12":  
39:                 System.out.println("December");  
40:                 break;  
41:            default:  
42:                 System.out.println("Invalid Month");  
43:            }  
44:       }  
45:  }  

10. Write a program to print numbers from 1 to 10 in a single row with one tab space.


1:  public class Assignment10 {  
2:       public static void main(String[] args) {  
3:            // TODO Auto-generated method stub  
4:            for(int i = 1; i <= 10; i++) {  
5:                 System.out.print(i+" ");  
6:            }  
7:       }  
8:  }  

11. Write a program to print even numbers between 23 and 57. Each number should be printed in a separate row.


1:  public class Assignment11 {  
2:       public static void main(String[] args) {  
3:            // TODO Auto-generated method stub  
4:            for(int i = 23; i <= 57; i++) {  
5:                 if(i % 2 == 0) {  
6:                      System.out.println(i);  
7:                 }  
8:            }  
9:       }  
10:  }  

12. Write a program to check if a given number is prime or not.


1:  import java.util.Scanner;  
2:  public class Assignment12 {  
3:       public static void main(String[] args) {  
4:            // TODO Auto-generated method stub  
5:            Scanner scan = new Scanner(System.in);  
6:            System.out.print("Enter the number:");  
7:            int num = scan.nextInt();  
8:            int count = 0;  
9:            for(int i = 2; i < num/2; i++) {  
10:                 if(num % i == 0) {  
11:                      count++;  
12:                 }  
13:                 else {  
14:                      continue;  
15:                 }  
16:            }  
17:            if(count == 0) {  
18:                 System.out.println("Number is prime");  
19:            }  
20:            else {  
21:                 System.out.println("Number is NOT prime");  
22:            }  
23:       }  
24:  }  

13. Write a program to print prime numbers between 10 and 99.


1:  public class Assignment13 {  
2:       public static void main(String[] args) {  
3:            // TODO Auto-generated method stub  
4:            int flag;  
5:            for(int i = 10; i <= 99; i++) {  
6:                 flag = 1;  
7:                 for(int j = 2; j <= i/2; j++) {  
8:                      if(i % j == 0) {  
9:                           flag = 0;  
10:                           break;  
11:                      }  
12:                 }  
13:                 if(flag == 1) {  
14:                      System.out.print(i+" ");  
15:                 }  
16:            }  
17:       }  
18:  }  

14. Write a Java program to find if the given number is prime or not.

Example1:

C:\>java Sample 

O/P: Please enter an integer number 

Example2:

C:\>java Sample 1

O/P:1 is neither prime nor composite

Example3:

C:\>java Sample 0

O/P: 0 is neither prime nor composite

 

Example4:

C:\>java Sample 10

O/P: 10 is not a prime number

Example5:

C:\>java Sample 7

O/P: 7 is a prime number


1:  import java.util.Scanner;  
2:  public class Assignment14 {  
3:       public static void main(String[] args) {  
4:            // TODO Auto-generated method stub  
5:            int num = 0;  
6:        if(args.length >= 1) {  
7:          num = Integer.parseInt(args[0]);  
8:        }  
9:        else {  
10:          Scanner scan = new Scanner(System.in);  
11:          System.out.print("Please enter an integer number ");  
12:          num = scan.nextInt();  
13:        }  
14:        if(num == 0 || num == 1) {  
15:           System.out.println(num + " is neither prime nor composite");  
16:        }  
17:        else {  
18:           if(isPrime(num))  
19:            System.out.println(num + " is a prime number");  
20:          else  
21:            System.out.println(num + " is a not prime number");  
22:        }  
23:      }  
24:      static boolean isPrime(int num) {  
25:        for(int i=2; i <= num/2; i++) {  
26:          if(num % i == 0)  
27:            return false;  
28:        }  
29:        return true;  
30:       }  
31:  }  

15. Write a program to print the sum of all the digits of a given number.

Example1: 

I/P:1234

O/P:10


1:  import java.util.Scanner;  
2:  public class Assignment15 {  
3:       public static void main(String[] args) {  
4:            // TODO Auto-generated method stub  
5:            Scanner scan = new Scanner(System.in);  
6:            System.out.print("Enter the number: ");  
7:            int num = scan.nextInt();  
8:            int sum;  
9:            for(sum = 0; num > 0; num = num/10)   
10:                sum = sum + (num % 10);  
11:            System.out.println(sum);  
12:       }  

16. Write a program to print * in Floyd's format (using for and while loop)

*

*  *

*  *   *

Example1:

C:\>java Sample 

O/P: Please enter an integer number

Example2:

C:\>java Sample 3

O/P :

*

*  * 

*  *  *


1:  import java.util.Scanner;  
2:  public class Assignment16 {  
3:       public static void main(String[] args) {  
4:            // TODO Auto-generated method stub  
5:            int count;  
6:            if(args.length == 0) {  
7:                 Scanner scan = new Scanner(System.in);  
8:                 System.out.println("Please enter an integer number:");  
9:                 count = scan.nextInt();  
10:            }  
11:            else {  
12:                 count = Integer.parseInt(args[0]);  
13:            }  
14:            System.out.println("Using For Loop :");  
15:            for(int i = 1; i <= count; i++) {  
16:                 for(int j = 1; j <=i; j++) {  
17:                      System.out.print("*");  
18:                 }  
19:                 System.out.println(" ");  
20:            }  
21:            System.out.println("Using While Loop :");  
22:            int i = count, j;                                                                   
23:            while(i >= 1){  
24:              j = i;  
25:              while(j <= count){  
26:                   System.out.print("*");  
27:                j++;  
28:              }  
29:              i--;  
30:              System.out.println(" ");  
31:            }  
32:       }  
33:  }  

17. Write a program to reverse a given number and print

Example1:

I/P: 1234

O/P:4321

Example2:

I/P:1004

O/P:4001


1:  import java.util.Scanner;  
2:  public class Assignment17 {  
3:       public static void main(String[] args) {  
4:            // TODO Auto-generated method stub  
5:            Scanner scan = new Scanner(System.in);  
6:            System.out.print("Enter the number :");  
7:            int num = scan.nextInt();  
8:            int digit, reverse = 0;  
9:            while(num > 0) {  
10:                 digit = num % 10;  
11:                 reverse = reverse * 10 + digit;  
12:                 num = num / 10;  
13:            }  
14:            System.out.println(reverse);  
15:       }  
16:  }  

18. Write a Java program to find if the given number is palindrome or not

Example1:

C:\>java Sample 110011

O/P: 110011 is a palindrome

Example2:

C:\>java Sample 1234

O/P: 1234 is not a palindrome


1:  public class Assignment18 {  
2:       public static void main(String[] args) {  
3:            // TODO Auto-generated method stub  
4:            int num = Integer.parseInt(args[0]);  
5:            int temp = num, digit, reverse = 0;  
6:            while(temp > 0) {  
7:                 digit = temp % 10;  
8:                 reverse = reverse * 10 + digit;  
9:                 temp = temp / 10;  
10:            }  
11:            if(num == reverse) {  
12:                 System.out.println(num + " "+ "is a palindrome");  
13:            }  
14:            else {  
15:                 System.out.println(num + " "+ "is not a palindrome");  
16:            }  
17:       }  
18:  }  

19. Write a program to print the first 5 values which are divisible by 2, 3, and 5.


1:  public class Assignment19 {  
2:       public static void main(String[] args) {  
3:            // TODO Auto-generated method stub  
4:            int count = 0, num = 1;  
5:            while(count < 5) {  
6:                 if(num % 2 == 0 && num % 3 == 0 && num % 5 == 0) {  
7:                      System.out.print(num + " ");  
8:                      count++;  
9:                 }  
10:                 num++;  
11:            }  
12:       }  

thank you