Get Started With Coding – Tutorial For a Simple Addition Problem

1 min read

New to the programming world, yet intrigued? Let’s get you started with the CodeChef journey with a very basic “Add Two Numbers” problem. So in this I shall explain both Python and C++ solutions. 

The problem is to find the sum of 2 integers that we give as input. As mentioned in the problem, the user (or Shivam) inputs the number of test cases (T) in the first line. Then followed by T lines, each line contains two integers A and B. So the logic here is simple, right? First, you need to input the number of test cases, then input the numbers you want to add, and the sum of the numbers is displayed as the output. The input function lets us take a text input from the user and the output function gives the data processed by the program.  

Solution in Python

Let us first take a look at how the program can be executed using Python. Before we get into the solution, we will have a look at the ‘for’ loop. A ‘for’ loop is an entry check loop which executes only if the condition is satisfied at the beginning. You can specify the range for the ‘for’ loop to iterate over. So now we will get into the logic of the problem. 

  • Your first input variable must be ‘ test case’. You can name this variable, for instance, T. 
  • The next input, i.e., the numbers you want to sum, is inputted in a ‘for’ loop. The range for the ‘for’ loop is the integer stored in the test case variable. 
  • Next, the split function is used to split the given string by the specified separator. 
  • The next step would be to input the numbers you want to add, and these are stored in 2 variables, a and b. 
  • Using the print function the sum of the numbers is printed.

Solution in C++

We shall now see how the same logic would be in C++. Instead of using the input() function, here it is ‘cin’. 

  • First, the test case integer is inputted and stored in a variable (refer it to as ‘a’). 
  • Then iterating over the ‘for’ loop from 0 to a+1, the numbers are entered by the user. 
  • Another variable, ‘C’ is created where the summation of the two numbers is stored. 
  • Later the output is printed using the ‘cout’ statement. 

Well, that was easy, right? Now head over and solve more problems!

A Starters Like Never Before | Action-Packed Ranklists &…

The first Starters of the month, which was rated for both Divisions, ended yesterday night, and it would be an understatement to say that...
1 min read

Can You Fly The Best Yet The Cheapest Route…

Hola! When was the last time you boarded a flight for a vacation? Every time we plan and book the flights a minimum of...
2 min read

Rising Stars | October Long Challenge 2021

October Long Challenge 2021, the first-rated challenge of the month, was surreal! While we saw lots of top coders maintain their high ranks and...
1 min read

7 Replies to “Get Started With Coding – Tutorial For a Simple…”

Leave a Reply