Interview Question
Country: United States
import java.util.Scanner;
class count1
{
public static void main(String arr[])
{
Scanner sc=new Scanner(System.in);
int []arry;
int num=0,count=0,temp=0;
System.out.println("How many Elements ?");
num=sc.nextInt();
arry=new int[num];
System.out.println("Enter Elements :");
while(--num>=0)
{
arry[num]=sc.nextInt();
}
for(num=0;num<arry.length;num++)
{
temp=Math.abs(arry[num]);
while(temp!=0)
{
if((temp%10)==1)
count++;
temp=temp/10;
}
}
System.out.println(count+" times");
}
}
package com.careercup;
- satys July 28, 2014public class Count1InGivenarray
{
/**
* @param args
*/
public static void main( String[] args )
{
int arr[]={1,2,3,4,44,31};
int count=0;
for( int i = 0; i < arr.length; i++ )
{
String str=""+arr[i];
char ch[]=str.toCharArray();
for( int j = 0; j < ch.length; j++ )
{
if(ch[j]=='1'){
count++;
}
}
}
System.out.println("count "+count);
}
}