Input a number, reverse (change) its sign (positive/negative) and print it.
import java.util.*;
public class Number
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println(“Enter a number”);
int n=sc.nextInt();
n = - n;
System.out.println(“Number: “+n);
}
}
2. Conditional Construct: Selection Statement
if
Input a number, if it is negative make it positive. Print the number.
import java.util.*;
public class Number
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println(“Enter a number”);
int n=sc.nextInt();
if(n<0)
n = - n;
System.out.println(“Number: “+n);
}
}
if else
Input a number, check and print whether it is positive or not positive.
import java.util.*;
public class Number
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println(“Enter a number”);
int n=sc.nextInt();
if(n>0)
System.out.println(“Positive”);
else
System.out.println(“Not Positive”);
}
}
if else if else
Input a number, check and print whether it is positive or negative or zero.
import java.util.*;
public class Number
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println(“Enter a number”);
int n=sc.nextInt();
if(n>0)
System.out.println(“Positive”);
else if(n<0)
System.out.println(“Negative”);
else
System.out.println(“Zero”);
}
}
Nested if
Input a number, check and print whether it is Positive Even or Not Positive Even.
import java.util.*;
public class Number
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println(“Enter a number”);
int n=sc.nextInt();
if(n>0)
{
if(n%2==0)
System.out.println(“Positive Even”);
else
System.out.println(“Not Positive Even”);
}
else
System.out.println(“Not Positive Even”);
}
}
Nested if else if else
Check whether an input number it is Positive Even or Positive Odd or Negative Even or Negative Odd or Zero.
import java.util.*;
public class Number
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println(“Enter a number”);
int n=sc.nextInt();
if(n>0)
{
if(n%2==0)
System.out.println(“Positive Even”);
else
System.out.println(“Positive Odd”);
}
else if(n<0)
{
if(n%2==0)
System.out.println(“Negative Even”);
else
System.out.println(“Negative Odd”);
}
else
System.out.println(“Zero”);
}
}
switch case
Write a menu driven program: 1) If an input number is negative even find its square;
otherwise nothing to be done. Print the number.
2) If an input number is positive odd find its cube; otherwise nothing to be done.
Print the number.
import java.util.*;
public class Number
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("1 Find square of negative even");
System.out.println("2 Find cube of positive odd");
System.out.println("Enter your choice");
int ch=sc.nextInt();
int n,x;
switch(ch)
{
case 1: System.out.println("Input a number: ");
n=sc.nextInt();
if(n<0 && n%2==0)
x=n*n;
System.out.println("Square: "+x);
break;
case 2: System.out.println("Enter a number ");
n=sc.nextInt();
if(n>0&&n%2==0)
x=n*n*n;
System.out.println("Cube: "+x);
default: System.out.println("Wrong input");
)
}
}
3. Iterative Construct (Loop):
for
Input n numbers, check whether they are positive or negative or zero. Count them separately.
import java.util.*;
public class Number
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println(“Enter number of numbers”);
int n=sc.nextInt();
int c1=0,c2=0,c3=0;
for(int i=1;i<=n;i++)
{
System.out.println(“Enter number”);
int num=sc.nextInt();
if(num>0)
{
System.out.println(“Positive “);
c1++;
}
else if(n<0)
{
System.out.println(“Negative “);
c2++;
}
else
{
System.out.println(“Zero”);
c3++;
}
}
System.out.println(“Count of positives: “+c1);
System.out.println(“Count of neatives: “+c2);
System.out.println(“Count of zeros: “+c3);
}
}
Remaining exercises will be opened after being discussed in the class
while
Input n numbers, check whether they are positive or negative or zero. Count them separately.
do while
Input some numbers, check whether positive or negative or zero. Find sum and product of them separately.