Thursday, October 23, 2014

How to add two numbers using C and java



USING C

#include<stdio.h>
#include<conio.h>
int main()
{
   int x,y,sum;
   x=5;
   y=6;
   sum=x+y;
   printf(" the summation is: %d ", sum);
// %d is use of integer type variables//
   return 0;
}


USING JAVA


public static void main(String[] args)
{
int firstvalue = 5;
int secondvalue = 6;
int sum = firstvalue + secondvalue ;
System.out.println(" the summation is: " + sum);
}

..........................................................................................................

in both  java and c the output will be

the summation is: 11

...........................................................................................................

IF WE WANT THE USER TO INPUT THE NUMBERS:

USING C

#include<stdio.h>
#include<conio.h>
int main()
{
   int x,y,sum;
   printf("enter the first value:");
   scanf("%d", &x);
   printf("Enter the second value:");
   scanf("%d", &y);
   sum=x+y;
   printf(" the summation is: %d ", sum);
// %d is use of integer type variables//
   return 0;
}

USING JAVA

import java.util.Scanner;
{
static Scanner sc= new Scanner(System.in);
        public static void main(String[] args)
       {
int firstvalue;
int secondvalue;
System.out.print("Enter the first value:");
firstvalue= sc.nextInt();
System.out.print("Enter the Second value:");
secondvalue=sc.nextInt();
int sum = firstvalue + secondvalue ;
System.out.println("the summation is:" + sum);



}


}

0 comments:

Post a Comment