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

Class 12

Uploaded by

M
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Class 12

Uploaded by

M
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

Q) Write a java program to display square of a given number?

input:
5

output:
25

ex:

import java.util.Scanner;
class Example3
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);

System.out.println("Enter the number :");


int n=sc.nextInt(); //5

//logic
int square=n*n;

System.out.println("square of a given number is ="+square);


}
}

Q) Write a java program to display cube of a given number?

input:
5

output:
125

ex:
---
import java.util.Scanner;
class Example4
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);

System.out.println("Enter the number :");


int n=sc.nextInt(); //5

//logic
int cube=n*n*n;

System.out.println("cube of a given number is ="+cube);


}
}

Q) Write a java program to perform area of a circle?

input:
r = 5
output:
78.5

ex:
---
import java.util.Scanner;
class Example5
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);

System.out.println("Enter the radius :");


int r=sc.nextInt();

//logic
float area=3.14f*r*r;

System.out.println("area of a circle is ="+area);


}
}

Q) Write a java program to perform perimeter of a circle?

import java.util.Scanner;
class Example6
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);

System.out.println("Enter the radius :");


int r=sc.nextInt();

//logic
float perimeter=2*3.14f*r;

System.out.println("perimeter of a circle is ="+perimeter);


}
}

Q) Write a java program to perform swapping of two numbers?

output:
Before swapping a = 10 and b = 20

After swapping a = 20 and b = 10

ex:
import java.util.Scanner;
class Example7
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the first number :");
int a=sc.nextInt(); //10

System.out.println("Enter the second number :");


int b=sc.nextInt(); //20

System.out.println("Before swapping a = "+a+" and b = "+b);

//logic
int temp=a;
a=b;
b=temp;

System.out.println("After swapping a = "+a+" and b = "+b);

}
}

Q) Write a java program to perform swapping of two numbers without using third
variable?

import java.util.Scanner;
class Example8
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);

System.out.println("Enter the first number :");


int a=sc.nextInt(); //10

System.out.println("Enter the second number :");


int b=sc.nextInt(); //20

System.out.println("Before swapping a = "+a+" and b = "+b);

//logic
a = a + b;
b = a - b;
a = a - b;

System.out.println("After swapping a = "+a+" and b = "+b);

}
}

Q) Write a java program to find out 10% of TDS from given salary?

import java.util.Scanner;
class Example9
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the salary :");
int salary=sc.nextInt();

float tax=(float)salary*10/100;

System.out.println("10 percent of TDS is ="+tax);


}
}

Q) Write a java program to convert CGPA to percentage?

import java.util.Scanner;
class Example10
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);

System.out.println("Enter the CGPA :");


float cgpa=sc.nextFloat();

float percent=cgpa*9.5f;

System.out.println("CGPA to percentage is ="+percent);


}
}

Assignment
==========
Q) Write a java program to accept six marks of a student then find out total and
average?

Typecasting in Java
====================
The process of converting from one datatype to another datatype is called
typecasting.

In java, typecasting can be done in two ways.

1) Implicit typecasting

2) Explicit typecasting

1) Implicit typecasting
-----------------------
If we want to store small value into a bigger variable then we need to use implicit
typecasting.

A compiler is responsible to perform implicit typecasting.

There is no possibility to loss the information.

It is also known as Widening or Upcasting.

We can perform implicit typecasting as follow.

ex:
byte --> short
-->
int --> long --> float --> double
-->
char

ex:
---

class Test
{
public static void main(String[] args)
{
byte b=10;

int i=b;

System.out.println(i);//10
}
}

ex:
---

class Test
{
public static void main(String[] args)
{
char ch='a';

long l=ch;

System.out.println(l);//97
}
}

ex:
---

class Test
{
public static void main(String[] args)
{
int i=10;

double d=i;

System.out.println(d);//10.0
}
}

1) Explicit typecasting
-----------------------
If we want to store big value into a small variable then we need to use explicit
typecasting.

A programmer is responsible to perform implicit typecasting.


There is a possibility to loss the information.

It is also known as Norrowing or Downcasting.

We can perform explicity typecasting as follow.

ex:
byte <-- short
<--
int <-- long <-- float <-- double
<--
char

You might also like