0% found this document useful (0 votes)
19 views2 pages

2ab Java

The document provides Java programs demonstrating type casting and type conversion, specifically converting an integer to a byte and a double to an integer. It also includes a program that uses a for-each loop to compute the average of the first ten natural numbers. The output of both programs is displayed, showing the results of the conversions and the summation of the numbers.

Uploaded by

a70497944
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views2 pages

2ab Java

The document provides Java programs demonstrating type casting and type conversion, specifically converting an integer to a byte and a double to an integer. It also includes a program that uses a for-each loop to compute the average of the first ten natural numbers. The output of both programs is displayed, showing the results of the conversions and the summation of the numbers.

Uploaded by

a70497944
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

OOPS With JAVA Laboratory (22CSL38)

2. a. Write a Java program to illustrate Type Casting of the datatype and type
conversion.

class Conversion

public static void main(String args[])

byte b;

int i = 257;

System.out.println("\nConversion of int to byte.");

b = (byte) i;

System.out.println("i and b " + i + " " + b);

System.out.println("\nConversion of double to int.");

i = (int) d;

System.out.println("d and i " + d + " " + i);

System.out.println("\nConversion of double to byte.");

Output:

Conversion of int to byte.


i and b 257 1

Conversion of double to int.

d and i 323.142 323

Dept. of Computer Science and Design 1


Faculty of Engineering & Technology, SUK
OOPS With JAVA Laboratory (22CSL38)

2.b. write a java program to implement for-each loop to compute average of n natural
numbers.

Class ForEach

Public static void main (string args[ ] )

Intnums [ ] = { 1, 2, 3, 4 , 5 , 6, 7, 8, 9, 10 };

Int sum=0;

For(int x : nums )

System.out.println(“value is : “+ x);

Sum += x;

System.out.println(“summation: “+sum);

Output:Value is : 1

Value is : 2
Value is : 3
Value is : 4
Value is : 5
Value is : 6
Value is : 7
Value is : 8
Value is : 9
Value is : 10

Summation : 55

Dept. of Computer Science and Design 2


Faculty of Engineering & Technology, SUK

You might also like