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

AP Lab 1

Uploaded by

Dhrubo Chaklader
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

AP Lab 1

Uploaded by

Dhrubo Chaklader
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Lab-1

Class Performance
1)public class First
{
public static void main(String args[])
{
int number = 16789;
int firstDigit = 0;

while(number!=0) {
firstDigit = number%10;
number /= 10;
}
System.out.println("First digit: "+firstDigit);
}
}

assignment:

1)import java.util.Scanner;
public class Positive {
public static void main(String[] args) {
System.out.println("Enter a NUmber");
Scanner in = new Scanner(System.in);
int num = in.nextInt();
int count = 0;
while(num>0) {
num = num / 10;
count++;
}
System.out.println("Number of digit in this numbers " + count);
}
}

2)import java.util.Scanner;
class Digit
{
public static void main(String arg[])
{
long n,sum;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number ");
n=sc.nextLong();
for(sum=0 ;n!=0 ;n/=10)
{
sum+=n%10;
}
System.out.println("Sum of digits "+sum);
}
}

3)class Occur
{
// Returns number of times x occurs in arr[0..n-1]
static int countOccurrences(int arr[], int n, int x)
{
int res = 0;
for (int i=0; i<n; i++)
if (x == arr[i])
res++;
return res;
}

public static void main(String args[])


{
int arr[] = {1, 2, 2, 2, 2, 3, 4, 7 ,8 ,8 };
int n = arr.length;
int x = 2;
System.out.println(countOccurrences(arr, n, x));
}
}

You might also like