Thursday, 5 November 2020
JAVA program to input and print n number using array.
JAVA program to input and print n number using array :-
PROGRAM :-
import java.io.*;
class Clsarray
{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new inputStreamReader(System.in));
int i, n;
int a[] = new int[100];
System.out.print("Enter the value of n = ");
n = Integer.parselnt(br.readLine());
System.out.println("Enter the values");
for(i =0; i<n; i++)
{
a[i] = Integer.parselnt(br.readLine());
}
System.out.println("The values are");
for(i =0; i<n; i++
{
System,out.print(a[i] +" ");
}
}
}
OUTPUT :-
Enter the value of n =5
Enter the values
10
20
30
40
50
The values are
10 20 30 40 50
The Type Promotion Rules in JAVA
The Type Promotion Rules :-
Java defines several type promotion rules that apply to expressions. They are as follows : all byte , short , and char values are promoted ti int. Then , if one operand is a long , the whole expression is promoted to long .If one operand is a float , the entire expression is promoted to float. If any of the operands is double. the result is double.
The following program demonstrates how each value in the expression gets promoted to match the second argument to each binary operation :
Program :-
class Promote
{
public static void main(String args[])
{
byte b = 42;
char c = 'a';
short s = 1024;
int i = 50000;
float f = 5.67f;
double d = .1234;
double result = (f * b) +(i / c) -(d*s);
System.out.println((f * b) + "+" +(i /c) + "-" +(d *s));
System.out.println("result =" +result);
}
}
OUTPUT:-
238.14 +515 -126.3616
result =626.7784146484375
The type promotions that occur in following line from program :-
double result = (f *b) + (i / c) - (d * s);
In the first subexpression, f *b , b is promoted to a float and the result of the subexpression is float. Next , in the subexpression i /c , c is promoted to int , and the result is of type int . Then , in d* s, the value of s is promoted to double, and the type of the subexpression is double.
Finally , these three intermediate values, float , int , and double are considered. The outcome of float plus an int is a float. Then the resultant float minus the last double is promoted to double, which is the type for the final result of the expression.
Enhanced for loop in JAVA
Enhanced for loop in JAVA
As of Java 5, the enhanced for loop was introduced.This is mainly used for arrays.
The syntax of enhanced for loop is :-
for(declaration : expression)
{
// Statement
}
Declaration : -
The newly declared block variable , Which is of a type compatible with the elements of the array we are accessing . The variable will be available within the for block and its value would be the same as the current array element .
Expression :-
This evaluates to the array we need to loop through. The expression can be an array variable or method call that returns an array .
EXAMPLE :-
public class Test
{
public static void main(String args[])
{
int [] numbers ={10,20,30,40,50}
for(int x :numbers)
{
System.out.print(x);
System.out.print(",");
}
System.out.print("\n");
String [] names ={"Sanika","Chahat","Ritesh","Simran"};
for(String name :names)
{
System.out.print(name);
System .out.print(",");
}
}
}
OUTPUT :-
10 , 20 , 30 , 40 , 50 ,
Sanika , Chahat , Ritesh , Simran ,
Wednesday, 4 November 2020
The for Loop in JAVA
The for Loop :-
A for loop is a repetition control structure that allows us to efficiently write a loop that needs to execute a specific number of times. A for loop is useful when we know how many times a task is to be repeated.
The syntax of a for loop is :-
for (initialization; Boolean _expression ;update)
{
//Statements
}
Here is the flow of control in a for loop :-
The initialization step is executed first, and only once .This step allow us to declare and initialize any loop control variables. We are not required to put a statement here, as long as a semicolon appears . Next , the Boolean expression is evaluated . if it is true , the body of is executed. if it is false, the body of the loop does not execute and flow of control jumps to the next statement past the for loop .
After the body of for loop executes, the flow of control jumps back up to the update statement . This statement allow us to update any loop control variables.
This statement can be left blank, as long as a semicolon appears after the Boolean expression . The Boolean expression is now evaluated again . If it is true , the loop executes and the process repeats itself (body of loop , then update step, then Boolean expression ). after the Boolean expression is false , the for loop terminates.
EXAMPLE :-
public class Test
{
public static void main(String args[])
{
for(int x =10; x<15; x = x+1)
{
System .out.print("value of x :" +x);
System .out.print("\n");
}
}
}
OUTPUT :-
value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
The do......while Loop in JAVA
The do......while Loop :-
A do.....while loop is similar to a while loop , except that a do .....while loop is guaranteed to execute at least one time.
The syntax of a do.....while loop is :-
do
{
//Statements
}
while(Boolean_expression);
Notice that the Boolean expression appears at the end of loop , so the statement in the loop execute once before the Boolean is tested. If the Boolean expression is true, The flow of control jumps back up to do , and the statement in the loop execute again.This process repeats until the Boolean expression is false.
EXAMPLE :-
public class Test
{
public static void main(String args[])
{
int x =10;
do
{
System.out.print("value of x :" +x);
x++;
System.out.print("\n");
}
while(x <15);
}
}
OUTPUT :-
value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
The while Loop in JAVA
The while Loop in JAVA
A while loop is a control structure that allows us to repeat a task a certain number of times.
The syntax of a while loop is :-
while(boolean_expression)
{
//Statements
}
When executing , if the boolean _expression result is true, then the action inside the loop will be executed. This will continue as long as the expression result is true.Here key point of the while loop is that the loop might not ever run . When the expression is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.
EXAMPLE :-
public class Test
{
public static void main(String args[])
{
int x =10;
while(x<15)
{
System .out.print("value of x :"+x);
x++;
System.out.print("\n");
}
}
}
OUTPUT :-
value of x :10
value of x: 11
value of x: 12
value of x: 13
value of x: 14
Subscribe to:
Comments (Atom)
Student Marks Calculation app using java
import javax.swing. *; import java.awt.event.ActionEvent ; import java.awt.event.ActionListener ; public class Student { private J...
-
The Type Promotion Rules :- Java defines several type promotion rules that apply to expressions. They are as follows : all byt...
-
import javax.swing. *; import java.awt.event.ActionEvent ; import java.awt.event.ActionListener ; public class Student { private J...
-
Features of Java Java Buzzwords The following are the list of buzzwords of Java:- 1. Simple 2. Secure 3. Portable 4...