Write a program to check no is happy no or not and perform operations
import [Link];
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){
[Link]("Happy nos are");
for(int i=1;i<=n;i++){
if(ishappy(i)){
[Link](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([Link]);
[Link]("Enter the no");
int n=[Link]();
boolean b=ishappy(n);
if(b)
[Link]("Happy no");
else
[Link]("Not happy no");
happyno(n);
int cnt=counthappy(n);
[Link]("\ntotal happy are "+cnt);
int sm=sumhappy(n);
[Link]("total happy are "+sm);
Write the program to count the no of bits
import [Link];
public class SetedNo {
static int countbits(int n){
int count=0;
while(n>0){
if(n%2==1){
count++;
n=n/2;
return count;
}
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
[Link]("enter the no");
int n=[Link]();
int s=countbits(n);
[Link]("the no of bits "+s);
Output
enter the no
the no of bits 3
Write a program to check no is happy no or not and perform operations
import [Link];
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){
[Link]("Happy nos are");
for(int i=1;i<=n;i++){
if(ishappy(i)){
[Link](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([Link]);
[Link]("Enter the no");
int n=[Link]();
boolean b=ishappy(n);
if(b)
[Link]("Happy no");
else
[Link]("Not happy no");
happyno(n);
int cnt=counthappy(n);
[Link]("\ntotal happy are "+cnt);
int sm=sumhappy(n);
[Link]("total happy are "+sm);