[Go to site: main page, start]

0% found this document useful (0 votes)
6 views10 pages

Java Codes

The document contains a series of Java programs that demonstrate various algorithms and concepts, including string reversal, prime number checking, factorial calculation, and palindrome detection. Each program is structured with a main method and performs a specific task, such as reversing numbers, finding Armstrong numbers, and counting vowels in a string. Overall, the document serves as a collection of coding exercises for fundamental programming skills.

Uploaded by

kirti sharma
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)
6 views10 pages

Java Codes

The document contains a series of Java programs that demonstrate various algorithms and concepts, including string reversal, prime number checking, factorial calculation, and palindrome detection. Each program is structured with a main method and performs a specific task, such as reversing numbers, finding Armstrong numbers, and counting vowels in a string. Overall, the document serves as a collection of coding exercises for fundamental programming skills.

Uploaded by

kirti sharma
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

1. Rev of a string.

Public class {

Public static void main (String[] args) {

String s = “ hello world”;

String rev = “”;

For (int i = [Link]()-1 ; i>-0 ; i--) {

Rev=rev+[Link](i);

[Link](rev);

2. Rev the words

public class Main {

public static void main (String[] args){

String name = "hello world";

String[] words= [Link](" ");

for ( int i = [Link] - 1; i >= 0; i--){

[Link](words[i] + " ");

3. Prime num
public class Main {
public static void main (String[] args){
int n = 26;
Boolean isPrime = true;
if ( n <= 1) {
isPrime = false;
}
else {
for (int i = 2; i<= n/2; i++) {
if (n % i == 0){
isPrime = false;
break;
}
}
}
if ( isPrime) {
[Link](n + " is prime");
}
else {
[Link](n + " is not prime");
}

}
}
4. Prime I range
//rev string
public class Main {
public static void main (String[] args){
int start = 1;
int end = 100;
[Link]("prime:");
for (int n = start; n<=end ; n++){
boolean isPrime= true;
if ( n <= 1) {
isPrime = false;
}
else {
for (int i = 2; i<= n/2; i++) {
if (n % i == 0){
isPrime = false;
break;
}
}
}
if (isPrime) {
[Link](n + " ");
}
}
}
}

5. Rev of num and sum of digits


public class Main {
public static void main (String[] args){
int num = 1234;
int rev=0;
while (num > 0) {
int digit = num % 10;
rev= rev * 10 + digit;
num= num/10;
}
[Link] (rev);
}
}

6. Palindrome in num

public class Main {


public static void main (String[] args){
int num = 123;
int temp = num;
int rev=0;
while (num > 0) {
int digit = num % 10;
rev= rev* 10 + digit;
num= num/10;
}
if (rev == temp){
[Link] (rev + "palin");
}
else {
[Link](rev + " not palin");
}
}
}
7. Palindrom in string

public class Main {


public static void main (String[] args){
String s = "madam";
String rev= "";
for (int i=[Link]()-1; i>= 0; i--){
rev= rev + [Link](i);
}
if ([Link](rev)) {
[Link](rev + " palin");

}
else {
[Link](rev + " not palin");
}
}
}

8. Amstrong number
public class Main {
public static void main (String[] args){
int n = 153;
int temp = n;
int rev =0;
while (n >0) {
int d = n % 10;
rev= rev + d * d *d;
n= n/10;
}
if (rev== temp) {
[Link](rev + " amstrong");
}
else {
[Link](rev +" not amstrong");
}
}
}
9. Fibanosis series

public class Main {


public static void main (String[] args){
int a= 0,b=1;
for (int I = 1; i<=10; i++) {
[Link](a+ “ “ );
Int c = a+b;
A= b;

B=c;

}
}

10. Factorial in range

public class Main {


public static void main (String[] args){
int start= 1;
int end=10;
for (int n=start; n<=end ; n++){
long fact = 1;
for (int i = 1; i<=n; i++){
fact=fact*i;
}
[Link](n +"fact is" + fact);
}
}
}

11. Swap two num


public class Main {
public static void main (String[] args){
int A= 1;
int B=2;
A=A+B;
B=A-B;
A=A-B;
[Link](A +" " + B);

}
}

12. Sum of N natural numbers


public class Main {
public static void main (String[] args){
int n = 4;
int sum= 0;
for (int i=1;i<=n;i++){
sum=sum+i;
}
[Link](sum);
}
}

13. Power of a number

public class Main {


public static void main (String[] args){

int base = 2;
int exp= 3;
int result= 1;
for (int i=1;i<=exp; i++){
result=result*base;
}
[Link](result);

}
}

14. Strong number

//145=1!+4!+5! =145
public class Main {
public static void main (String[] args){

int n=145;
int sum=0;
int temp = n;
while(n>0){
int d= n%10;
int fact =1;
for (int i=1; i<=d; i++){
fact=fact*i;

}
sum=sum+fact;
n=n/10;
}
if(sum== temp) {
[Link](temp + " strong");

}
else {
[Link](temp + " not a strong num");
}
}
}

15. Factors of a num

public class Main {


public static void main (String[] args){

int n= 12;
for (int i=1;i<=n; i++) {
if (n%i==0){
[Link](i + " ");
}
}
}
}
16. Perfect number
//sum of factors == number

//palin string
public class Main {
public static void main (String[] args){

int n= 28;
int sum = 0;
for (int i=1;i<n;i++){
if(n%i==0){
sum=sum+i;
}
}
if(sum==n){
[Link](n + " perfect number");
}
else {
[Link](n + " not perfect number");
}
}
}

17. Harshad number

//21/2+1 =21/3 =7

//palin string

public class Main {

public static void main (String[] args){

int n= 21;

int temp= n;

int result = 0;

while (n!=0){

int digit = n%10;

result= result+digit;

n=n/10;

if( temp % result ==0){

[Link](temp + " harsed number");

else {

[Link](temp + " not harsed number");

}
18. Largest and smallest in an array

Public class large{


public static void main(String[] args){
int[] arr ={ 12,13,4,44,20};
int max=arr[0];
for (int i=1; i< [Link]; i++){
if(arr[i]>max){
max=arr[i];
}
[Link](max);
}
}

19. Largest and smallest

import [Link];
public class Main{
public static void main(String[] args){
int[] arr ={12,13,4,44,20};
[Link](arr);
[Link](arr[0]);
[Link](arr[[Link]-2]);
}
}
[Link] vowels

public class Main{


public static void main(String[] args){
String s= "hello";
int count = 0;
for (int i=0; i< [Link](); i++ ){
char ch = [Link](i);
if ((ch == 'a' || ch == 'e' || ch == 'i' || ch== 'o' || ch== 'u')){
count++;
}
}
[Link]( count);
}
}
20. Print vowels

public class Main{

public static void main(String[] args){

String s= "hello";

for (int i=0; i< [Link](); i++ ){

char ch = [Link](i);

if ((ch == 'a' || ch == 'e' || ch == 'i' || ch== 'o' || ch== 'u')){

[Link](ch + " ");

You might also like