In this blog I was discuss about how to write a simple
program in any language, because of most of most of the beginners are facing
struggle to write a program, so to avoid that confusion read below article.
First of all clearly understand what the program do and find
out the solution of that.
Please see the below example:
In this example I was performed arithmetic operations like
Addition, Subtraction, Multiplication and Division.
For any program write an algorithm for that then start
writing of code in your desired programming language.
Algorithm:
Start
Take input for two numbers.
Perform the operation (Addition)
Display the Result
Stop.
Flow Chart for Addition of Two Numbers :
If you observe the above diagram first
it starts the process and take input and perform the operation on it, finally
print the result.
Pseudo code for Addition of two numbers:
Begin
Take two variables A,B.
Assign values to them. (take input)
A =3, B=4;
Print A+B;
End
As a beginner first concentrate about the flow chart and algorithm then it is simple to write the program in any language like C, C++ or Java ...etc.
Addition of two number in C:
#include<stdio.h>
int main()
{
int a, b, c;
printf("Enter two numbers to add\n");
scanf("%d%d",&a,&b);
c = a + b;
printf("Sum of entered numbers = %d\n",c);
return 0;
}
Addition of Two Numbers in C++:
#include <iostream>
using namespace std;
int main()
{
int a, b, c;
cout << "Enter two numbers to add\n";
cin >> a >> b;
c = a + b;
cout <<"Sum of entered numbers = " << c << endl;
return 0;
}
Addition of Two numbers in Java:
import java.util.*;
class SumofTwoNumbers
{
public static void main(String[] args)
{
int a,b,sum;
System.out.println("Enetr a value to A and B:");
Scanner sr = New Scanner(System.in);
a = sr.nextInt();
b = sr.nextInt();
sum = a+b;
System.out.println("addition of two numbers is:"+ sum);
}
}
Tags
Computer Science