Javalab
Javalab
Javalab
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();
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){
import java.lang.Math;
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) {
else {
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;
String word="hello";
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;
sb.reverse();
String outputstring=sb.toString();
System.out.println("The reversed string for the input string is: " +outputstring);
OUTPUT: