freetimestudys@gmail.com          +91-7097633380
  Login Here                        

sum of n numbers

import java.io.*;
class clssum2
{public static void main(String args[])throws IOException
{BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the value of n");
int n,a,i;int s=0; n = Integer.parseInt(br.readLine());
for(i=1;i<=n;i++)
{a = Integer.parseInt(br.readLine()); s+=a;
}
System.out.println("Sum is"+s);}}

intput and output

Enter the value of n
3
1
4
5
Sum is 10

sum of two numbers using class and object

import java.io.*;
class sum
{
int a;
int b;
void getdata(int c,int d)
{
a=c;
b=d;
}
int sum1()
{
return(a+b);
}}
class clsmain
{
public static void main(String args[])throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
int d,e;
d=Integer.parseInt(br.readLine());
e=Integer.parseInt(br.readLine());
sum r = new sum();
r.getdata(d,e);
System.out.println("The sum is " + r.sum1());
}
}

intput and output

10 20 The sum is 30

sort n numbers

import java.io.*;
class clsarray
{
public static void main(String args[])throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
int i,n,j,t;
int a[] = new int[100];
System.out.println("Enter the value of n");
n=Integer.parseInt(br.readLine());
System.out.println("Enter the values");
for(i=0;i{
a[i]=Integer.parseInt(br.readLine());
}
for(i=0;i{
for(j=i+1;j{
if(a[i]>a[j])
{
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
System.out.println("After sorting");
for(i=0;i{
System.out.println(a[i]);
}
}

}

intput and output

Enter the value of n
3
Enter the values
2
4
3
9
7
After sorting
2
3
4
7
9