How to solve this Easy Problem ?

Problem :
Write a program to calculate the addition of 2 integers and print the output.

Input Format:
Line 1 : An integer
Line 2 : An integer

Output:
The output consists of a single integer which corresponds to addition, followed by a new line
Line 1 : Addition of two numbers

Sample Input and Output:
Input
3
1
Output
4
Input
a
b
Output
Invalid Input

Declare it as characters instead of integers

So I guess your problem is how to handle invalid inputs. One way is to read input as string and check if both strings are integers. If so convert them to ints and add them. Else print “Invalid Input”.

First we need to check if the strings are integers. To do so we can use a simple function:

bool isInt(const string s){
    return s.find_first_not_of("0123456789") == string::npos;
}

This function returns true if all string indeces are digits. But if the number is negative it won’t work since ‘-’ isn’t recognized as a digit. So we need to modify it to return true if the only non-digit is the ‘-’ character and appears ONLY in the beginning of the string. So after this modification the code becomes like this:

bool isInt(const string s){
    string s2=s;
    if(s[0]=='-'){
	    s2.erase(s2.begin());
    }
    return s2.find_first_not_of("0123456789") == string::npos;
}

What we do is copy the orgiginal string to a second one and if the original string starts with ‘-’ sign we remove it and perform the first check for rest of the string indeces.

Now if both strings are integers then convert them to ints using stoi (C++11 and later) and print their sum.

Here is the full code:

#include <iostream>
#include <string>
using namespace std;

bool isInt(const string s){
    string s2=s;
    if(s[0]=='-'){
	    s2.erase(s2.begin());
    }
    return s2.find_first_not_of("0123456789") == string::npos;
}


int main(){
    string a,b;
    cin>>a;
    cin>>b;

    if(isInt(a) && isInt(b)){ //both are integers
        int a1=stoi(a), a2=stoi(b);
        cout<<a1+a2<<endl;
    }
    else{
        cout<<"Invalid Input"<<endl;
    }

    return 0;
}

Here is a very simple java solution

import java.io.BufferedReader;
import java.io.InputStreamReader;


public class Main {

public static void main(String[] args)  {

	
BufferedReader r=new BufferedReader(new InputStreamReader(System.in));//creates a fast scanner
int a=0,b=0;//init variables


try{
	 a=Integer.parseInt(r.readLine());//try to get input from console if it is valid then a+b is printed
	 b=Integer.parseInt(r.readLine());//else Invalid input is printed
}catch(Exception e)
{
	System.out.println("Invalid Input");
	System.exit(0);
}
System.out.println(a+b);			//printing a+b
}

}

import java.util.Scanner;

class AddNumbers
{
public static void main(string args[])
{
int x,y,z;
system.out.println(“Enter two integers to calculate their sum”);
Scanner in = new Scanner(System.in);
x=in.nextInt();
y=in.nextInt();
z=x+y;
system.out.println("Sum of entered integers = "+z);
}
}

can anyone help out that if i want to add + and - for the below code i just need an output of invalid input but am getting it as some garbage value:

#include<stdio.h>

int main()

{

int a,b;

scanf("%d",&a);

scanf("%d",&b);

if(a>0 && b>0)

printf("%d\n",a+b);

else

printf("Invalid Input\n");

return 0;

}

thank you for explaining it.