UNIVERSITY OF LUCKNOW INDEX Sr. Practical Name Date of Practical Signature No. THEORY OF AUTOMATA LAB NCS-354
SUBMITTED BY: SUBMITTED TO:
JANHVI NARAYAN DR. PRIYANKA JAISWAL CSE(AI)-I (P2) 2310013155050
FACULTY OF ENGINEERING AND TECHNOLOGY
UNIVERSITY OF LUCKNOW INDEX Sr. Practical Name Date of Practical Signature No. PROGRAM – 1 Question: Write a program to check whether a number is a Palindrome number or not. Input: import java.util.*; class program {public static void main(String args[]) { Scanner sc= new Scanner(System.in); int num, sum=0, rem, temp; System.out.print("Enter any number to check if it is a Palindrome or not: "); num=sc.nextInt(); temp=num; while(temp>0) { rem=temp%10; sum=sum*10+rem; temp=temp/10;} if(sum==num){ System.out.println("The number is a Palindrome number."); }else {System.out.println("The number is not a Palindrome number."); }}}
Output: //Janhvi Narayan
PROGRAM – 2 Question: Write a program to find the sum of the digits of a number. Input: import java.util.*; class SumofDigits { public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter a number: "); int num=sc.nextInt(); int sum=0; while(num!=0) { sum+=num%10; num/=10; } System.out.println("The sum of digits is:"+sum); } }
Output: //Janhvi Narayan
PROGRAM – 3 Question: Write a program to find the sum and average of two numbers. Input: import java.util.Scanner; class Sum_Avg { public static void main(String args[]) { Scanner sc=new Scanner(System.in); float a,b; System.out.println("Enter the 1st number: "); a=sc.nextFloat(); System.out.println("Enter the 2nd number: "); b=sc.nextFloat(); float sum=a+b; System.out.println("The sum is: "+sum); float avg=sum/2; System.out.println("The average is: "+avg); } }
Output: //Janhvi Narayan
PROGRAM – 4 Question: Write a program to compute the sum of the first and last digits of a number. Input: import java.util.*; class Sum_first_last {public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.print("Enter any number: "); int num=sc.nextInt(); int temp=num; int last=num%10; while(num>=10) {num/=10;} int first=num; int sum=first+last; System.out.println("The sum of the first and last digit: "+sum); } }
Output: //Janhvi Narayan
PROGRAM – 5 Question: Write a program to swap two numbers without using third variable. Input: import java.util.*; class Swap { public static void main(String a[]) { System.out.println("Enter the value of x and y"); Scanner sc = new Scanner(System.in); /*Define variables*/ int x = sc.nextInt(); int y = sc.nextInt(); System.out.println("Before swapping numbers: "+x +" "+ y); /*Swapping*/ x = x + y; y = x - y; x = x - y; System.out.println("After swapping: "+x +" " + y); } }
Output: //Janhvi Narayan
PROGRAM – 6 Question: Write a program to check whether a number is Armstrong number or not. Input: import java.util.Scanner; public class ArmstrongNumber public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); int number = scanner.nextInt(); if (isArmstrong(number)) { System.out.println(number + " is an Armstrong number.");} else {System.out.println(number + " is not an Armstrong number.");}} private static boolean isArmstrong(int number) { int originalNumber = number; int numDigits = String.valueOf(number).length(); int sum = 0; while (number > 0) { int digit = number % 10; sum += Math.pow(digit, numDigits); number /= 10;} return sum == originalNumber;}}
Output: //Janhvi Narayan
PROGRAM – 7 Question: Write a program to print Floyd’s Triangle. 1 23 456 .............. 91 Input: public class patternOf1_91 { public static void main(String[] args) { int num=1 ,row=1; while(num<=91){ for(int col=1;col<=row;col++){ System.out.print(num+" "); num++;} System.out.println(); row++; }}}
Output: //Janhvi Narayan
PROGRAM – 8
Question: Write a program to print given * pattern.
* ** ***
Input: public class StarPattern {
public static void main(String[] args) {
int rows = 3; // Number of rows in the pattern // Outer loop for rows for (int i = 1; i <= rows; i++) { // Inner loop for printing stars in each row for (int j = 1; j <= i; j++) { System.out.print("* ");} System.out.println(); // Move to the next line}}}
Output: //Janhvi Narayan
PROGRAM – 9 Question: Write a program to check whether a number is even number or not. Input: import java.util.Scanner; public class EvenOddChecker { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); int number = scanner.nextInt(); if (number % 2 == 0) { System.out.println(number + " is an even number."); } else { System.out.println(number + " is an odd number."); } scanner.close(); } }
Output: //Janhvi Narayan
PROGRAM – 10 Question: Write a program which prints your name using command line arguments. Input: public class PrintName { public static void main(String[] args) { // Check if any arguments were passed if (args.length > 0) { System.out.println("Your name is: " + args[0]+" "+args[1]); } else { System.out.println("No name provided!"); } } }