Programmator
Programmator
  • Home
  • Download
  • Social
    • YouTube
    • Instagram
    • Facebook
    • Telegram
  • Features
    • C/C++
    • Java
  • Contact Us

Write a java program to swap two numbers

In this program, you will learn the technique of swapping two numbers using temporary variable in java programming language.  

Exchanging the values stored in two variables is known as swapping. There are multiple ways to swap the values of two variables. It can be swapped using a temporary variable or simple or simple mathematical operations such as addition and subtraction or multiplication and division or bitwise XOR can also be used to perform the swap operation.

In the below program, two numbers 2.60f and 4.44f which are to be swapped are stored in variables: first and second respectively.

The variable is printed before swapping using println() to see the results clearly after swapping is done. Below given steps are followed swapping process. After completing swapping process and the variables are printed on the screen.

Remember, the only use of temporary is hold the value of first before swapping. You can also swap the numbers without using temporary.

  • Program:

public class SwapNumbers {

    public static void main(String[] args) {

        float first = 2.60f, sec = 4.44f;

        System.out.println("--Before swapping the numbers--");
        System.out.println("First number = " + first);
        System.out.println("Second number = " + sec);

        // Value of first is assigned to temporary
        float temporary = first;

        // Value of sec is assigned to first
        first = sec;

        // Value of temporary (which contains the initial value of first) is assigned to sec 
        sec = temporary;

        System.out.println("--After swapping the numbers--");
        System.out.println("First number = " + first);
        System.out.println("Second number = " + sec);
    }
}

  • Output:

--Before swapping the numbers--
First number = 2.6
Second number = 4.44
--After swapping the numbers--
First number = 4.44
Second number = 2.6

  • Step:

Below are the simple steps we follow:
  1. Assign first to a temporary variable: temporary = first
  2. Assign second to first: first = second
  3. Assign temp to second: second = temporary

Let us understand with simple example,

first = 111, second = 222
After line 1: temporary = first
temporary = 111
After line 2: first = second
first = 222
After line 3: second = temporary
second = 222

  • Visit:

Java program to print an Integer Here
Java program to add two integers Here
Java program to find ASCII value of a character Here
Java program to compute quotient and remainder Here

Write a java program to compute quotient and remainder

In this program, the quotient and remainder find in java programming language.

The different parts of division are dividend, divisor, quotient and remainder. Quotient can be defined as the resultant of the division. Formula for quotient is Quotient = Dividend/Divisor. In division, a number is divided by another number to get the output in the form of another number. The dividend is the number that is getting divided, and the divisor is the number that divides the given number. In fraction, the numerator is dividend, denominator is divisor, and when numerator is divided by the denominator, the result will be quotient. For, instance, suppose we have to divide 20(dividend) by 4(divisor); we will get 5, which is the quotient.

Remainder is defined as a value that is left after division. As an instance, if we divide 25 with 4, we will get 1 as a remainder. If the number divides another number completely, the remainder will be 0.
Remainder is always smaller than the divisor, if it is large, that means the division is not completed. But remainder can be less or greater than the quotient; for instance, if we divide 41 by 7, we will get 5 as a quotient, and 6 as a remainder which is less than divisor greater than the quotient. we know the method of Division = Divisor x Quotient + Remainder. so, the formula for remainder will be Remainder = Dividend - (Divisor x Quotient)

Here, we have created two variables dividend and divisor. we are calculating the quotient and remainder by dividing 55 by 6.
To find the quotient, we have used the / operator, we have divided dividend (55) by divisor (4). Since both dividend and divisor are integers, the result will also be integer.
55 / 6 // result 8(integer)
Likewise, to find the remainder we use the % operator. Here, the dividend is divided by the divisor and the remainder is returned by the % oprtator.
55 % 6 // result 1
finally, quotient and remainder are printed on the screen using println() function.

  • Program:

public class QuotientRemainder {

  public static void main(String[] args) {

    int dividend = 55, divisor = 6;

    int quotient = dividend / divisor;
    int remainder = dividend % divisor;

    System.out.println("Quotient = " + quotient);
    System.out.println("Remainder = " + remainder);
  }
}

  • Output:

Quotient = 9
Remainder = 1

  • Visit:

Java program to print an Integer Here
Java program to add two integers Here
Java program to find ASCII value of a character Here

Write a java program to find ASCII value of a character

In this program, find and display the ASCII value of a character in java using typecasting and normal variable assignment operations.

Here, character n is stored in a char variable ch. Like, single quotes (' ') are used to declare characters, we use double quotes (" ") are used to declare strings.

Now, to find the ASCII value of ch, we just assign ch to an int variable ASCII. Internally, Java converts the characters value to an ASCII value.

We can also cast the character ch to an integer using int. In simple terms, casting is converting variable from one type to another, here char variable ch is converted to an int variable castAscii

Last, we print the ascii value using the println() function.

  • Program:


public class AsciiValue {

    public static void main(String[] args) {

        char ch = 'n';
        int ASCII = ch;
        // You can also cast char to int
        int castAscii = (int) ch;

        System.out.println("The ASCII value of " + ch + " is: " + ASCII);
        System.out.println("The ASCII value of " + ch + " is: " + castAscii);
    }
}

  • Output:


The ASCII value of a is: 110
The ASCII value of a is: 110

  • Visit:

Java program to print an Integer Here
Java program to add two integers Here

Java program to add two Integers

In this program, two integers 11 and 12 are stored in integer variables first and second respectively.

Then, first and second are added using the + operator, and its result is stored in another variable sum.

Finally, sum is printed on the screen using println() function.

New YouTube video:


  •  Program:

class Main {

  public static void main(String[] args) {
    
    System.out.println("Enter two numbers");
    int first = 11;
    int second = 12;
    
    System.out.println(first + " " + second);

    // add two numbers
    int sum = first + second;
    System.out.println("The sum is: " + sum);
  }
}

  •  Output:

Enter two numbers
11 12
The sum is: 23

  •  Visit:
Write a Java program to print an Integer Here
How to print an integer entered by an user

In this program, an object of scanner class, reader is created to take input from standard input, Which is keyboard.

Then, Enter a number prompt is printed to give the user a visual cue as to what they should do next.

reader.nextInt() the reads all entered integers from the keyword unless it encounters a new line character \n Enter. The entered integer are then saved to the integer variable number.

If you enter any character which is not an integer, the compiler will throw an InputMismatchException.

Finally, number is printed onto the standard output (system.out) - computer screen using the function println().

  •  Program:

import java.util.Scanner;

public class HelloWorld {

    public static void main(String[] args) {

        // Creates a reader instance which takes
        // input from standard input - keyboard
        Scanner reader = new Scanner(System.in);
        System.out.print("Enter a number: ");

        // nextInt() reads the next integer from the keyboard
        int number = reader.nextInt();

        // println() prints the following line to the output screen
        System.out.println("You entered: " + number);
    }
}

  •  Output:

Enter a number: 10
You entered: 10

  •  Visit:

Write a Java program to add two integers Here
Newer Posts Older Posts Home

ABOUT

I could look back at my life and get a good story out of it. It's a picture of somebody trying to figure things out.

SUBSCRIBE & FOLLOW

POPULAR POSTS

  • Write a C program to enter a distance in to kilometer and convert it in to meter, feet, inches and centimeter
  • Write a C program to computer Fahrenheit from centigrade ( f=1.8*c+32 )
  • Write a C program to interchange two numbers
  • Write a C program to read and store the roll no and marks of 20 students using array
  • Write a C program to evaluate the series sum=1-x+x^2/2!-x^3/3!+x^4/4!......-x^9/9!

Advertisement

👆 Shopping 👆

Powered by Blogger

Archive

  • February 20241
  • January 20242
  • November 20234
  • October 20236
  • September 20236
  • August 20235
  • April 20231
  • March 20231
  • October 20221
  • August 20223
  • July 20222
  • May 20223
  • April 20222
  • March 20221
  • January 20221
  • December 20216
  • November 20214
  • October 20211
  • September 20216
  • August 202127
  • July 20216

Report Abuse

Copyright

  • Home
  • About us
  • Privacy Policy
  • Disclaimer
  • Contact Us

About Me

Nilesh Patel
View my complete profile

Copyright © Programmator. Designed by OddThemes