Class 12
Class 12
input:
5
output:
25
ex:
import java.util.Scanner;
class Example3
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
//logic
int square=n*n;
input:
5
output:
125
ex:
---
import java.util.Scanner;
class Example4
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
//logic
int cube=n*n*n;
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);
//logic
float area=3.14f*r*r;
import java.util.Scanner;
class Example6
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
//logic
float perimeter=2*3.14f*r;
output:
Before swapping a = 10 and b = 20
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
//logic
int temp=a;
a=b;
b=temp;
}
}
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);
//logic
a = a + b;
b = a - b;
a = a - 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;
import java.util.Scanner;
class Example10
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
float percent=cgpa*9.5f;
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.
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.
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.
ex:
byte <-- short
<--
int <-- long <-- float <-- double
<--
char