First & last digit of a number in MATLAB & JAVA
Find the first & last digit of a number :
Example:
Input:12345
Output:First digit --1
Last digit--5
Please try yourself once before checking the video , it will improve your thinking skills
Algorithm & MATLAB & JAVA CODE :
JAVA Code:
import java.util.Scanner;
class Ka
{
public static void main(String args[])
{
Scanner obj=new Scanner(System.in);
System.out.println("Enter the number");
int x=obj.nextInt();
int m=x;
int c=0;
int b=0;
while(m>0)
{
b=m%10;
c=c+1;
m=m/10;
}
int y[]=new int[c];
m=x;
int k=c-1;
while(m>0)
{
b=m%10;
y[k]=b;
m=m/10;
k=k-1;
}
System.out.print("The first number is:"+y[0]);
System.out.println();
System.out.print("The last number is:"+y[c-1]);
}
}
Example:
Input:12345
Output:First digit --1
Last digit--5
Please try yourself once before checking the video , it will improve your thinking skills
Algorithm & MATLAB & JAVA CODE :
JAVA Code:
import java.util.Scanner;
class Ka
{
public static void main(String args[])
{
Scanner obj=new Scanner(System.in);
System.out.println("Enter the number");
int x=obj.nextInt();
int m=x;
int c=0;
int b=0;
while(m>0)
{
b=m%10;
c=c+1;
m=m/10;
}
int y[]=new int[c];
m=x;
int k=c-1;
while(m>0)
{
b=m%10;
y[k]=b;
m=m/10;
k=k-1;
}
System.out.print("The first number is:"+y[0]);
System.out.println();
System.out.print("The last number is:"+y[c-1]);
}
}
No comments