Thursday, February 2, 2023

Convert a decimal number to hexadecimal number

Write a Java program to convert a decimal number to hexadecimal number.

Decimal number: The decimal numeral system is the standard system for denoting integer and non-integer numbers. It is also called base-ten positional numeral system.

Hexadecimal number: Hexadecimal is a positional numeral system with a radix, or base, of 16. It uses sixteen distinct symbols, most often the symbols 0-9 to represent values zero to nine, and A, B, C, D, E, F (or alternatively a, b, c, d, e, f) to represent values ten to fifteen.

Test Data:
Input a decimal number: 15

Java Code:

import java.util.Scanner;

public class Exercise20 {

      public static void main(String args[])

    {

        int dec_num, rem;

        String hexdec_num="";

        /* hexadecimal number digits */       

        char hex[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};       

        Scanner in = new Scanner(System.in);         

        System.out.print("Input a decimal number: ");

        dec_num = in.nextInt();      

        while(dec_num>0)

        {

            rem = dec_num%16;

            hexdec_num = hex[rem] + hexdec_num;

            dec_num = dec_num/16;

        }

        System.out.print("Hexadecimal number is : "+hexdec_num+"\n");        

    }

}

Output:

Input a decimal number: 15                                                                                    

Hexadecimal number is : F

 

Convert a decimal number to binary number

Write a Java program to convert a decimal number to binary number.

Decimal number: The decimal numeral system is the standard system for indicating integer and non-integer numbers. It is also called base-ten positional numeral system.

Binary number: In digital electronics and mathematics, a binary number is a number expressed in the base-2 numeral system or binary numeral system. This system uses only two symbols: typically 1 (one) and 0 (zero).

Test Data:
Input a Decimal Number : 5

Java Code:

import java.util.Scanner;

public class Exercise19 {

      public static void main(String args[])

    {

        int dec_num, quot, i=1, j;

        int bin_num[] = new int[100];

        Scanner scan = new Scanner(System.in);  

        System.out.print("Input a Decimal Number : ");

        dec_num = scan.nextInt();

        quot = dec_num;

        while(quot != 0)

        {

            bin_num[i++] = quot%2;

            quot = quot/2;

        }

        System.out.print("Binary number is: ");

        for(j=i-1; j>0; j--)

        {

            System.out.print(bin_num[j]);

        }

        System.out.print("\n");

    }

}

Output:

Input a Decimal Number : 5                                                                                    

Binary number is: 101

Find the area and perimeter of a circle

Write a Java program to print the area and perimeter of a circle.

In geometry, the area enclosed by a circle of radius r is πr2. Here the Greek letter π represents a constant, approximately equal to 3.14159, which is equal to the ratio of the circumference of any circle to its diameter.

The circumference of a circle is the linear distance around its edge.

Pictorial Representation:

Given Solution:

Java Code:

public class Exercise11 {

   private static final double radius = 7.5;

    public static void main(String[] args) {

        double perimeter = 2 * Math.PI * radius;

        double area = Math.PI * radius * radius;

        System.out.println("Perimeter is = " + perimeter);

        System.out.println("Area is = " + area);

    }

}

Output:

Perimeter is = 47.12388980384689                                                                             

Area is = 176.71458676442586

Wednesday, February 1, 2023

Find the sum (addition), multiply, subtract, divide and remainder of two numbers

Write a Java program to print the sum (addition), multiply, subtract, divide and remainder of two numbers.

Test Data:
Input first number: 125
Input second number: 24

Given Solution:

The program follows he basic arithmetic operations formula. First both the numbers are taken as input from the user. Then all the formulas are applied in coding one by one and the result is printed on the screen.

Java Code:

import java.util.Scanner;

public class Exercise1 {

 public static void main(String[] args) {

  Scanner in = new Scanner(System.in);

  System.out.print("Input first number: ");

  int num1 = in.nextInt();

  System.out.print("Input second number: ");

  int num2 = in.nextInt();

  System.out.println(num1 + " + " + num2 + " = " + (num1 + num2));

  System.out.println(num1 + " - " + num2 + " = " + (num1 - num2));

  System.out.println(num1 + " x " + num2 + " = " + (num1 * num2));

  System.out.println(num1 + " / " + num2 + " = " + (num1 / num2));

  System.out.println(num1 + " mod " + num2 + " = " + (num1 % num2));

 }

}

Resulting Output:

Input first number: 125                                                                                        

Input second number: 24                                                                                       

125 + 24 = 149

125 - 24 = 101

125 x 24 = 3000

125 / 24 = 5

125 mod 24 = 5

Calculate the volume of a cylinder

Write a program in C++ to calculate the volume of a cylinder.

What is a cylinder?

There are many types of cylinders. This page describes the simplest shape, where a cylinder looks like a tube or can of soup, with two equally sized parallel circles at each end.

Cylinder Terminology

To calculate the volume of a cylinder, first need to understand some terms.

Radius - The radius is the distance from the center of the end circle to                  the end.

Pi - Pi is a special number used in circles. We use a                                        shortened version of Pi = 3.14. We also use the symbol π to refer             to the number pi in the equation.

HeightCylinder height or length.

Volume of a Cylinder

There is a special formula for finding the volume of a cylinder. Volume indicates the amount of space occupied by the interior of the cylinder. Answers to questions about volume are always in cubic units.

Volume = πr2h

This is the same as 3.14 x radius x radius x height.

Sample Solution:

C++ Code :

#include <iostream>

using namespace std; 

    int main()

    {

    int rad1,hgt;

    float volcy;

     cout << "\n\n Calculate the volume of a cylinder :\n";

     cout << "-----------------------------------------\n";     

     cout<<" Input the radius of the cylinder : ";

     cin>>rad1;

    cout<<" Input the height of the cylinder : ";

    cin>>hgt;

    volcy=(3.14*rad1*rad1*hgt);

    cout<<" The volume of a cylinder is : "<< volcy << endl;

     cout << endl;

     return 0;

    }

Sample Output:

Calculate the volume of a cylinder :                                 

-----------------------------------------                             

 Input the radius of the cylinder : 6                                 

 Input the height of the cylinder : 8                                 

 The volume of a cylinder is : 904.32

 

Popular Posts