Skip to main content

Sum of two numbers using C, C++ & Java

 To find the sum of two numbers using C, C++ & Java we have two ways :-

1. Hardcode values in the code.

2. Take input from user.


1. Hardcode values in the code using C.

#include<stdio.h>

int main()
{
    int n1=2,n2=3,sum=0;

    sum=n1+n2;
    printf("The sum of 2 number is : %d ",s);
    return 0;
}

1. Hardcode values in the code using C++.


#include <iostream>

using namespace std;

int main()
{
    int n1=2,n2=3,sum=0;
    
    sum = a+b;
    cout<<"\nThe sum of two numbers  : "<<sum;
    return 0;
}

1. Hardcode values in the code using Java.

public class SumOfNumbers1  
{  
    public static void main(String args[])   
    {  
        int n1 = 2, n2 = 3, sum;  
        sum = n1 + n2;  
        System.out.println("The sum of two numbers is: "+sum);  
    }  
}  

2. Take input from user using C

#include<stdio.h>

int main()
{
    int n1,n2,sum=0;
    printf("Enter 2 Number : ");
    scanf("%d%d",&n1,&n2);

    sum=n1+n2;
    printf("The Sum of two number : %d ",sum);
    return 0;
}

2. Take input from user using C++

#include <iostream>

using namespace std;

int main()
{
    int n1, n2, sum=0;
    cout<<"Enter any two numbers : ";
    cin>>n1>>n2;

    sum = n1+ n2;
    
    cout<<"\nSum of 1st three numbers   : "<<sum;
    return 0;
}

2. Take input from user using Java


import java.util.Scanner;  
public class SumOfNumbers2  
{  
    public static void main(String args[])  
    {  
        int n1, n2, sum=0;  
        Scanner sc = new Scanner(System.in);  
        System.out.print("Enter the first number: ");  
        n1 = sc.nextInt();  
        System.out.print("Enter the second number: ");  
        n2 = sc.nextInt();  
        sum = n1+n2; 
        System.out.println("The sum of two numbers n1 and n2 is: " + sum);  
    }  
}  



Comments

Popular posts from this blog

Applet program to find a number is prime or not using Java

import java.applet.*; import java.awt.*; import java.awt.event.*; /* <applet code="prime.class" width="500" height="600"> </applet> */ public class prime extends Applet implements ActionListener { Label lbl1; TextField txt1,txt2; Button btn1; public void init() { lbl1=new Label("Enter any Number : "); txt1=new TextField(20); txt2=new TextField(20); btn1=new Button("Check"); setLayout(null); txt2.setBounds(130,50,200,20); add(txt2); txt1.setBounds(230,100,120,20); add(txt1); lbl1.setBounds(100,100,120,20); add(lbl1); btn1.setBounds(100,120,40,20); add(btn1); btn1.addActionListener(this); } public void actionPerformed(ActionEvent ae) { String num1=txt1.getText(); int i; int k=0; int num; String str1="Prime no."; String str2="Not Prime no."; num=Integer.parseInt(num1); for(i=1;i<=num;i++) { if(num%i==...

Coding questions & answers for beginners

Coding Questions Top 100 Coding questions & solutions for beginners to develop analytical and logical skills. 1. Sum of two numbers using C, C++ & Java 2. Difference of two numbers using C, C++ & Java 3. Product of two numbers using C, C++ & Java 4. Generate Fibonacci series upto a specified number using Java 5. Write a program to find the HCF of two numbers 6. Write a program to find a leap year using Java 7. Write a program to find the sum of digits of a number using Java 8. Write a program to swap two numbers using temporary variable using Java 9. Write a program to swap two numbers without using temporary variable using Java 10. Write a program to find the whole number using Java 11. Write a program to find the armstrong number using Java 12. Write a program to print table of a given number using Java 13. Write a program to find smallest and largest in three numbers using Java 14. Write a program to find area of a rectangle using Java