Java Quiz (2) : Show your java strength

28 views
Skip to first unread message

Kamlesh Paunikar

unread,
Sep 26, 2009, 6:35:44 AM9/26/09
to Code Gurukul
Hello

Can you write a program to check whether the given number (n) is
positive, negative or zero using switch
case statement. where n is input through the keyboard.

Thanks
KP Solutions

Dhawal Saiya

unread,
Sep 27, 2009, 10:23:01 AM9/27/09
to Code Gurukul


On Sep 26, 3:35 pm, Kamlesh Paunikar <kamleshpauni...@gmail.com>
wrote:
Program code :

public class Main {

public static void main(String[] args) {
if(args.length==1)
{
//float n=Float.parseFloat(args[0]);
float c=Math.signum(Float.parseFloat(args[0]));
int d=(int)(c);
switch(d)
{
case -1:
System.out.println("Entered number is negative");;
break;
case 0:
System.out.println("Entered number is Zero");
break;
case 1:
System.out.println("Entered number is positive");;
break;
default:
System.out.println("Invalid number!!!");

}
else
System.out.println("Invalid number of
arguments!!!");
}
}

}

Rahul Raja

unread,
Sep 27, 2009, 10:01:55 PM9/27/09
to Code Gurukul


On Sep 26, 3:35 pm, Kamlesh Paunikar <kamleshpauni...@gmail.com>
wrote:
Program:

class example2
{
public static void main(String arg[])
{
if(arg.length>1)
System.out.println("Invalid number of arguments");
else
{
int n=Integer.parseInt(arg[0]);
int b;
if(n>0)
b=1;
else if(n<0)
b=-1;
else
b=0;
switch(b)
{
case 1 : System.out.println("Entered number is positive number");
break;
case -1: System.out.println("Entered number is negative number");
break;
case 0: System.out.println("Entered number is Zero");
break;
}
}
}
}

Thanks,
Rahul Raja

Manoj Gulahe

unread,
Sep 27, 2009, 8:22:23 AM9/27/09
to code-g...@googlegroups.com
import java.io.*;
class NumberType
{

public static void main(String args[])throws IOException
{
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
System.out.println("*********It only built up for number
Recognization not for 'character'*********\nPlease,enter only
number:");
switch(bf.read())
{
case 45://minus
System.out.println("Negative number");
break;
case 48://zero
System.out.println("Zero");
break;
default:
System.out.println("positive");

Manoj Gulahe

unread,
Sep 28, 2009, 6:04:31 AM9/28/09
to Code Gurukul
//Last Program having error in detecting zero.So i Write this one

import java.io.*;
class NumberType
{

public static void main(String args[])throws IOException
{
String str;
int num=0;
BufferedReader bf=new BufferedReader(new InputStreamReader
(System.in));
System.out.println("Enter only number:");
str=bf.readLine();
try{
num=Integer.parseInt(str);
str=""+num;
switch(str.charAt(0))
{
case 45://minus
System.out.println("Negative number");
break;
case 48://zero
System.out.println("Zero");
break;
default:
System.out.println("positive");
}
}
catch(NumberFormatException e){
System.out.println("Invalide Integer");
}


}
}

Manoj Gulahe

unread,
Sep 27, 2009, 8:32:59 AM9/27/09
to Code Gurukul
***Check it sir ***
import java.io.*;
class NumberType
{

public static void main(String args[])throws IOException
{
BufferedReader bf=new BufferedReader(new InputStreamReader
(System.in));
System.out.println("*********It only built up for number
Recognization not for 'character'*********\nPlease,enter only
number:");
switch(bf.read())
{

Sumit Desai

unread,
Sep 30, 2009, 6:41:04 AM9/30/09
to Code Gurukul
class Example
{
public static void main(String arg[])
{ int a=0;
int flag=0;
if(arg.length>1)
{

System.out.println("Invalid no. of Arguments");
}
else
{
String s=arg[0];
int l=s.length();

if(s.charAt(0)=='-')
{
a=0;
}
else
{
for(int i=0;i<l;i++)
{ if((s.charAt(i))!='0')
{
flag=1;
break;
}
}
if(flag==0)
{
a=1;
}
else
{
a=2;
}

}
switch(a)
{
case 0 :
System.out.println("No. is negative");
break;

case 1 :
System.out.println("No. is zero");
break;

case 2 :
System.out.println("No. is positive");
break;
}
}
}
}
Reply all
Reply to author
Forward
0 new messages