0% found this document useful (0 votes)
5 views6 pages

Program

Uploaded by

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

Program

Uploaded by

Asif Badeghar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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);

You might also like