0% found this document useful (0 votes)
22 views

Import Java

The code defines a main method that takes user input of a number using a Scanner, then checks if the number is even or odd by testing if it is divisible by 2 using the modulo operator. It prints either "Number is even!" or "Number is Odd!" depending on the result. This allows the program to determine and output whether a user-inputted number is even or odd.

Uploaded by

Noor Saba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Import Java

The code defines a main method that takes user input of a number using a Scanner, then checks if the number is even or odd by testing if it is divisible by 2 using the modulo operator. It prints either "Number is even!" or "Number is Odd!" depending on the result. This allows the program to determine and output whether a user-inputted number is even or odd.

Uploaded by

Noor Saba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

import java.util.

Scanner;
public class App {
   
   
    public static void main(String[] args)  {
        Scanner myObj = new Scanner(System.in);
        int x;
        System.out.println("Enter a num!");
        x=myObj.nextInt();
       
if(x%2==0)
{
    System.out.println("Number is even!");
}
else{
    System.out.println("Number is Odd!");
}
}
}

Output

Code:

import java.util.Scanner;
public class App {
   
   
    public static void main(String[] args)  {
        Scanner myObj = new Scanner(System.in);
        int x;
        System.out.println("Enter a num!");
        x=myObj.nextInt();
       
switch(x){
case 1:
    System.out.println("Number is even!");
   
case 2:
    System.out.println("Number is Odd!");
    break;

default:
    System.out.println("Wrong Input");
break;
}
    }
}
Output

Code

import java.util.Scanner;
public class App {
   
   
    public static void main(String[] args)  {
 
        int sum=0;    
for(int i=1;i<=100;i++)
{
  sum=sum+i;
}
System.out.println(sum);
    }
}

Output

Code:

import java.util.Scanner;
public class App {
   
   
    public static void main(String[] args)  {
        int x=0;
        int sum=0;
while(x>=0)
{
    Scanner myObj = new Scanner(System.in);
        System.out.println("Enter a num!");
        x=myObj.nextInt();
  sum=sum+x;
}
System.out.println("You have entered negative num");
System.out.println("Sum of positive numbers you have entered is");
System.out.println(sum);
    }
}
Output:

Code:

import java.util.Scanner;
public class App {
   
   
    public static void main(String[] args)  {
        int x,y;
        Scanner myObj = new Scanner(System.in);
        System.out.println("Enter a your marks!");
        x=myObj.nextInt();
        int [] arr;
arr= new int[];
        for(int i=0;i<5;i++)
{
    arr[i]=myObj.nextInt();
}
System.out.println("The marks stored in the array are:");
for(int i=0;i<5;i++)
{
    System.out.println(arr[i]);
}
System.out.println("Enter the index from 0 to 4 to access any random value!");
y=myObj.nextInt();
System.out.println("The marks of student at index"+y+"are:"+arr[y]);

    }
}

Output:
Code:

import java.util.Scanner;
public class App {
    public static double calarea(double r)
    {
double area;
area=Math.PI*r*r;
return area;
    }
   
    public static void main(String[] args)  {
        double x;
        Scanner myObj = new Scanner(System.in);
        System.out.println("Enter the radius!");
        x=myObj.nextDouble();
        System.out.println(calarea(x));
    }
}

Output:
Code:

You might also like