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

Java Program To Find Largest of Three Numbers

The document contains code snippets from Java programs for various algorithms and problems: 1. A Java program that takes three numbers as input and prints which number is the largest. 2. A Java program that takes a number as input and prints whether it is even or odd. 3. A C program that implements bubble sort to sort an array of numbers in ascending order. 4. A Java program that swaps two numbers without using a temporary variable.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
167 views

Java Program To Find Largest of Three Numbers

The document contains code snippets from Java programs for various algorithms and problems: 1. A Java program that takes three numbers as input and prints which number is the largest. 2. A Java program that takes a number as input and prints whether it is even or odd. 3. A C program that implements bubble sort to sort an array of numbers in ascending order. 4. A Java program that swaps two numbers without using a temporary variable.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Java program to find largest of three numbers

1. import java.util.Scanner;
2.
3. class Largest
4. {
5. public static void main(String args[])
6. {
7. int x, y, z;
8. System.out.println("Enter three integers");
9. Scanner in = new Scanner(System.in);
10.
11. x = in.nextInt();
12. y = in.nextInt();
13. z = in.nextInt();
14.
15. if (x > y && x > z)
16. System.out.println("First number is largest.");
17. else if (y > x && y > z)
18. System.out.println("Second number is largest.");
19. else if (z > x && z > y)
20. System.out.println("Third number is largest.");
21. else
22. System.out.println("The numbers are not distinct.");
23. }
24. }

Java program to find odd or even


1. import java.util.Scanner;
2. class OddOrEven
3. {
4. public static void main(String args[])
5. {
6. int x;
7. System.out.println("Enter an integer to check if it is odd
or even");
8. Scanner in = new Scanner(System.in);
9. x = in.nextInt();
10.
11. if (x % 2 == 0)
12. System.out.println("The number is even.");
13. else
14. System.out.println("The number is odd.");
15. }
16. }
Bubble sort
1. #include<stdio.h>
2. void main ()
3. {
4. int i, j,temp;
5. int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
6. for(i = 0; i<10; i++)
7. {
8. for(j = i+1; j<10; j++)
9. {
10. if(a[j] > a[i])
11. {
12. temp = a[i];
13. a[i] = a[j];
14. a[j] = temp;
15. } } }
16. printf("Printing Sorted Element List ...\n");
17. for(i = 0; i<10; i++)
18. {
19. printf("%d\n",a[i]);
20. } }

swap two numbers without using a temporary


variable?
import java.*;

class Geeks {

public static void main(String a[])


{
int x = 10;
int y = 5;
x = x + y;
y = x - y;
x = x - y;
System.out.println("After swaping:"
+ " x = " + x + ", y = " + y);
}
}
Program To Check Given Number Is Prime Or Not
public class PrimeNumbers {

public static void main(String[] args) {

int num = 20, count;

for (int i = 1; i & lt; = num; i++) {


count = 0;
for (int j = 2; j & lt; = i / 2; j++) {
if (i % j == 0) {
count++;
break;
}}

if (count == 0) {
System.out.println(i);
}}}}

Fibonacci Series in Java


1. class FibonacciExample1{
2. public static void main(String args[])
3. {
4. int n1=0,n2=1,n3,i,count=10;
5. System.out.print(n1+" "+n2);//printing 0 and 1
6. for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already printed
7. {
8. n3=n1+n2;
9. System.out.print(" "+n3);
10. n1=n2;
11. n2=n3;
12. }}}
Palindrome String in Java
class ChkPalindrome
{
public static void main(String args[])
{
String str, rev = "";
Scanner sc = new Scanner(System.in);

System.out.println("Enter a string:");
str = sc.nextLine();

int length = str.length();


for ( int i = length - 1; i >= 0; i-- )
rev = rev + str.charAt(i);

if (str.equals(rev))
System.out.println(str+" is a palindrome");
else
System.out.println(str+" is not a palindrome");
}}

Java program to find factorial

1. class FactorialExample{
2. public static void main(String args[]){
3. int i,fact=1;
4. int number=5;//It is the number to calculate factorial
5. for(i=1;i<=number;i++){
6. fact=fact*i;
7. }
8. System.out.println("Factorial of "+number+" is: "+fact);
9. }
10. }
Add two numbers without using arithmetic operators

class GFG {
static int Add(int x, int y)
{
// Iterate till there is no carry
while (y != 0)
{
// carry now contains common // set bits of x and y
int carry = x & y;
// Sum of bits of x and // y where at least one // of the bits is not
set
x = x ^ y;
// Carry is shifted by // one so that adding it // to x gives the
required sum y = carry << 1;
}
return x;
}
// Driver code
public static void main(String arg[])
{
System.out.println(Add(15, 32));
}
}

Subtract two numbers without using arithmetic operators

class GFG
{
static int subtract(int x, int y)
{
while (y != 0)
{
int borrow = (~x) & y;
x = x ^ y;
y = borrow << 1;
}
return x; }
// Driver Code

public static void main (String[] args)


{
int x = 29, y = 13;
System.out.println("x - y is " + subtract(x, y)); }}
Swapping using temporary or third variable
1. import java.util.Scanner;
2.
3. class SwapNumbers
4. {
5. public static void main(String args[]){
6. int x, y, temp;
7. System.out.println("Enter x and y");
8. Scanner in = new Scanner(System.in);
9. x = in.nextInt();
10. y = in.nextInt();
11.
12. System.out.println("Before Swapping\nx =
"+x+"\ny = "+y);
13.
14. temp = x;
15. x = y;
16. y = temp;
17.
18. System.out.println("After Swapping\nx =
"+x+"\ny = "+y);
19. }
20. }

java program to add two numbers


1. class AddNumbers
2. { public static void main(String args[]) {
3. int x, y, z;
4. System.out.println("Enter two integers to
calculate their sum");
5. Scanner in = new Scanner(System.in);
6. x = in.nextInt();
7. y = in.nextInt();
8. z = x + y;
9. System.out.println("Sum of the integers =
" + z); }}
10.
Java programming code to reverse a string
1. import java.util.*;
2. class ReverseString
3. {
4. public static void main(String args[])
5. {
6. String original, reverse = "";
7. Scanner in = new Scanner(System.in);
8.
9. System.out.println("Enter a string to reverse");
10. original = in.nextLine();
11.
12. int length = original.length();
13.
14. for (int i = length - 1 ; i >= 0 ; i--)
15. reverse = reverse + original.charAt(i);
16. System.out.println("Reverse of the string: " + reverse);
17. }}

c program to add two numbers using pointers


1. #include <stdio.h>
2.
3. int main()
4. {
5. int first, second, *p, *q, sum;
6.
7. printf("Enter two integers to add\n");
8. scanf("%d%d", &first, &second);
9.
10. p = &first;
11. q = &second;
12.
13. sum = *p + *q;
14.
15. printf("Sum of the numbers = %d\n", sum);
16.
17. return 0;
18. }
19. *
20. * *
21. * * *
22. * * * *
23. * * * * *

public class Pattern {

public static void main(String[] args) {

int rows = 5;

for(int i = 1; i <= rows; ++i) {

for(int j = 1; j <= i; ++j) {

System.out.print("* "); }

System.out.println(); }}

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
public class Pattern {

public static void main(String[] args) {

int rows = 5;

for(int i = 1; i <= rows; ++i) {

for(int j = 1; j <= i; ++j) {

System.out.print(j + " ");

}System.out.println();

} } }
* * * * *
* * * *
* * *
* *
*

public class Pattern {

public static void main(String[] args) {


int rows = 5;

for(int i = rows; i >= 1; --i) {


for(int j = 1; j <= i; ++j) {
System.out.print("* ");
}
System.out.println();
}
}
}

*
* * *
* * * * *
* * * * * * *
* * * * * * * * *

public class Pattern {

public static void main(String[] args) {


int rows = 5, k = 0;

for(int i = 1; i <= rows; ++i, k = 0) {


for(int space = 1; space <= rows - i; ++space) {
System.out.print(" ");
}

while(k != 2 * i - 1) {
System.out.print("* ");
++k;
}

System.out.println();
}
} }
1
2 3
4 5 6
7 8 9 10

public class Pattern {

public static void main(String[] args) {


int rows = 4, number = 1;

for(int i = 1; i <= rows; i++) {

for(int j = 1; j <= i; j++) {


System.out.print(number + " ");
++number;
}

System.out.println();
}
}
}

GCD of two numbers using for loop and if statement


public class GCD {

public static void main(String[] args) {

int n1 = 81, n2 = 153, gcd = 1;

for(int i = 1; i <= n1 && i <= n2; ++i)


{
// Checks if i is factor of both integers
if(n1 % i==0 && n2 % i==0)
gcd = i;
}

System.out.printf("G.C.D of %d and %d is %d", n1, n2, gcd);


}
}

You might also like