Java.RA
- write a program to accept the 'n' different number from user and store it in array. Also print the sum of elements of the array.
import java.util.*;
import java.util.Arrays;
public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n;
System.out.println("Enter the total number of elements ");
n=sc.nextInt();
int arr[]=new int[n];
System.out.println("Enter the elements of the array ");
for(int i=0; i<n ;i++)
{
arr[i]=sc.nextInt();
}
int sum = 0;
while(i!=n)
{
sum=sum+arr[i];
I++; //Increment to iterate to the next element
}
System.out.println("The sum of all the elements in the array is "+sum);
}
}
2.write a program to copy the contents from one file into another file in upper case
import java.io.File;
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException;
public class CopyExample
{
public static void main(String[] args)
{
FileInputStream instream = null; FileOutputStream outstream = null;
try{
File infile =new File("C:\MyInputFile.txt");
File outfile =new File("C:\MyOutputFile.txt");
instream = new FileInputStream(infile);
outstream = new FileOutputStream(outfile);
byte[] buffer = new byte[1024];
int length;
while ((length = instream.read(buffer)) > 0){ outstream.write(buffer, 0, length); }
instream.close(); outstream.close();
System.out.println("File copied successfully!!"); }catch(IOException ioe){ ioe.printStackTrace();
}
}
}