[Go to site: main page, start]

0% found this document useful (0 votes)
11 views19 pages

Java Programs for Basic Arithmetic and Logic

The document contains a series of Java programming exercises, each with a specific task such as performing arithmetic operations, calculating areas, using control statements, and handling user input. Each exercise includes a code solution, expected output, and explanations of concepts like operators, loops, and conditionals. The tasks range from basic operations like addition and finding the largest number to more complex tasks like checking for vowels and printing days of the week.

Uploaded by

uzaird63
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)
11 views19 pages

Java Programs for Basic Arithmetic and Logic

The document contains a series of Java programming exercises, each with a specific task such as performing arithmetic operations, calculating areas, using control statements, and handling user input. Each exercise includes a code solution, expected output, and explanations of concepts like operators, loops, and conditionals. The tasks range from basic operations like addition and finding the largest number to more complex tasks like checking for vowels and printing days of the week.

Uploaded by

uzaird63
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

[Link] a Program to print Addition of Two numbers.

Solution:

import [Link].*;
class addition
{
public static void main(String[] args) {
int num_1 = 5;
int num_2 = 10;
int sum;

sum = num_1 + num_2;


[Link]("The sum is :" +sum);
}
}

Output:

The sum is :15

[Link] a Program to print addition , substraction and multiplication of two


numbers.

Solution:

import [Link].*;
class addition
{
public static void main(String[] args) {
int num_1 = 5;
int num_2 = 10;
int sum,sub,multi;

sum = num_1 + num_2;


sub = num_1 - num_2;
multi = num_1 * num_2;

[Link]("The addition is :" +sum);


[Link]("The substraction is :" +sub);
[Link]("The multiplicaion is :" +multi);
}
}

Output:
The addition is :15

The substraction is :-5

The multiplicaion is :50

[Link] a Program To Calculate Area Of Circle.

Solution:

import [Link].*;
class Area_of_circle
{
public static void main(String[] args) {
float area,radius,pi;
radius = 4;
pi = 3;

area = pi * radius * radius;

[Link]("The area of circle is:" +area);


}
}

Output:

The area of circle is : 48.0

[Link] a Program to calculate the area of rectangle.

Solution:

import [Link].*;
class Area_of_rectangle
{
public static void main(String[] args) {
float length,bradth,area;
length = 45;
bradth = 59;

area = length * bradth;

[Link]("The area of Rectangle is :" +area);


}
}

Output:

The area of Rectangle is : 2655.0


[Link] a program to calculate area of square.

Solution:

import [Link].*;
class Area_of_square
{
public static void main(String[] args) {
int area, side;
side = 8;
area = side*side;

[Link]("The area of square is :" +area);


}
}

Output:

The area of square is : 64

[Link] a program using operators.

Solution:

import [Link].*;
class operators
{
public static void main(String[] args) {
int num_1,num_2;
num_1=10;
num_2=4;

[Link](num_1 > num_2);


[Link](num_1 < num_2);
[Link](num_1 == num_2);
[Link](num_1 != num_2);

}
}

Output:

true

false

false

true
[Link] a program in java using If Statement.

Solution:

import [Link].*;
class if_operator
{
public static void main(String[] args) {
int age=19;

if (age>=18)
{
[Link]("Your adult");
}
}
}

Output:

Your adult

[Link] a Program in Java to Find The Highest number.

Solution:

import [Link].*;
class highest_number
{
public static void main(String[] args) {
int num_1,num_2;
num_1=50;
num_2=25;

if (num_1>num_2)
{
[Link]("num_1 is greater");
}
else {
[Link]("num_2 is highest");
}
}
}

Output:

num_1 is greater

[Link] a Program in java to find the smallest number.


Solution:

import [Link].*;
class smallest_number
{
public static void main(String[] args) {
int num_1,num_2;
num_1=50;
num_2=100;

if (num_1<num_2)
{
[Link]("num_1 is smallest");
}
else {
[Link]("num_2 is smallest");
}
}
}

Output:

num_1 is smallest.

[Link] a Program In java to check if the number is even or not.

Solution:

import [Link].*;
class check_if_even_or_not
{
public static void main(String[] args) {
int num=6;

if (num % 2 == 0)
{
[Link]("num is even");
}
else {
[Link]("Num is not even");
}
}
}

Output:

num is even.
[Link] a program in java to find the largest among three numbers.

Solution:

import [Link].*;
class Largest_among_three
{
public static void main(String[] args) {
int num_1=10;
int num_2=20;
int num_3=30;

if (num_1>num_2 && num_1>num_3)


{
[Link]("num_1 is largest");
} else if (num_2>num_1 && num_2>num_3) {
[Link]("num_2 is largest");
} else if (num_3>num_1 && num_3>num_2) {
[Link]("num_3 is largest");
}
else {
[Link]("all numbers are equal");
}
}
}

Output:

num_3 is largest.

[Link] a program to check if the number is even or odd by taking input


from the user.

Solution:

import [Link].*;
import [Link];

class check_if_even_or_odd
{
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number :");
int num = [Link]();
if (num % 2 == 0)
{
[Link]("Number is even");
}
else {
[Link]("Number is odd");
}
}
}

Output:

Enter a number :

Number is even

[Link] a program in java to check if you are adult or not.

Solution:

import [Link].*;
import [Link];
class adult_or_not
{
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter your age:");
int age = [Link]();

if (age >= 18)


{
[Link]("You ara an adult");
}
else
{
[Link]("You are not an adult");
}
}
}

Output:

Enter your age:

19
You ara an adult.

[Link] a Program in java to check the grades of a student.

Solution:

import [Link].*;
class grade
{
public static void main(String[] args) {
int marks = 91;

if(marks >90)
{
[Link]("A Grade");
} else if (marks >80) {
[Link]("B Grade");
}
else {
[Link]("you are just pass");
}
}
}

Output:

A Grade.

[Link] a Program in java to check if the number is positive , negative or


zero.

Solution:

import [Link].*;
class Check_if_the_number_is_positive_negative_or_zero
{
public static void main(String[] args) {
int num=10;

if(num>0)
{
[Link]("number is positive");
} else if (num < 0) {
[Link]("num is negative");
}
else {
[Link]("num is zero");
}
}
}

Output:

number is positive

16. Write a Program in java using if else-if.

Solution:

import [Link].*;
class if_else_if
{

public static void main(String[] args) {


int num=25;

if(num==25)
{
[Link]("num is found");
} else if (num == 25) {
[Link]("num is found");
}
else {
[Link]("num is not found");
}
}
}

Output:

num is found.

[Link] a Program in java which shows the use of input statement.

Solution:

import [Link].*;
import [Link];
class input
{
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a name:");
String name = [Link]();

[Link]("Welcome : "+ name);


}
}

Output:

Enter a name:Moahammad uzair

Welcome : Moahammad Uzair.

18.

[Link] a Program in java to print your first, middle and last name.

Solution:

import [Link].*;
import [Link];
class print_first_last_middle_name
{

public static void main(String[] args) {


Scanner sc = new Scanner([Link]);

[Link]("ENter your first name");


String Fn = [Link]();
[Link]("ENter your Middle name:");
String Mn = [Link]();
[Link]("ENter your Last name:");
String Ln = [Link]();

[Link]("First Name :" +Fn);


[Link]("Middle Name :" +Mn);
[Link]("Last Name :" +Ln);

}
}

Output:

Enter your first name : Moahmmad

Enter your Middle name : Uzair

Enter your Last name : Hingora

First Name : Moahmmad


Middle Name :Uzair

Last Name :Hingora

[Link] a program in java for Swapping two numbers.

Solution:

import [Link].*;
class swap
{
public static void main(String[] args) {
int a = 10;
int b = 20;
int c;

[Link](a);
[Link](b);

c = a;
a = b;
b = c;

[Link]("After swap");
[Link](a);
[Link](b);
}
}

Output:

10

20

After swap

20

10

[Link] a Program in java to check Character is alphabet or not.

Solution:

import [Link].*;
class alphabet
{
public static void main(String[] args) {
char c = 'a';

if( (c>='a' && c<='z') || (c>='A' && c>='Z') )


{
[Link]("character is alphabet");
}
else{
[Link]("character is not alphabet");
}
}
}

Solution:

character is alphabet.

[Link] a program using Or Operator.

Solution:

import [Link].*;
import [Link];
class or_operator
{
public static void main(String[] args) {
//program start
Scanner sc = new Scanner([Link]);
[Link]("enter weather");
String weather = [Link]();
[Link]("Eneter any day");
String day = [Link]();

if (weather == "good" || day == "sunday")


{
[Link]("you can go outside");
}
else {
[Link]("You cannot go outside");
}
//Program end
}
}

Output:

enter weather : good


Eneter any day : sunday

You cannot go outside

[Link] a Program to show the use of increment operator.

Solution:

import [Link].*;
class incerement
{
public static void main(String[] args) {
int counter = 0;

[Link]("Increment Counter");
[Link]("Initial Value :" +counter);

// post increment
[Link]("Post-increment(counter++):");
[Link]("return value :" + counter++);

//pre-increment
[Link]("Pre-increment(++counter ):");
[Link]("return value :" + ++counter);

}
}

Output:

Increment Counter

Initial Value :0

Post-increment(counter++):

return value :0

Pre-increment(++counter ):

return value :2

[Link] a program in java to show the use of decrement operator.

Solution:

import [Link].*;
import [Link];
public class decrement_operator {
public static void main(String[]args)
{
Scanner Sc = new Scanner([Link]);
[Link]("Enter your no");
int counter = [Link]();

[Link]("decrement operatos(--)");
[Link]("initial value:"+counter);

//post decrement

[Link]("\n post decrement(counter --):");


[Link]("returned value :" +counter--);
[Link]("value after "+counter);

//pre decrement

[Link]("\n pre decrement(counter --):");


[Link]("returned value " + --counter);
[Link]("value after:" +counter);

}
}

Output:

Enter your no

decrement operatos(--)

initial value:3

post decrement(counter --):

returned value :3

value after 2

pre decrement(counter --):

returned value 1
value after:1

[Link] a program to check enter value is vowel or not.

Solution:

import [Link].*;
import [Link];
class check_enter_value_is_vowel_or_not
{
public static void main(String[] args) {
//Program Start
Scanner sc = new Scanner([Link]);
[Link]("Enter any alphabet :");
char vowel = [Link]().charAt(0);

switch (vowel)
{
case 'a','A','e','E','i','I','o','O','u','U':
[Link](vowel +" is a vowel");
break;

default:
[Link](vowel+"is not a vowel");
}
}
}

Output:

Enter any alphabet : a

a is a vowel

[Link] a Program to print day according to the number.

Solution:

import [Link].*;
import [Link];

class print_day_according_to_number
{
public static void main(String[] args) {
//program start
Scanner sc = new Scanner([Link]);
[Link]("enter a no : ");
int day = [Link]();

switch (day)
{
case 1:
[Link]("Monday");
break;

case 2:
[Link]("Tuesday");
break;

case 3:
[Link]("Wednesday");
break;

case 4:
[Link]("Thursday");
break;

case 5:
[Link]("Friday");
break;

case 6:
[Link]("Saturday");
break;

case 7:
[Link]("Sunday");
break;

default:
[Link]("Enter a Valid no.");

}
}

Output:

enter a no : 5
Friday

[Link] a program to print 10 numbers using for loop.

Solution:

import [Link].*;
class print_10_numbers_using_for_loop
{
public static void main(String[] args) {
//Program start
for (int i = 0; i < 10; i++) {
[Link](i);
}
}
}

Output:

[Link] a program to print “hello world” 3 times using for loop.

Solution:

import [Link].*;
class print_hello_world_3_times_using_for_loop
{
public static void main(String[] args) {
//program start
for (int i = 0; i < 3; i++) {
[Link]("Hello World.");
}
//program end
}
}

Output:

Hello World.

Hello World.

Hello World.

[Link] a program to print first 10 even numbers using for loop.

Solution:

import [Link].*;
class print_first_10_even_numbers_using_for_loop
{
public static void main(String[] args) {
//program start
for (int i = 0; i < 18; i+=2) {
[Link](i);
}
//program end
}
}

Output:

10

12

14

16

[Link] a program to print first 10 odd numbers using for loop.

Solution:
import [Link].*;
class print_first_10_odd_numbers_using_for_loop
{
public static void main(String[] args) {
//program start
for (int i = 1; i < 19; i+=2) {
[Link](i);
}
//program end
}
}

Output:

11

13

15

17

You might also like