Please somebody explain it to me i really want to learn it

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include<iostream.h>
#include<conio.h>
void single(int);
void ten2ninety(int);
void elev2nine(int);
void dig2word(int);
void main()
{
clrscr();
int a;
cout<<"\n Enter a number\n";
cin>>a;
dig2word(a);
getch();
}
void dig2word(int y)
{
int s=y,ctr=0,ldig,sdig,l2dig,l3dig,l3rddig;
while(y>0)
{
y=y/10;
++ctr;
}
if(ctr==1)
{
single(s);
return;
}
else if(ctr==2)
{
ldig=s%10;
s=s/10;
sdig=s%10;
 if(ldig==0)
 {
   ten2ninety(sdig);
   return;
 }
 if(sdig==1)
 {
 elev2nine(ldig);
 return;
 }
ten2ninety(sdig);
single(ldig);
return;
}
       else if(ctr==3)
       {
l2dig=s%100;
s=s/100;
sdig=s%10;
if(l2dig==0)
{
single(sdig);
cout<<" Hundred";
return;
}
else
{
single(sdig);
cout<<" Hundred";
dig2word(l2dig);
return;
}
       }
       else if(ctr==4)
       {
l3dig=s%1000;
s=s/1000;
sdig=s%10;
if(l3dig==0)
{
single(sdig);
cout<<" Thousand";
return;
}
else
{
single(sdig);
cout<<" Thousand";
dig2word(l3dig);
return;
}
       }
      else if(ctr==5)
       {
l2dig=s%100;
l3dig=s%1000;
s=s/100;
l3rddig=s%10;
s=s/10;
if(l2dig==0&&l3rddig==0)
{
dig2word(s);
cout<<"Thousand";
return;
}
dig2word(s);
cout<<"Thousand";
dig2word(l3dig);
return;
       }
}
void single(int x)
{
 switch(x)
 {
  case 0:cout<<" Zero ";break;
  case 1:cout<<" One ";break;
  case 2:cout<<" Two ";break;
  case 3:cout<<" Three ";break;
  case 4:cout<<" Four ";break;
  case 5:cout<<" Five ";break;
  case 6:cout<<" Six ";break;
  case 7:cout<<" Seven ";break;
  case 8:cout<<" Eight ";break;
  case 9:cout<<" Nine ";break;
 }
}
void elev2nine(int x)
{
 switch(x)
 {
  case 1:cout<<" Eleven ";break;
  case 2:cout<<" Tweleve ";break;
  case 3:cout<<" Thirteen ";break;
  case 4:cout<<" Fourteen ";break;
  case 5:cout<<" Fifteen ";break;
  case 6:cout<<" Sixteen ";break;
  case 7:cout<<" Seventeen ";break;
  case 8:cout<<" Eighteen ";break;
  case 9:cout<<" Nineteen ";break;
 }
}
void ten2ninety(int x)
{
 switch(x)
 {
  case 1:cout<<" Ten ";break;
  case 2:cout<<" Twenty ";break;
  case 3:cout<<" Thirty ";break;
  case 4:cout<<" Forty ";break;
  case 5:cout<<" Fifty ";break;
  case 6:cout<<" Sixty ";break;
  case 7:cout<<" Seventy ";break;
  case 8:cout<<" Eighty ";break;
  case 9:cout<<" Ninety ";break;
 }
}
Whats there that creating problem for you? Is it not running, wrong output or you dont understand it?
If you dont understand this, I'd advise you to learn about functions.
i know about functions but i have a problem with
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
int s=y,ctr=0,ldig,sdig,l2dig,l3dig,l3rddig;
while(y>0)
{
y=y/10;
++ctr;
}
if(ctr==1)
{
single(s);
return;
}
else if(ctr==2)
{
ldig=s%10;
s=s/10;
sdig=s%10;
 if(ldig==0)
 {
   ten2ninety(sdig);
   return;
 }
 if(sdig==1)
 {
 elev2nine(ldig);
 return;
 }
ten2ninety(sdig);
single(ldig);
return;
}
       else if(ctr==3)
       {
l2dig=s%100;
s=s/100;
sdig=s%10;
if(l2dig==0)
{
single(sdig);
cout<<" Hundred";
return;
}
else
{
single(sdig);
cout<<" Hundred";
dig2word(l2dig);
return;
}
       }
       else if(ctr==4)
       {
l3dig=s%1000;
s=s/1000;
sdig=s%10;
if(l3dig==0)
{
single(sdig);
cout<<" Thousand";
return;
}
else
{
single(sdig);
cout<<" Thousand";
dig2word(l3dig);
return;
}
       }
      else if(ctr==5)
       {
l2dig=s%100;
l3dig=s%1000;
s=s/100;
l3rddig=s%10;
s=s/10;
if(l2dig==0&&l3rddig==0)
{
dig2word(s);
cout<<"Thousand";
return;
}
dig2word(s);
cout<<"Thousand";
dig2word(l3dig);
return;
       }
}
Last edited on
basically i have a problem with each mathematical portion
for example }
else if(ctr==5)
{
l2dig=s%100;
l3dig=s%1000;
s=s/100;
l3rddig=s%10;
s=s/10;
if(l2dig==0&&l3rddig==0)
Wow!
The name of variables of this code is really Bad! try to fix them!
Maybe i will get headache if i want to read this code!

You should use indentation correctly, its quite hard to follow your code.
just explain to me mathematical portion i mean why dividing an taking "mod" of number entered by user
Modified.........
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//ones,tenths,hundredths,thousandths,tenthousandths,hundredthousandths
#include<iostream.h>
#include<conio.h>
void single(int);
void ten2ninety(int);
void elev2nine(int);
void dig2word(int);
void main()
{
	
	int a;
	cout<<"\n Enter a number\n";
	cin>>a;
	dig2word(a);
	getch();
}
void dig2word(int y)
{			
	int s=y,ctr=0,ones,tenths,hundredths,thousandths,tenthousandths;
	while(y>0)
		
	{
		y=y/10;
		++ctr;
	}
	if(ctr==1)
	{
		single(s);
		return;
	}
	else if(ctr==2)
	{
		ones=s%10;
		s=s/10;
		tenths=s%10;
		if(ones==0)
		{
			ten2ninety(tenths);
			return;
		}
		if(tenths==1)
		{
			elev2nine(ones);
			return;
		}
		ten2ninety(tenths);
		single(ones);
		return;
	}
	else if(ctr==3)
	{
		hundredths=s%100;
		s=s/100;
		tenths=s%10;
		if(hundredths==0)
		{
			single(tenths);
			cout<<" Hundred";
			return;
		}
		else
		{
			single(tenths);
			cout<<" Hundred";
			dig2word(hundredths);
			return;
		}
	}
	else if(ctr==4)
	{
		thousandths=s%1000;
		s=s/1000;
		tenths=s%10;
		if(thousandths==0)
		{
			single(tenths);
			cout<<" Thousand";
			return;
		}
		else
		{
			single(tenths);
			cout<<" Thousand";
			dig2word(thousandths);
			return;
		}
	}
	else if(ctr==5)
	{
		hundredths=s%100;
		thousandths=s%1000;		
		s=s/100;			
		tenthousandths=s%10;
		s=s/10;
		if(hundredths==0&&tenthousandths==0)
		{
			dig2word(s);
			cout<<"Thousand";
			return;
		}
		dig2word(s);
		cout<<"Thousand";
		dig2word(thousandths);
		return;
	}
}
void single(int x)
{
	switch(x)
	{
	case 0:cout<<" Zero ";break;
	case 1:cout<<" One ";break;
	case 2:cout<<" Two ";break;
	case 3:cout<<" Three ";break;
	case 4:cout<<" Four ";break;
	case 5:cout<<" Five ";break;
	case 6:cout<<" Six ";break;
	case 7:cout<<" Seven ";break;
	case 8:cout<<" Eight ";break;
	case 9:cout<<" Nine ";break;
	}
}
void elev2nine(int x)
{
	switch(x)
	{
	case 1:cout<<" Eleven ";break;
	case 2:cout<<" Tweleve ";break;
	case 3:cout<<" Thirteen ";break;
	case 4:cout<<" Fourteen ";break;
	case 5:cout<<" Fifteen ";break;
	case 6:cout<<" Sixteen ";break;
	case 7:cout<<" Seventeen ";break;
	case 8:cout<<" Eighteen ";break;
	case 9:cout<<" Nineteen ";break;
	}
}
void ten2ninety(int x)
{
	switch(x)
	{
	case 1:cout<<" Ten ";break;
	case 2:cout<<" Twenty ";break;
	case 3:cout<<" Thirty ";break;
	case 4:cout<<" Forty ";break;
	case 5:cout<<" Fifty ";break;
	case 6:cout<<" Sixty ";break;
	case 7:cout<<" Seventy ";break;
	case 8:cout<<" Eighty ";break;
	case 9:cout<<" Ninety ";break;
	}
}
amirtork and Softrix i changed names of variables .now i hope you guy's will better understand it........................if u can explain it to me please
Last edited on

Do you know how modulus works?

Lets assume I entered the value 10531, modulus is used to find out how many thousands, hundreds, tens etc., and is used to breakdown your integer.

So take this example..

hundredths = s % 100; is simply performing a calculation to determine what the remainder is, so 10531 / 100 = 105 remainder 31 (so here you can see what's below 100 i.e. 31).

You then do this for the thousands, tens etc.
oh , Thank you soooo much......... Softrix!
Topic archived. No new replies allowed.