21BEC2059 17 - 03 - 2022 (K. Pradeep)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Name: K.

Pradeep

Registration number: 21BEC2059

COMPUTER ASSINGNMENT – 3

1.

public class Main{

public static void main(String[] args){

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

System.out.println(cars[0]);

Output:

2.

public class Main{

public static void main(String[] args){

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

cars[0] = "Opel";

System.out.println(cars[0]);

}
}

Output:

3.

public class Main{

public static void main(String[] args){

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

System.out.println(cars.length);

Output:

4.

public class Main{

public static void main(String[] args){

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

for (int i = 0; i < cars.length; i++){

System.out.println(cars[i]);
}

Output:

5.

public class Main{

public static void main(String[] args){

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

for (String i : cars){

System.out.println(i);

Output:
6.

public class Main{

public static void main(String[] args){

String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

System.out.println("The length of the txt string is: " + txt.length());

Output:

7.

public class Main{

public static void main(String[] args){

String txt = "Hello World";

System.out.println(txt.toUpperCase());
System.out.println(txt.toLowerCase());

Output:

8.

public class Main{

public static void main(String[] args){

String txt = "Please locate where 'locate' occurs!";

System.out.println(txt.indexOf("locate"));

Output:

9.

public class Main{

public static void main(String args[]){

String firstName = "John";


String lastName = "Doe";

System.out.println(firstName + " " + lastName);

Output:

10.

public class Main{

public static void main(String[] args){

String firstName = "John ";

String lastName = "Doe";

System.out.println(firstName.concat(lastName));

Output:

11.

public class Main{


public static void main(String[] args){

String txt = "It\'s alright.";

System.out.println(txt);

Output:

12.

public class Main{

public static void main(String[] args){

String txt = "Hello\nWorld!";

System.out.println(txt);

Output:

13.

public class Main{


public static void main(String[] args){

String txt = "Hello\rWorld!";

System.out.println(txt);

Output:

14.

public class Main{

public static void main(String[] args){

String txt = "Hello\tWorld!";

System.out.println(txt);

Output:

15.

public class Main{

public static void main(String[] args){


String txt = "Hel\blo World!";

System.out.println(txt);

Output:

16.Write a java program to reverse a string.

Code:

public class Reverse {

public static void main(String[] args) {

String str = "gnitseT erawtfoS";

StringBuilder stbuilder = new StringBuilder(str);

stbuilder.reverse();

System.out.println(stbuilder);

}
Output:

17.Write a java program to swap the two numbers.

Code:

public class SwapNumbers {

public static void main(String[] args) {

float first = 100, second = 200;

System.out.println("Before swap");

System.out.println("First number = " + first);

System.out.println("Second number = " + second);

float temporary = first;

first = second;

second = temporary;

System.out.println("After swap");

System.out.println("First number = " + first);

System.out.println("Second number = " + second);

}
Output:

18.Write a java program tofind whether a string or number is palindrome or


not.

Code:

public class PalindromeExample{

public static void main(String args[]){

int r,sum=0,temp;

int n=454;//It is the number variable to be checked for palindrome

temp=n;

while(n>0){

r=n%10; //getting remainder

sum=(sum*10)+r;

n=n/10;

if(temp==sum)

System.out.println("palindrome number ");

else

System.out.println("not palindrome");

}
}

Output:

You might also like