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!

Watch The Inspiring Story Of Malvika Raj Joshi Who…

Do you remember the person who got into MIT without the conventional schooling style you are used to? Would it come as another big...
1 min read

Why Do You Badly Need A Well Oiled Coding…

What do super awesome coders do? They harness their knack in technology into a constructive skill. Where does that happen? At the club!  Gamers,...
2 min read

Let’s Build A Coding Club In Your College Together!

A thriving Coding Club in your college is rather pertinent to shaping a promising programming career for you and your classmates. The unparalleled benefits...
2 min read

Leave a Reply