Conditional Statement Introduction

Conditional is an essential part in programming. You can do many things with it. In the past, I read a book about basic programming. The author said that most of programs written from various language are only contain  repetition and conditional. So this time I’ll give a solution from 30 days challenge code from hackerrank. This is really a simple program about selection of odd and even number.




Okay, the image above is the condition which we should implement in listing code. It’s quite easy right. First,  you input integer number (not decimal). Remember the constraint is number you input is from 1 to 100. .Then you start with selection whether the number is odd or not. In mathematic we all know that odd number is an integer number that can not be divided by 2. So if we operate it with modulo operation, odd number will result 1. Odd number should print “Weird”.


Back again, if the number is not odd then do a selection again for even number. The range already mention in the picture. Print “Weird” or not “Not Weird” according the range. So here is the solution from this task


import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
   
   public static void main(String[] args) {
      Scanner scan = new Scanner(System.in);
      int num = scan.nextInt(); 
      scan.close();
      String st="";
          
        //if-else
       if(num>0 && num <=100){
            if(num%2 == 1){
                 st="Weird";
            }else if (num >= 2 && num <= 5){
                st="Not Weird";
            }else if (num > 20){
                st="Not Weird";
            }else {
                st="Weird";
            }
       }
        
      System.out.println(st);
   }
}


The result of listing code above is like this :



Comments

Popular posts from this blog

Quantization of Image Data Implementation in C#

Computer Graphics Project "Planet Orbit" Source Code with Glut OpenGL

Computer Graphics Project "MINION" Source Code with Glut OpenGL