Kpit solution
Telegram channel : https://t.me/jobs_unlimted
Telegram group : https://t.me/online_examdiscusion
1. Recursion of no.
public class AddNumbers {
public static void main(String[] args) {
int number = 20;
int sum = addNumbers(number);
System.out.println("Sum = " + sum);
}
public static int addNumbers(int num) {
if (num != 0)
return num + addNumbers(num - 1);
else
return num;
}
}
2.PERFECT NUMBER
Class number{
{
static boolean isPerfect(int n)
{
int sum = 1;
for (int i = 2; i * i <= n; i++)
{
if (n % i==0)
{
if(i * i != n)
sum = sum + i + n / i;
else
sum = sum + i;
}
}
if (sum == n && n != 1)
return true;
return false;
}
public static void main (String[] args)
{
System.out.println("Below are all perfect" +
"numbers till 10000");
for (int n = 2; n < 10000; n++)
if (isPerfect(n))
System.out.println( n +
" is a perfect number");
}
}
Count no of words in a given sentence
Comments
Post a Comment