0% found this document useful (0 votes)
8 views13 pages

Untitled Document

The document contains multiple Java programs that perform various tasks such as finding the longest word in a sentence, checking for 'superspy' numbers, determining if a number is automorphic, sorting arrays, and generating patterns. Each program prompts the user for input and processes it accordingly, demonstrating fundamental programming concepts like loops, conditionals, and array manipulation. Overall, the document serves as a collection of coding exercises for practicing Java programming skills.

Uploaded by

r29110157
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)
8 views13 pages

Untitled Document

The document contains multiple Java programs that perform various tasks such as finding the longest word in a sentence, checking for 'superspy' numbers, determining if a number is automorphic, sorting arrays, and generating patterns. Each program prompts the user for input and processes it accordingly, demonstrating fundamental programming concepts like loops, conditionals, and array manipulation. Overall, the document serves as a collection of coding exercises for practicing Java programming skills.

Uploaded by

r29110157
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/ 13

1.

import java.util.*;
class String{
public static void main( ) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the sentence:");
String s = sc.nextLine();
s = s + " ";
String w = "", maxW = "";

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


char c = s.charAt(i);
if (c != ' ') {
w = w + c;
} else {
if (w.length() > maxW.length()) {
maxW = w;
}
w = "";
}
}

System.out.println("Longest word: " + maxW);


}
}
2.
import java.util.*;

class Array {
public static void main( ) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the sentence:");
String s = sc.nextLine();
s = s + " ";
String w = "", rev = "";

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


char c = s.charAt(i);
if (c != ' ') {
w = w + c;
rev = c + rev;
} else {
if (w.equals(rev)) {
System.out.println(w);
}
w = "";
rev = "";
}
}
}
}
3.

import java.util.*;

class superspy {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
int n = sc.nextInt();
int c = 0;
int sum = 0;

while (n > 0) {
int r = n % 10;
sum = sum + r;
c++;
n = n / 10;
}

if (sum == c) {
System.out.println("superspy");
} else {
System.out.println("Not superspy");
}
}
}
4.

import java.util.*;

class automorphic{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
int n = sc.nextInt();
int c = 0;
int t = n;

while (n > 0) {
int r = n % 10;

c++;
n = n / 10;
}
int d =(int)Math.pow(10,c);
int s = t*t;
int l = s%d;

if (l == t) {
System.out.println("automorphic");
} else {
System.out.println("Not automorphic");
}
}
}
5.

import java.util.*;

class superspy {
public static void main( ) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of elements:");
int n = sc.nextInt();
int arr[] = new int[n]; System.out.println("Enter the elements:");

for (int i = 0; i < n; i++) {


arr[i] = sc.nextInt();
}

int f = 0;
for (int i = 0; i < n - 1; i++) {
if (arr[i] > arr[i + 1]) {
f = 1;
break;
}
}

if (f == 0) {
System.out.println("The array is sorted in ascending order.");
} else {
System.out.println("The array is not sorted in ascending order.");
}
}
}
6.
import java.util.*;

class binary {
public static void main( ) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of elements:");
int n = sc.nextInt();
int arr[] = new int[n]; System.out.println("Enter the elements:");

for (int i = 0; i < n; i++) {


arr[i] = sc.nextInt();
}

System.out.println("Enter the element to find:");


int k = sc.nextInt();

int f = 0; // First index


int l = n - 1;
while (f <= l) {
int mid = (f + l) / 2;
if (arr[mid] == k) {
System.out.println("Element found at index " + mid);

break;
} else if (arr[mid] > k) {
l = mid - 1;
} else {
f = mid + 1;
}
}

if (f > l) {
System.out.println("Element not found");
}
}
}
7.
import java.util.*;

class Array {
public static void main( ) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of the array:");
int n = sc.nextInt();
int arr[] = new int[n]; System.out.println("Enter the elements:");

for (int i = 0; i < n; i++) {


arr[i] = sc.nextInt();
}

System.out.println("Enter the element to be searched:");


int k = sc.nextInt();
int f = -1;

for (int i = 0; i < n; i++) {


if (arr[i] == k) {
f = i;
break; }
}

if (f == -1) {
System.out.println("Element not found");
} else {
System.out.println("Element found at index: " + f);
}
}
}
8.
import java.util.*;

class spattern {
public static void main( ) {
String s = "ICSE";
int n = s.length();

for (int i = 1; i <= n; i++) { // Fixed condition: i <= n

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


System.out.print(s.charAt(i-1));
}
System.out.println();
}
}
}
9.

import java.util.*;

class Pattern {
public static void main( ) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter 1 for pattern 1 and 2 for pattern 2");
int check = sc.nextInt();

switch (check) {
case 1:
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j % 2);
}
System.out.println();
}
break;

case 2:
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print((char) (64 + j));
}
System.out.println();
}
break;

default:
System.out.println("Invalid choice");
break;
}

}
}

10..
import java.util.*;

class Array {
public static void main( ) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of elements:");
int n = sc.nextInt();
int arr[] = new int[n];

System.out.println("Enter the elements of the array:");

for (int i = 0; i < n; i++) {


arr[i] = sc.nextInt();
}

for (int i = 0; i < n - 1; i++) {


for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {

int t = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = t;
}
}
}

System.out.println("The elements after sorting:");


for (int i = 0; i < n; i++) {
System.out.println(arr[i]);
}
}
}
11.

import java.util.*;

class string {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the sentence:");
String s = sc.nextLine(); // Use nextLine() to read the entire sentence
String res = " ";
s = " "+s;
int l = s.lastIndexOf(' ');
for(int i = 0; i<l; i++)
{
char c = s.charAt(i);
if(c == '.')
{
res = res+s.charAt(i+1) + ".";

}
}
res = res+s.substring(l);
System.out.println("result" +res);
}
}
12.

import java.util.*;

class Pattern {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter 1 for pattern 1 and 2 for pattern 2");
int check = sc.nextInt();

switch (check) {
case 1:
for (int i = 1; i <= 5; i++) {
for (int j = i; j >= 1; j--) {
System.out.print(j);
}
System.out.println();
}
break;

case 2:
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j%2);
}
System.out.println();
}
break;

default:
System.out.println("Invalid choice");
break;
}

}
}
13.
import java.util.*;

class Pattern {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter 1 for pattern 1 and 2 for pattern 2:");
int check = sc.nextInt();

switch (check) {
case 1:
// Fibonacci Pattern
int a = 0, b = 1, c = 0;

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


System.out.print(a + " ");
c = a + b;
a = b;
b = c;
}

// For a new line after the sequence


break;

case 2:
// Character Pattern
System.out.println("Character Pattern:");
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print((char) (64 + j));
}
System.out.println();
}
break;

default:
System.out.println("Invalid choice");
break;
}
}
}
14.

import java.util.*;

class StringCounter {
public static void main( ) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the sentence:");
String s = sc.nextLine();
int l= 0, d = 0, sp = 0;

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


char c = s.charAt(i);
if (Character.isLetter(c)) l++;
} else if (Character.isDigit(c)) {
d++;
} else{
sp++;
}
}

System.out.println("Number of letters: " + l);


System.out.println("Number of digits: " + d);
System.out.println("Number of special characters: " + sp);
}
}
15.

import java.util.*;
class spattern {
public static void main( ) {
String s = "BLUEJ";
int n = s.length();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(s.charAt(j-1)); }
System.out.println();
}
}

You might also like