write a program to read a number and print in words that is
in sentence for example 21,219 then output is "twenty one
thousand and two hundred nineteen" by using only control
flow statements (only loops and switch case )?



write a program to read a number and print in words that is in sentence for example 21,219 then out..

Answer / anil kumar

import java.util.Scanner;


public class numberFormat
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.println("Enter 4 digit number : ");
int num=s.nextInt();
int a=num;

a=num/1000;

if(a==1)
System.out.println("One thousand");
else if(a==2)
System.out.println("Two thousand");
else if(a==3)
System.out.println("Three thousand");
else if(a==4)
System.out.println("Four thousand");
else if(a==5)
System.out.println("Five thousand");
else if(a==6)
System.out.println("Six thousand");
else if(a==7)
System.out.println("Seven thousand");
else if(a==8)
System.out.println("Eight thousand");
else if(a==9)
System.out.println("Nine thousand");
int first=num-a*1000;

int y=first/100;

if(y==1)
System.out.println("One hundred");
else if(y==2)
System.out.println("Two hundred");
else if(y==3)
System.out.println("Three hundred");
else if(y==4)
System.out.println("Four hundred");
else if(y==5)
System.out.println("Five hundred");
else if(y==6)
System.out.println("Six hundred");
else if(y==7)
System.out.println("Seven hundred");
else if(y==8)
System.out.println("Eight hundred");
else if(y==9)
System.out.println("Nine hundred");
int second=first-y*100;

int x=second/10;
if(x==1)
System.out.println("Ten");
else if(x==2)
System.out.println("Twenty");
else if(x==3)
System.out.println("Thirty");
else if(x==4)
System.out.println("Fourty");
else if(x==5)
System.out.println("Fifty");
else if(x==6)
System.out.println("Sixty");
else if(x==7)
System.out.println("Seventy");
else if(x==8)
System.out.println("Eighty");
else if(x==9)
System.out.println("Ninty");
int third=second-x*10;



int z=third;

if (z==2)
System.out.println(" two");
else if (z==3)
System.out.println(" three");
else if (z==4)
System.out.println(" four");
else if (z==5)
System.out.println(" five");
else if (z==6)
System.out.println(" six");
else if (z==7)
System.out.println(" seven");
else if (z==8)
System.out.println(" eight");
else if (z==9)
System.out.println(" nine");



}

}



use switch case in case of if condition you will get it.

Is This Answer Correct ?    3 Yes 0 No

Post New Answer

More C Interview Questions

What is enumerated data type in c?

0 Answers  


read an array and search an element

1 Answers  


Explain the difference between structs and unions in c?

0 Answers  


#include<stdio.h> main() { char *p1; char *p2; p1=(char *) malloc(25); p2=(char *) malloc(25); strcpy(p1,"Ramco"); strcpy(p2,"Systems"); strcat(p1,p2); printf("%s",p1); } Tell me the output?

6 Answers   Ramco,


int a=2,b=3,c=4; printf("a=%d,b=%d\n",a,b,c); what is the o/p?

6 Answers   Verifone,






What is encapsulation?

2 Answers  


Explain what is a const pointer?

0 Answers  


Explain what is the heap?

0 Answers  


What is the output of the following program #include<stdio.h> main() { int i=0; fork(); printf("%d",i++); fork(); printf("%d",i++); fork(); wait(); }

8 Answers   ADITI, Adobe,


void main() { int x=25,y=32; clrscr(); x=x++ + y++; y=++x + ++y; printf("%d%d",x,y); }

5 Answers  


How do you list a file’s date and time?

0 Answers  


how to connect oracle in C/C++.

3 Answers   TCS, Wipro,


Categories