0% found this document useful (0 votes)
13 views3 pages

Basic Programs

The document contains several basic Java programs demonstrating fundamental programming concepts. These include adding two numbers, calculating the area of a circle, finding simple and compound interest, converting Celsius to Fahrenheit, and swapping numbers without using a temporary variable. Each program is presented with code snippets and explanations of their functionality.

Uploaded by

Vamc Chowdary
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)
13 views3 pages

Basic Programs

The document contains several basic Java programs demonstrating fundamental programming concepts. These include adding two numbers, calculating the area of a circle, finding simple and compound interest, converting Celsius to Fahrenheit, and swapping numbers without using a temporary variable. Each program is presented with code snippets and explanations of their functionality.

Uploaded by

Vamc Chowdary
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
You are on page 1/ 3

Basic Programs

1. Program to Accept values of Two Numbers and print their Addition.


import java.util.Scanner;
class AddNumbers
{
public static void main(String args[])
{
int x, y, z;
System.out.print("Enter two integers to calculate their sum : ");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
z = x + y;
System.out.println("Sum of entered integers = " + z);
}
}
2. Program to Accept value of Radius and Calculate Area of Circle.

class CircleArea
{
public static void main(String[] args)
{
int radius = 3;
System.out.println("The radius of the circle is " + radius);
// NOTE : use Math.PI constant to get value of pi
double area = Math.PI * radius * radius;
System.out.println("Area of a circle is " + area);
}
}
3. Program to Find Simple Interest and Compound Interest.

import java.lang.*;
import java.util.Scanner;

class CalculateInterest
{
public static void main(String[] args)
{
double p, n, r, si, ci;

Scanner s = new Scanner(System.in);

System.out.print("Enter the amount : ");


p = s.nextDouble();
System.out.print("Enter the No.of years : ");
n = s.nextDouble();

System.out.print("Enter the Rate of interest : ");


r = s.nextDouble();

si = (p * n * r) / 100;
ci = p * Math.pow(1.0 + r / 100.0, n) - p;

System.out.println("\nAmount : " + p);


System.out.println("No. of years : " + n);
System.out.println("Rate of interest : " + r);
System.out.println("Simple Interest : " + si);
System.out.println("Compound Interest : " + ci);
}
}

4. Program to Convert Celsius to Fahrenheit.

import java.util.*;
class CelsiustoFahrenheit
{
public static void main(String[] args)
{
double temperature;
Scanner in = new Scanner(System.in);

System.out.println("Enter temperature in Celsius");


temperature = in.nextInt();

temperature = (temperature * 9 / 5.0) + 32;


System.out.println("Temperature in Fahrenheit = " + temperature);
}
}

5. Program to swap two numbers without using temporary variable.


public class Swapping
{
public static void main(String[] args)
{
int x = 100;
int y = 200;

//Swapping in steps
x = x + y; //x = 100 + 200 = 300
y = x - y; //y = 300 - 200 = 100
x = x - y; //x = 300 - 100 = 200

//Verify swapped values


System.out.println("x = " + x + " and y = " + y);
}
}

6. Program to swap two numbers without using temporary variable.


public class Main
{
static int num1, num2, num3;
public static void main(String[] args)
{
num1 = 30; num2 = 60; num3 = 90;
System.out.println("Before swapping: num1="+num1+",num2="+num2+",num3="+num3);
num1 = num1 + num2 + num3;
num2 = num1 - (num2 + num3);
num3 = num1 - (num2 + num3);
num1 = num1 - (num2 + num3);
System.out.println("After swapping: num1="+num1+",num2="+num2+",num3="+num3);
}
}

You might also like