0% found this document useful (0 votes)
69 views

Program in C

The document contains 14 code snippets with descriptions. Each code snippet demonstrates how to write a Java program to perform various tasks like: 1) Calculate average of array elements 2) Create and initialize 2D array 3) Accept command line arguments and perform calculations 4) Count vowels, characters in strings 5) Check if first letter of arguments is uppercase

Uploaded by

vibz09
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

Program in C

The document contains 14 code snippets with descriptions. Each code snippet demonstrates how to write a Java program to perform various tasks like: 1) Calculate average of array elements 2) Create and initialize 2D array 3) Accept command line arguments and perform calculations 4) Count vowels, characters in strings 5) Check if first letter of arguments is uppercase

Uploaded by

vibz09
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9

AIM: WRITE A PROGRAM,THAT CREATES AND INITIALIZES A FOUR-ELEMENT INT ARRAY.

CALCULATE AND DISPLAY THE AVERAGE OF ITS VALUES class prg3 { public static void main(String args[]) { float avg=0; int[] intarr={100,200,300,400}; for(int i=0;i<4;i++) { System.out.println( i + " element is :" + intarr[i]); avg = avg + intarr[i]; } avg=avg/4; System.out.println("Average of the elements in the array is : "+ avg); } } OUTPUT E:\helloworld\src>java prg3 0 element is :100 1 element is :200 2 element is :300 3 element is :400 Average of the elements in the array is : 250.0

AIM: WRITE A PROGRAM,THAT CREATES A 2-D ARRAY WITH INT VALUES THE FIRST ELEMENT SHOULD BE AN ARRAY CONTAINING -32.THE SECOND ELEMENT SHOULD BE AN ARRAY CONTAINING 500 AND 300.THE THIRD ELEMENT SHOULD BE AN ARRAY CONTAINING 39,45 AND 600.DECLARE,ALLOCATE AND INITIALIZE THE ARRAY DISPLAY ITS LENGTH AND ELEMENTS. class prg4 { public static void main(String args[]) { int[][] intarr={{-32},{500,300},{39,45,600}}; System.out.println("Length of the 2-d array is:"+intarr.length); for(int i=0;i<3;i++) { for(int k=0;k<=i;k++) { System.out.println(i +" "+ k +"Element of the array is : "+ intarr[i][k]); } } } } OUTPUT

E:\helloworld\src>java prg4 Length of the 2-d array is:3 0 0Element of the array is : -32 1 0 Element of the array is : 500 1 1 Element of the array is : 300 2 0 Element of the array is : 39 2 1 Element of the array is : 45 2 2 Element of the array is : 600

AIM: WRITE A PROGRAM,THAT CREATES A 2-D ARRAY WITH INT VALUES THE FIRST SECOND AND THIRD SHOULD BE AN ARRAY WITH TWO,THREE AND FIVE NUMBERS RESPECTIVELY.DISPLAY THE LENGTH OF EACH DIMENSION class prg5 { public static void main(String args[]) { int count=0; int[][] intarr={{10,20},{100,200,300}, {1000,2000,3000,4000,5000}}; System.out.println("Length of the 2-d array is:"+intarr.length); for(int i=0;i<3;i++) { for(int k=0;k<=i;k++) { count=k+1; System.out.println(i +" "+ k +"Element of the array is : "+ intarr[i][k]); } System.out.println("Length of"+" "+(i+1)+" "+"dimension is "+ count); } } } OUTPUT
E:\helloworld\src>java prg5 Length of the 2-d array is:3 0 0 Element of the array is : 10 Length of 1 dimension is 1 1 0 Element of the array is : 100 1 1 Element of the array is : 200 Length of 2 dimension is 2 2 0 Element of the array is : 1000 2 1 Element of the array is : 2000 2 2 Element of the array is : 3000

AIM: WRITE An APPLICATION THAT ACCEPTS TWO DOUBLES AS ITS COMMAND LINE ARGUMENTS, MULTIPLE THESE TOGETHER AND DISPLAY THE PRODUCT class prg9 { public static void main(String args[]) { double c; double a = Double.parseDouble(args[0]); double b = Double.parseDouble(args[1]); c=a*b; System.out.println("Multiplication of the two no is : "+c); } } OUTPUT

E:\helloworld\src>java prg9 2.3 5.6 Multiplication of the two no is : 12.879999999999999

AIM: WRITE An APPLICATION THAT ACCEPTS ONE COMMAND LINE ARGUMENT,DISPLAY THE LINE OF REPORTING IF ITS PRIME OR NOT class prg10 { public static void main(String args[]) { String str; int a = Integer.parseInt(args[0]); if(a%2==0) System.out.println("Number "+ " "+ a +" " +"is" + " Even" ); else System.out.println("Number "+ " "+ a +" " +"is" + " Odd" ); } }

OUTPUT E:\helloworld\src>java prg10 26 Number 26 is Even

AIM: WRITE An APPLICATION THAT ACCEPTS RADIUS OF ITS COMMAND LINE ARGUMENT,DISPLAY THE AREA class prg11 { public static void main(String args[]) { double area; int a = Integer.parseInt(args[0]);

A CIRCLE AS

area = Math.PI*a*a; System.out.println("Area of the circle is :"+area ); } }

OUTPUT E:\helloworld\src>java prg11 26 Area of the circle is :2123.7166338267

AIM: WRITE An APPLICATION THAT COUNTS NO OF VOWELS IN A STRING SPECIFIED AS ITS COMMAND LINE ARGUMENT class prg12 { public static void main(String args[]) { String str[] = new String[10]; String str1[] = {"a","e","i","o","u","A","E","I","O","U"}; int len = 0,count=0; for(int i =0;i<args.length;i++) { len = args[i].length(); for(int k =0;k<len;k++) { str[k] = args[i].substring(k,k+1 ); for(int j=0;j<str1.length;j++) { if(str[k].matches(str1[j])) { count++; } } } } System.out.println("No of vowels in the Argument given is: "+count); } } OUTPUT E:\helloworld\src>java prg12 this is a test text No of vowels in the Argument given is:5

AIM: WRITE An APPLICATION THAT COUNTS TOTAL NO OF CHARACTERS IN ALL OF ITS COMMAND LINE ARGUMENT

class prg13 { public static void main(String args[]) { int len = 0,count=0; for(int i =0;i<args.length;i++) { len = args[i].length(); count=count+len; System.out.println("length of "+args[i]+" is "+len); } System.out.println("Length of the total characters is :"+count); } }

OUTPUT E:\helloworld\src>java prg13 THIS IS A TEST TEXT length of THIS is 4 length of IS is 2 length of A is 1 length of TEST is 4 length of TEXT is 4 Length of the total characters is :15

AIM: WRITE An APPLICATION THAT SEARCHES THROUGH ITS COMMAND LINE ARGUMENTS. IT AN ARGUMENT IS FOUND THAT DOES NOT BEGIN WITH AN UPPER CASE LETTER ,DISPLAY AN ERROR MESSAGE class prg14 { public static void main(String args[]) { char str1; int code; for(int i =0;i<args.length;i++) { str1 = args[i].charAt(0); code = (int)str1; if(code<65||code>92) { System.out.println("One of the word is not in upper case"); break; } } } }

E:\helloworld\src>java prg14 THIS iS A TEST TEXT One of the word is not in upper case

E:\helloworld\src>java prg14 This Is A Test Text

E:\helloworld\src>

You might also like