0% found this document useful (0 votes)
40 views

Java Prog

The document contains 6 code snippets written in Java that demonstrate various programming concepts: 1) A program to find prime numbers within a given range. 2) A program to check if a string is a palindrome. 3) A program to sort an array of strings alphabetically. 4) A program to multiply two matrices and output the result. 5) A program to input and sum integers in an array. 6) Additional Java code snippets.

Uploaded by

Arun Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Java Prog

The document contains 6 code snippets written in Java that demonstrate various programming concepts: 1) A program to find prime numbers within a given range. 2) A program to check if a string is a palindrome. 3) A program to sort an array of strings alphabetically. 4) A program to multiply two matrices and output the result. 5) A program to input and sum integers in an array. 6) Additional Java code snippets.

Uploaded by

Arun Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

2nd question

import java.util.*; import java.lang.*; import java.io.*; class prime { public static void main(String ar[]) { int i,j; Scanner h=new Scanner(System.in); System.out.println("enter the value of n"); int num=h.nextInt(); System.out.println("Prime number:"); for(i=1;i<=num;i++) { for(j=2;j<i;j++) { int n=i%j; if(n==0) { break; } } if(i==j) { System.out.println("the prime no. is"+i); }

} } }

3rd question
import java.util.*; import java.lang.*; class palindrome { public static void main(String ar[]) { Scanner h=new Scanner(System.in); String s=h.nextLine();

String s2=new StringBuffer(s).reverse().toString(); if(s.equalsIgnoreCase(s2)) { System.out.println("the string is palindrome"); } else { System.out.println("the string is not a palindrome"); }

} }

4th question
import java.util.*; import java.lang.*; class sortng { public static void main(String ar[]) { Scanner h=new Scanner(System.in); System.out.println("enter value of n"); int n=h.nextInt(); String temp=new String(); String names[]=new String[n+1]; for(int i=0;i<=n;i++) { names[i]=h.nextLine(); } for(int i=0;i<=n;i++) { for(int j=i+1;j<=n;j++) { if(names[i].compareTo(names[j])>0) { temp=names[i]; names[i]=names[j];

names[j]=temp; } } } System.out.println("the sorted oreder is"); for(int i=0;i<=n;i++) { System.out.println(names[i]); } } }

5th question
import java.util.*; import java.lang.*; class mul { public static void main(String ar[]) { Scanner h=new Scanner(System.in); System.out.println("enter the row"); int n=h.nextInt(); System.out.println("enter the col"); int m=h.nextInt(); int a[][]=new int[50][50]; int b[][]=new int[50][50]; int c[][]=new int[50][50]; System.out.println("reading the mat1");

for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { a[i][j]=h.nextInt(); }

6th question
import java.util.*; import java.lang.*; class array { public static void main(String ar[]) { int sum=0; Scanner h=new Scanner(System.in); int s[]=new int[10]; for(int i=0;i<10;i++) { s[i]=h.nextInt(); sum=sum+s[i]; System.out.println(s[i]); System.out.println("the sum is"+sum); } }

} System.out.println("reading the mat2"); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { b[i][j]=h.nextInt(); } } System.out.println("the mul mat is "); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { c[i][j]=0; for(int k=0;k<n;k++) { c[i][j]=c[i][j]+(a[i][k]*b[k][j]); } System.out.println(c[i][j]); } } } }

You might also like