Computer Science, asked by chouhanakash40, 1 year ago

write a program to calculate total no. of character's used to write one to one thousand like one+two+three+four+five=19

Attachments:

Answers

Answered by chouhanakash4078
0

import java.util.*;

class Count{

static int combine(String s1,String s2,String a[])

{

int i,t,c=0,sum=0;

for(i=0;i<a.length;i++)

{

t=s1.length()+a[i].length();

c=c+t;

}

sum=c+s2.length();

return sum;

}


static int combine1(String s1,String s2,String s3[],String s4[],String s5[],String s6)

{

int i,j,t,c=0,sum=0;

for(i=0;i<10;i++)

{

if(i==0)

{

for(j=0;j<9;j++)

{

t=s1.length()+s2.length()+s3[j].length();

c=c+t;

}

sum=c+s3[i].length()+s2.length()+s4[i].length();

}

else if(i==1)

{

for(j=0;j<9;j++)

{

t=s1.length()+s2.length()+s5[j].length();

c=c+t;

}

sum=c+s3[i].length()+s2.length()+s4[i].length();

}

else

{

for(j=0;j<9;j++)

{

t=s1.length()+s2.length()+s4[i-1].length()+s3[j].length();

c=c+t;

}

if(i==9)

{

sum=c+s6.length()+s2.length();

}

else if(i==9 || s1.equals("nine"))

{

sum=c+s3[0].length()+s6.length();

}

else{

sum=c+s3[i].length()+s2.length()+s4[i].length();

}

}

}

return sum;

}

public static void main(String []arg)

{

int i,j,c=0,n=0,a,b,count=0;

String arr[]={"one","two","three","four","five","six","seven","eight","nine","thousend"};

String arr1[]={"eleven","twalve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};

String arr2[]={"ten","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety","hundrad"};


for(i=0;i<9;i++)

{

n=arr[i].length();

c=c+n;

}

a=c+arr2[0].length();

c=0;

for(i=0;i<9;i++)

{

n=arr1[i].length();

c=c+n;

}

b=c+arr2[1].length();

count=a+b;

for(i=1;i<9;i++)

{

count=count+combine(arr2[i],arr2[i+1],arr);

}


for(i=0;i<9;i++)

{

count=count+combine1(arr[i],arr2[9],arr,arr2,arr1,arr[i+1]);

}

System.out.println(count);

}

}

Similar questions