Click here to Skip to main content
15,882,464 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Write a program that prompts the user for a number (that may be a fraction). The program reads in the input and print the following: the input as a double, the input as an int and finally the input as a char. e.g. if the input is 65.790, then the output will be 65.790, 65, and A. use the appropriate method of convert class.

What I have tried:

using System;

namespace ConsoleApp08
    class Program
int Main()
            float a;
            printf("Enter a number");
            scanf("%f", &a);
            printf("\ndouble value=%.3f", a);
            printf("\ninteger value=%d", (int)a);
            printf("\nchar value=%c", (char)a);
            return (7);
Updated 28-Jan-22 5:10am
Richard Deeming 28-Jan-22 10:25am    
printf and scanf are not C# methods.

Besides that, you forgot to ask a question.
Richard Deeming 28-Jan-22 11:15am    
No, your homework assignment is not a question. It's a piece of work you are expected to do yourself.

If you really don't know where to start, then talk to your teacher.
PIEBALDconsult 28-Jan-22 10:32am    
And never use the Convert class (except maybe the ChangeType method) -- it's a rookie technique.

1 solution

You can't grab code at random from the internet, chuck it into a C# file, and get a working app: the two languages are very very different, and just share some common syntax.
Much of what makes C work is not going to work in C#: it doesn't use pointers (except in tightly control circumstances) and C doesn't have any clue about classes or references. And while both have a struct keyword, they use it for very different things.

If you need a C# solution, write C# code.
These may help you:
Console.ReadLine Method (System) | Microsoft Docs[^]
Int32.TryParse Method (System) | Microsoft Docs[^]
Double.TryParse Method (System) | Microsoft Docs[^]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900