You are on page 1of 3

Write a program to check no is happy no or not and perform operations

import java.util.Scanner;

public class HappyNos {

static boolean ishappy(int n)

while(n>9){

int sum=0;

while(n!=0){

int r=n%10;

sum=sum+r*r;

n=n/10;

n=sum;

return n==1||n==7;

static void happyno(int n){

System.out.println("Happy nos are");

for(int i=1;i<=n;i++){

if(ishappy(i)){

System.out.print(i+ " ");

}
}

static int counthappy(int n){

int count=0;

for(int i=1;i<=n;i++){

if(ishappy(i)){

count++;

return count;

static int sumhappy(int n){

int sum=0;

for(int i=1;i<=n;i++){

if(ishappy(i)){

sum=sum+i;

return sum;

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println("Enter the no");

int n=sc.nextInt();

boolean b=ishappy(n);
if(b)

System.out.println("Happy no");

else

System.out.println("Not happy no");

happyno(n);

int cnt=counthappy(n);

System.out.println("\ntotal happy are "+cnt);

int sm=sumhappy(n);

System.out.println("total happy are "+sm);

You might also like