convert c code to c++
Sure! Here is a response:
C Code:
#include <stdio.h>
int main() {
int number1, number2, sum;
printf("Enter first number: ");
scanf("%d", &number1);
printf("Enter second number: ");
scanf("%d", &number2);
sum = number1 + number2;
printf("Sum is: %d", sum);
return 0;
}
C++ Code:
#include <iostream>
int main() {
int number1, number2, sum;
std::cout << "Enter first number: ";
std::cin >> number1;
std::cout << "Enter second number: ";
std::cin >> number2;
sum = number1 + number2;
std::cout << "Sum is: " << sum;
return 0;
}
Explanation:
1. Replaced #include <stdio.h>
with #include <iostream>
to enable C++ input/output operations.
2. Replaced printf
and scanf
with std::cout
and std::cin
respectively for C++ style input/output.
3. No changes required for variable declarations and arithmetic operations.
4. Modified printf
statement to std::cout
and scanf
to std::cin
for taking input and output in C++.