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

Javalab

The document contains code snippets for 5 Java programs: 1) A recursive program to find the factorial of a number 2) A program to check if a number is an Armstrong number 3) A program to check if a number is a palindrome 4) A program to check if a user-input string matches a stored string 5) Two programs to reverse a string, one using a for loop and the other using the StringBuilder class.

Uploaded by

Karthik Prasad
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)
22 views

Javalab

The document contains code snippets for 5 Java programs: 1) A recursive program to find the factorial of a number 2) A program to check if a number is an Armstrong number 3) A program to check if a number is a palindrome 4) A program to check if a user-input string matches a stored string 5) Two programs to reverse a string, one using a for loop and the other using the StringBuilder class.

Uploaded by

Karthik Prasad
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/ 6

OPEN LAB-I

LANGUAGE: JAVA
COURSE CODE: 18CSC382
ROLL NO: CB.SC.I5DAS20141
NAME: KARTHIK PRASAD G

1) Write a java program to find the factorial of a number and using recursion.
CODE BLOCK:
import java.util.Scanner;
public class factorial{
public int fact(int x)
{
if (x==0){
return 1;
}
else{
return x*fact(x-1);
}
}
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
System.out.print("Enter first number- ");
int a = sc.nextInt();

factorial obj=new factorial();


int result=obj.fact(a);
System.out.println("The factorial of the number is: " + result);
}
}
OUTPUT:
2) Write a java program to check if a given number is an Armstrong number.

CODE BLOCK:
import java.util.Scanner;
import java.lang.Math;
public class armstrong {
public static void main(String[] args) {
int inum=371;
int num=inum;
int result=0;
int remainder=0;
while (inum!=0)
{
remainder=inum%10;
result += Math.pow(remainder, 3);
inum/=10;

}
if (result==num){

System.out.println(num + " is an Armstrong number.");


}
else{
System.out.println(num + " is not an Armstrong number.");
}
}
}
OUTPUT:
3)Write a java program to check if the given integer number is a palindrome
CODE BLOCK:

import java.lang.Math;

public class palindrome {

public static void main(String[] args)

int n=1221;

int rev=0;

int rem;

int temp;

temp=n;

while(n>0) {

rem=n%10;

rev=rev*10+rem;

n=n/10;

if(temp==rev) {

System.out.println("The number is a palindrome");

else {

System.out.println("The number is not a palindrome");

OUTPUT:
4) Write a Java program to check if the user specified string is equal to a string
stored in a variable using the String library function.
CODE BLOCK:

import java.util.Scanner;

public class checkstring{

public static void main(String[] args){

String word="hello";

Scanner sc= new Scanner(System.in);

System.out.print("Enter a string- ");

String a= sc.nextLine();

boolean b=a.equals(word);

if (b==true){

System.out.println("The word matches with the string the value is: " +b );

else{

System.out.println("The word doest match with the string the value is: " +b);

OUTPUT:
5-a) Write a Java Program to reverse a string with and without using the library
function
CODE BLOCK:

import java.util.Scanner;
public class reversestring{
public static void main(String[] args){
Scanner sc= new Scanner(System.in);
System.out.print("Enter a string to reverse: ");
String str= sc.nextLine();
String revstr="";
for(int i=str.length()-1;i>=0;i--){
revstr=revstr+str.charAt(i);
}
System.out.println("The reversed string for the input string is: " +revstr);
}
}
OUTPUT:

5b) Write a Java Program to reverse a string with and without using the library
function
CODE BLOCK:
import java.lang.String;

import java.util.Scanner;

public class reversestring{


public static void main(String[] args){

Scanner sc= new Scanner(System.in);

System.out.print("Enter a string to reverse: ");

String str= sc.nextLine();

StringBuilder sb=new StringBuilder(str);

sb.reverse();

String outputstring=sb.toString();

System.out.println("The reversed string for the input string is: " +outputstring);

OUTPUT:

You might also like