Untitled

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

write a java code to print diamond pattern

enter the value

**

***

****

*****

****

***

**

solution

import java.util.Scanner;

public class DiamondPattern {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println("enter the value");

int rows=sc.nextInt();

/* Code for Upper triangle */

for (int i = 1; i < rows; i++) {

for (int j=rows-i; j>=1 ; j--) {

System.out.print(" ");
}

for (int k = 1; k <= i; k++) {

System.out.print("* ");

System.out.println();

/* Code for inverted triangle */

for (int i = 1; i <= rows; i++) {

for (int j=1 ; j< i ; j++) {

System.out.print(" ");

for (int k = i; k <= rows; k++) {

System.out.print("* ");

System.out.println();

EFFICIENT WAY

import java.util.Scanner;

public class Diamond_pattern1


{

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println("enter the odd value");

int rows=sc.nextInt();

int space=rows/2;

int star=1;

for (int i = 1; i <= rows; i++)

for (int j = 1; j <=space; j++)

System.out.print(" ");

for (int k = 1; k <= star; k++)

System.out.print("*");

System.out.println();

if(i<=rows/2)

space--;

star=star+2;

}else

space++;
star=star-2;

Note - always try to enter odd number and start i from 1

String Programs

-----------------------------------------------------------------

write java program to convert given string in lowercase

import java.util.Scanner;

public class toLowerCase {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println("enter string value");

String s=sc.nextLine();

String nstr="";

char[] ch=s.toCharArray();//converting to array

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

if (ch[i]>=65 && ch[i]<=90)


{

nstr=nstr+(char)(ch[i]+32);

else

nstr=nstr+ch[i];

System.out.println(nstr);

2. Write a java code to remove space between the string

input -- i am a good boy

output -- iamagoodboy

import java.util.Scanner;

public class RemoveSpace {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println("enter string value");

String s=sc.nextLine();

String nstr="";

//create a new empty string

char[] ch=s.toCharArray();
//convert the string into array

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

if(ch[i]!=' ')

nstr=nstr + ch[i];

/* if the character at ith index is not equal


to space

then add that character to new empty string*/

System.out.println(nstr);

3. write a java program to sort string characters into ascending order

import java.util.Scanner;

public class sort {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println("enter string value");

String str=sc.nextLine();

char[] ch=str.toCharArray();

//sort string in alphabetical order

for(int i=0;i<ch.length-1;i++)

for(int j=i+1;j<ch.length;j++)
{

if(ch[i]>ch[j])

char temp=ch[i];

ch[i]=ch[j];

ch[j]=temp;

String st=new String(ch);

System.out.println(st);

input RACE

ouput ACER

You might also like