file1

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

1.Write the program to make the following pattern.

A
ABA
import java.util.*; ABCBA
public class monkey ABCDCBA
{ ABCDEDCBA
public static void main(String[] args) ABCDEFEDCBA
{
char i,j;
for(i='A';i<='F';i++)
{
for(char ch='F';ch>=i;ch--)
{
System.out.print(" ");
}
for(j='A';j<=i;j++)
{
System.out.print(j);

}
for(char b=(char)(i-1);b>='A';b--) Output
{ A
System.out.print(b); ABA
} ABCBA
System.out.println("");
ABCDCBA
}
ABCDEDCBA
}
ABCDEFEDCBA
}
2.Write the program to make the following pattern. A
AA
public class ramram ACA
{ ADDA
public static void main(String[] args) AEEEA
{ AFFFFA
char i,j;
for(i='A';i<='F';i++)
{
for(j='A';j<=i;j++)
{
if(j=='A')
{
System.out.print("A");
}
else if(i==j)
{
System.out.print("A");
}
else
{
System.out.print(i);
} Output
} A
System.out.println(""); AA
} ACA
} ADDA
AEEEA
} AFFFFA
3. Write the program to make the following pattern. ******
import java.util.*; * *
public class God { * *
public static void printPattern(int n) * *
{
int i, j; * *
******
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {

if (i == 0 || j == 0 || i == n - 1 || j == n - 1) {
System.out.print("*");
}
else {
System.out.print(" ");
}
}
System.out.println();
}
}

public static void main(String args[]) Output


{ ******
int n = 6;
printPattern(n); * *
} * *
}
* *
* *
******
4.Write the program to make the following pattern.
import java.util.*; 1
public class God { 01
public static void printPattern(int n) 101
{
int i, j; 0101
10101
for (i = 1; i <= n; i++) {
for (j = 1; j <= i; j++) { 010101
if ((i + j) % 2 == 0) {
System.out.print(1 + " ");
}
else {
System.out.print(0 + " ");
}
}

System.out.println();
}
}

public static void main(String args[])


{ Output
int n = 6;
printPattern(n); 1
} 01
}
101
0101
10101
010101
5.Write a program to print the array element

class Main {

public static void main(String[] args) {

int[ ] age = {12, 4, 5, 2, 5};

System.out.println("Accessing Elements of Array:");


System.out.println("First Element: " + age[0]);
System.out.println("Second Element: " + age[1]);
System.out.println("Third Element: " + age[2]);
System.out.println("Fourth Element: " + age[3]);
System.out.println("Fifth Element: " + age[4]);
}
}

Output
Accessing Elements of Array:
First Element: 12
Second Element: 4
Third Element: 5
Fourth Element: 2
Fifth Element: 5
6. Write a program to multiply two 2D array in a single 2D array.

import java.util.*;
import java.util.Scanner;
public class mul {

public static void main(String[] args) {


Scanner sc=new Scanner(System.in);
int a[][]=new int[3][3];
int b[][]=new int[3][3];
int c[][]=new int[3][3];
for(int x=0;x<3;x++)
{
for(int y=0;y<3;y++)
{
System.out.println("enter the value in a matrix");
a[x][y]=sc.nextInt();
System.out.println("enter the value in b matrix");
b[x][y]=sc.nextInt();
}
}
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
c[i][j]=0;
for(int k=0;k<3;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
}
7.Write a program to search an element in an array by using Linear
Search Technique .

import java.util.*;
public class oreo {

public static void main(String[] args)


{
Scanner sc= new Scanner(System.in);
System.out.print("enter no. found");
int n=sc.nextInt();
int i,f=0;
int a[]={4,8,3,6,9,11,20,16};
for(i=0;i<7;i++)
{
if(a[i]==n)
{
f=1;
break;
}
}
if(f==1)
{
System.out.println("element position"+(i+1));
}
else
{
System.out.println("element is missing"); Output
} Elementposition 8
}}
8.Write a program to search an element in an array by using Binary
Search Technique .
public class fhhf {

public static void main(String[] args) {


int A[]={5,10,15,20,25,30,35,40,45,50};
int l=0;
int u=9;
int m=0;
int n=45;
int flag=0;
while(l<=u)
{
m=(l+u)/2;
if(n>A[m])
{
l=m+1;
}
else if(n<A[m])
{
u=m-1;
}
else
{
Output
flag=1;
Element present in position 45
break;
}
}
if(flag==1)
{
System.out.println("element present in position"+(m+1));
}
else
{
System.out.println("element not present");
}
}
9.Write a program to arrange the element in an array by using
Selection Short Technique .
import java.util.*;
public class ghff
{
public static void main(String[] args)
{
int a[]={5,3,8,4,9,2,1,12,90,15};
Scanner sc=new Scanner(System.in);
int i,j,small,tmp,pos;
for(i=0;i<10;i++)
{ Output
small=a[i]; Array in ascending order is -->
pos=i; 1
for(j=i+1;j<10;j++) 2
{ 3
if(a[j]<small) 4
{ 5
small=a[j]; 8
pos=j; 9
} 12
} 15
tmp=a[i]; 90
a[i]=a[pos];
a[pos]=tmp;
}
System.out.println("Array in ascending order is -->");
for(i=0;i<10;i++)
System.out.println(a[i]+"\t");
}
}
10. Write a program to input 15 integer elements in an array and sort
them in ascending order using the bubble sort technique.

package soun;
import java.util.*;
public class oreo {

public static void main(String[] args)


{
Scanner kb= new Scanner(System.in);
int i,temp,x,j;
int arr[]=new int [15];
System.out.println("enter 15 elements=");
for(i=0;i<15;i++)
arr[i]=kb.nextInt();
x=arr.length; Output
for(i=0;i<x-1;i++) sorted array is:
{
1
for(j=0;j<x-1;j++)
1
{
2
if(arr[j]>arr[j+1])
3
{
6
temp=arr[j];
8
arr[j]=arr[j+1];
13
arr[j+1]= temp;
19
}
22
}
33
}
38
System.out.println("sorted array is:");
53
for(i=0;i<x;i++)
55
{
74
System.out.println(arr[i]+" ");
86
}}}
11. Write a program to tell whether the entered day is Weekday or
Weekend day by using switch case statement.

import java.util.*;
public class ramram {
public static void main(String[] args) {
int a;
Scanner sc=new Scanner (System.in);
System.out.println("Enter day no.");
a=sc.nextInt();
switch(a)
{
case 1:
System.out.println("Weekend");
break;
case 2:
System.out.println("Weekday");
break;
case 3:
System.out.println("weekday");
break;
case 4:
System.out.println("Weekday");
break ;
case 5: Output
System.out.println("Weekday"); Sunday
break ; Weekend
case 6:
System.out.println("Weekday");
break ;
case 7:
System.out.println("Weekday");
break ;
default:
System.out.println("Error");
break ;
} }}
12. Write a program to input a string in uppercase and print the
frequency of each character
Example:
INPUT: COMPUTER HARDWARE

package sunshines;

public class string {

public static void main(String[] args) {


String a="COMPUTER HARDWARE";
char ch=' ';
int s=0;
for(ch='A';ch<='Z';ch++)
{ Output
s=0; A 2
for(int i=0;i<a.length();i++) C 1
{ D 1
if(a.charAt(i)==ch) E 2
{ H 1
s=s+1; M 1
} O 1
} P 1
if(s==0) R 3
{ T 1
continue; U 1
} W 1
System.out.println(ch+"\t\t"+s);
}
}}
13. Write a program to input day number and print the day buy using
Switch case statement.
import java.util.*;
public class day {

public static void main(String[] args) {


Scanner sc=new Scanner(System.in);
int day=sc.nextInt();
if(day==1){
System.out.println("sunday");}
else if(day==2)
{
System.out.println("monday");
}
else if(day==3)
{
System.out.println("Tuesday");
}
else if(day==4)
{
System.out.println("Wednesday");
}
else if(day==5)
{
System.out.println("Thursday");
}
else if(day==6)
{
System.out.println("friday");
}
else if(day==7)
{
System.out.println("Saturday");
}
else{
System.out.println("error");
}}}
14. Write a program to find out the entered number is divisible by 5 or
6 or by both by using switch case statement.
import java.util.*;
public class O {

public static void main(String[] args) {


int a;
int z;
Scanner sc=new Scanner (System.in);
System.out.print("enter a number");
a=sc.nextInt();
if(a%5==0&&a%6==0) {
System.out.print("number is devided by 5 and 6");
z=0;
}
else if(a%5==0) {
System.out.print("number is devided by 5");
z=0;
}
else if(a%6==0)
{
System.out.print("number is divisible by 6");
z=0;
}
else
{
System.out.print("number is not devided by 5 and 6");
z=1;
}
switch(z) {
case 0:
System.out.print("?true");
break;
case 1:
System.out.print("?false");
break;
} }}
15. Write a program in java to accept a string in lower case and change
the first letter of every word to uppercase.display the new string

import java.util.Scanner;
public class ey {

public static void main(String args[])


{
Scanner sc=new Scanner(System.in);
System.out.print("Enter a string in lower case :");
String st=inp.nextLine();
StringBuffer str=new StringBuffer(st);
int In=str.length();
char ch;
if(st.charAt(0)!='')
{
ch=st.charAt(0);
st.setCharAt(0, Character.toUppercase(ch));
}
for(int i=0;i<In-1;i++)
if(st.charAt(i)==''&&str.charAt(i+1)!='')
{
ch=st.charAt(i+1);
st.setCharAt(i+1), Character.toUpperCase(ch));
}
System.out.println(str);
}
}

output
input: we are in cyber world
output: We Are In Cyber World
16. Write a program to identify that how many alphabet ,numbers and
special number are there in string.

package soun;

import java.util.*;
public class furro {

public static void main(String[] args)


{
Scanner sc= new Scanner (System.in);
System.out.println("enter string");
String s=sc.nextLine();
int a=0,b=0,c=0;
for(int i=0;i<s.length();i++)
{
char ch=s.charAt(i);
if((ch=='a')&&(ch=='z'))
{
a=a+1;
}
else if(ch>1&&(ch<=90))
{
b=b+1;
} Input : 123shw@#$
else Output:
{ Alphabet=3
c=c+1; Number=3
} Special=3
}
System.out.println("Alphabet="+a);
System.out.println("Number="+b);
System.out.println("Special="+c);
}

}
17.Write a program to accept a word and convert it into lowercase if it
is in uppercase,and display the new word by replacing only the vowels
with the character following it.

import java.util.*;
public class ghff
{
public static String convert(String s)
{
StringBuffer str=new StringBuffer(s);
int n=s.length();
char ch;
for(int i=0;i<n;i++)
ch=str.charAt(i);
if( Charater.isUpperCase(ch)==true)
str.setCharAt(i,Charater.toLowerCase(ch));
ch=str.charAt(i);

if(str.charAt(i)=='a'||str.charAt(i)=='e'||str.charAt(i)=='i'||str.charAt(i)=='o'||st
r.charAt(i)=='u')
{
str.setCharAt(i,(char)(ch+1));
}
}
return str.toString();
}
public static void main(String []args)
{
String word;
Scanner kb=new Scanner(System.in);
Input : computer
System.out.println("Enter a word");
Output: cpmpvtfr
word=kb.next();
System.out.println(convert(word));
}
}
18. Write a program to enter the marks and print the grade according
to the entered marks by using if else ladder statement.
public class radhe {
public static void main(String[] args) {
int marks=65;

if(marks<50){
System.out.println("fail");
}
else if(marks>=50 && marks<60){
System.out.println("D grade");
}
else if(marks>=60 && marks<70){
System.out.println("C grade");
}
else if(marks>=70 && marks<80){
System.out.println("B grade");
}
else if(marks>=80 && marks<90){
System.out.println("A grade");
}else if(marks>=90 && marks<100){
System.out.println("A+ grade");
}else{
System.out.println("Invalid!");
}
}
}

Output:
C grade
19. Write a program to find out the entered number is buzz number or
not a buzz number in java

import java.util.Scanner;

class Main {

static boolean checkBuzz(int num)


{
if(num % 10 == 7 || num % 7 == 0)
return true;
else
return false;
}

public static void main(String args[])


{
int n;
Scanner sc=new Scanner(System.in);
n = sc.nextInt();
if (checkBuzz(n))
System.out.println(n + " is a Buzz number");
else
System.out.println(n + " is not a Buzz number");
}
}
20. Write a program to find out the entered number is Armstrong
number or not in java.

public class Armstrong {

public static void main(String[] args) {

int number = 371, originalNumber, remainder, result = 0;

originalNumber = number;

while (originalNumber != 0)
{
remainder = originalNumber % 10;
result += Math.pow(remainder, 3);
originalNumber /= 10;
}

if(result == number)
System.out.println(number + " is an Armstrong number.");
else
System.out.println(number + " is not an Armstrong number.");
}
}

Output
371 is an Armstrong number

You might also like