Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So far I have only been able to figure out how to allow the user to input an integer.

What I have tried:

C#
using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Input integer");
            //ask user to input integer
            String n = Console.ReadLine();
            string nn = Console.ReadLine();
            string nnn = Console.ReadLine();
            //allows user to input integer

        }
    }
}
Posted
Updated 22-Mar-18 22:44pm
v2
Comments
CPallini 23-Mar-18 4:09am    
Nope, so far you have only figured out how to allow the user to input three strings.
BillWoodruff 23-Mar-18 12:59pm    
Assuming n = 72: please clarify if "nn" means 72*72 or means 72 * 2 or means 7272

Read the question again:
It clear says "accepts an integer(n)" and "computes the value n + nn + nnn"
I.e. Read a value from the user (for example 72) and work out the value of:
72 + 7272 + 727272 = 734616


You aren't doing that: you are trying to read three values.

This is your homework, so I'll give you no code. But it's pretty simple:
1) Read a value from the user into a string -> n
2) Create a new string nn and append the original string twice
3) Create a new string nnn and append the original string three times
4) Convert each of these strings to a numeric value: check your notes and you'll find information of either Convert.ToInt32 (basic and bad) or int.TryParse (much better, but harder to use). put them in three new integer variables iN, iNN, and iNNN.
5) Add together iN, iNN, and iNNN, then print the result.
 
Share this answer
 
Comments
Jon McKee 23-Mar-18 4:00am    
Griff: "...Convert.ToInt32 (basic and bad) or int.TryParse (much better, but harder to use)."

They have different meanings to me. Convert.ToInt32 is used when you expect success (failure is exceptional) while Try patterns are used when failure is expected occasionally and specific failure information isn't needed.
OriginalGriff 23-Mar-18 4:35am    
It's user input you are converting - you should always expect problems! :laugh:
Jon McKee 23-Mar-18 16:15pm    
True, haha. I was more footnoting because "basic and bad" and "better but harder" feel like general statements. ToInt32 never stops being "basic" even when it's good to use :thumbs_up:

I'm still trying to figure out whether they mean "12 + 1212 + 121212", "12 + 12^2 + 12^3", or "12 + 2*12 + 3*12".
CPallini 23-Mar-18 4:06am    
5.
George Swan 23-Mar-18 8:19am    
I think the problem set using algebraic notion so n+nn+nnn where n=2 equates to 2+4+8
Quote:
Write a C# program that accepts an integer (n) and computes the value of n+nn+nnn.

Assuming that n < 10.
nn = 10 * n + n
nnn = 100 * n + 10 * n + n

If you read n as a string, you can build nn and nnn by concatenating n to itself.
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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