0% found this document useful (0 votes)
9 views10 pages

Java Programs for Basic Operations

The document provides Java programming examples for various tasks, including swapping two numbers, finding the maximum of two or three numbers, checking if a number is even or odd, calculating the sum of natural numbers, and finding the factorial of a number. Each task includes an algorithm and implementation using methods such as loops, recursion, and the Math library. The document also contains sample outputs for user inputs.

Uploaded by

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

Java Programs for Basic Operations

The document provides Java programming examples for various tasks, including swapping two numbers, finding the maximum of two or three numbers, checking if a number is even or odd, calculating the sum of natural numbers, and finding the factorial of a number. Each task includes an algorithm and implementation using methods such as loops, recursion, and the Math library. The document also contains sample outputs for user inputs.

Uploaded by

Rehan Hussain
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

DURGASOFT

public static void main(String[] args)


{
Scanner obj = new Scanner([Link]);
[Link]("Enter a value");
int a = [Link]();
[Link]("Enter b value");
int b = [Link]();
[Link](a,b);
}
}

C:\prakashclasses>javac [Link]

C:\prakashclasses>java Test
Enter a value
11
Enter b value
22
before swaping a=11 and b=22
after swaping a=22 and b=11

C:\prakashclasses>java Test
Enter a value
-2
Enter b value
-3
before swaping a=-2 and b=-3
after swaping a=-3 and b=-2

Logic5: without using temp variable by using single line


------------------------------------------------------------
==> print a and b values
==> by using single line

a = (a+b) - (b=a);

==> print a and b value

Implementation:
---------------
import [Link].*;

class Demo
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
11  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
{
static void swap(int a,int b)
{
[Link]("before swaping a="+a+" and b="+b);
a = a+b-(b=a);
[Link]("after swaping a="+a+" and b="+b);
}
}

class Test
{
public static void main(String[] args)
{
Scanner obj = new Scanner([Link]);
[Link]("Enter a value");
int a = [Link]();
[Link]("Enter b value");
int b = [Link]();
[Link](a,b);
}
}

C:\prakashclasses>javac [Link]

C:\prakashclasses>java Test
Enter a value
12
Enter b value
13
before swaping a=12 and b=13
after swaping a=13 and b=12

DESIGN A PROGRAM TO FIND MAX OF TWO NUMBERS:


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Algorithm:
----------

1. read two numbers from the user as a and b

2. apply logic

DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,


12  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
version1:
conditional operator

(condition)?tb:fb

(a>b)?a:b
version2:
[Link](a,b)

3. print the result

can we check how the [Link] logic implemented internally --> abstraction

Implementation:
---------------
import [Link].*;

class Demo
{
static int max_version1(int a,int b)
{
//manual code
return (a>b)?a:b;
}

static int max_version2(int a,int b)


{
//predefined lib's
return [Link](a,b);
}
}

class Test
{
public static void main(String[] args)
{
Scanner obj = new Scanner([Link]);
[Link]("Enter a value");
int a = [Link]();
[Link]("Enter b value");
int b = [Link]();
[Link]("max value from version1=
"+Demo.max_version1(a,b));
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
13  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
[Link]("max value from version2=
"+Demo.max_version2(a,b));
}
}

DESIGN A PROGRAM TO FIND WHETHER THE GIVEN NUMBER IS EVEN OR ODD:


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Algorithm:
----------

1. read number 'n' value from user


2. if n is divisible by 2 means 'even' else 'odd'

if(n%2==0)
print even
else
print odd
Implementation:
---------------
import [Link].*;

class Demo
{
static String check_evenorodd(int n)
{
//manual code
return (n%2==0)?"EVEN NUMBER":"ODD NUMBER";
}

class Test
{
public static void main(String[] args)
{
Scanner obj = new Scanner([Link]);
[Link]("Enter n value");
int n = [Link]();
[Link](Demo.check_evenorodd(n));
}
}

C:\prakashclasses>javac [Link]
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
14  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
C:\prakashclasses>java Test
Enter n value
34
EVEN NUMBER

C:\prakashclasses>java Test
Enter n value
55
ODD NUMBER

DESIGN A PROGRAM TO FIND SUM OF 'N' NATURAL NUMBERS:


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Algorithm:
----------
1. read 'n' value from the user.
2. apply the logic to find sum of n natural numbers

Logic1:
sum=0;
for(i=1;i<=n;i++)
{
sum=sum+i;
}

Logic2:
int sum(int n)
{
if(n==0)
return 1;
else
return n+sum(n-1);
}
Logic3:
math formula ---> n*(n+1)/2
3. print result on the screen

Implementation:
---------------
import [Link].*;

class Demo
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
15  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
{
static int sumofn_v1(int n)
{
//for loop
int sum=0;
for(int i=1;i<=n;i++)
{
sum=sum+i;
}
return sum;
}

static int sumofn_v2(int n)


{
//recursion
if(n==0)
return 0;
else
return n+sumofn_v2(n-1);
}

static int sumofn_v3(int n)


{
//math formula
return (n*(n+1))/2;
}
}

class Test
{
public static void main(String[] args)
{
Scanner obj = new Scanner([Link]);
[Link]("Enter n value");
int n = [Link]();
[Link](Demo.sumofn_v1(n));
[Link](Demo.sumofn_v2(n));
[Link](Demo.sumofn_v3(n));
}
}

C:\prakashclasses>javac [Link]
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
16  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
C:\prakashclasses>java Test
Enter n value
5
15
15
15

IMPLEMENT A PROGRAM TO READ THREE NUMBERS FROM THE USER AND CAL MAX
OF THREE NUMBERS
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~
Algorithm:

1. read a,b and c values from user.

2. apply the logic

logic1: by using manual method

(a>b && a>c)?a:((b>c)?b:c)

logic2: by using max method in math

[Link]([Link](number1,number2),number3)

3. print the result

Implementation:
---------------
import [Link].*;

class Demo
{
static int max1(int a,int b,int c)
{
return (a>b && a>c)?a:(b>c?b:c);
}

static int max2(int a,int b,int c)


{
return [Link]([Link](a,b),c);
}
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
17  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
}

class Test
{
public static void main(String[] args)
{
Scanner obj = new Scanner([Link]);
[Link]("Enter a value");
int a = [Link]();
[Link]("Enter b value");
int b = [Link]();
[Link]("Enter c value");
int c = [Link]();
[Link](Demo.max1(a,b,c));
[Link](Demo.max2(a,b,c));
}
}

C:\prakashclasses>javac [Link]

C:\prakashclasses>java Test
Enter a value
1
Enter b value
2
Enter c value
3
3
3

C:\prakashclasses>java Test
Enter a value
1
Enter b value
2
Enter c value
-3
2
2

C:\prakashclasses>java Test
Enter a value
1
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
18  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
Enter b value
-2
Enter c value
-3
1
1

IMPLEMENT A PROGRAM TO FIND FACTORIAL OF THE GIVEN NUMBER


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Algorithm:

1. read n value from the user.

2. apply any one of the following logic

logic1:
by using looping

f=1;
for(i=1;i<=n;i++)
{
f=f*i;
}
print f

logic2:

by using recursion

int fact(int n)
{
if(n==1)
return 1;
else
return n*fact(n-1);
}

3. print the result

IMPLEMENTATION:
---------------
class Demo
{
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
19  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
static int fact1(int n)
{
//loop
int i,f=1;
for(i=1;i<=n;i++)
{
f=f*i;
}
return f;
}

static int fact2(int n)


{
//recursion
if(n==1 || n==0)
return 1;
else
return n*fact2(n-1);
}
}

class Test
{
public static void main(String[] args)
{
[Link] obj = new [Link]([Link]);
[Link]("Enter n value");
int n = [Link]();
if(n>=0)
{
[Link]("factorial by using loop = "+Demo.fact1(n));
[Link]("factorial by using recursion = "+Demo.fact2(n));
}
else
[Link]("Arey what happend to you factorial for -ve
num not existed");
}
}

C:\prakashclasses>javac [Link]

C:\prakashclasses>java Test
Enter n value
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
20  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]

You might also like