1. Write a program to generate a Pascal Triangle in which each number in the row is the sum of the two numbers immediately above it. The program should print the first 5 rows of the triangle. public class ques39
{
public static void main()
{
int size=5;
int a[][]=new int[size][size];
int i,j;
a[0][0]=1;
// prepare the pascal triangle //
for(i=1;i<size;i++)
{
for(j=0;j<size;j++)
{
if((i==j) || (j==0))
a[i][j]=1;
else
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
}
// display the pascal triangle //
for(i=0;i<size;i++)
{
for(j=1;j<=15-i;j++)
System.out.print(" ");
for(j=0;j<=i;j++)
System.out.print(a[i][j]+" ");
System.out.println();
}
}
} 2. Assign 10 numbers into an array and then find the Highest and Lowest value in the array. public class maxmin
{
public static void main()
{
int i;
int a[]={1,2,3,4,5,6,7,8,9,10};
int max=a[0],min=a[0];
for(i=0;i<10;i++)
{
if(a[i]>max)
max=a[i];
if(a[i]<min)
min=a[i];
}
System.out.println("Maximum = "+max);
System.out.println("Minimum = "+min);
}
} 3. WAP to accept 10 numbers in an array. Then ask the users to enter a Number and search it in array (using binary search method). Display the position if number is found otherwise display "not found". public class binarySearch
{
public static void main(int x)
{
int a[]={4,6,10,13,15,18,20};
int l,h,m;
char ans='n';
l=0;
h=a.length-1;
m=(l+h)/2;
while(h>=l && ans=='n')
{
m=(l+h)/2;
if(a[m]>x)
h=m-1;
else
if(a[m]<x)
l=m+1;
else
{
ans='y';
break;
}
}
if(ans=='y')
System.out.println("found in position "+(m+1));
else
System.out.println("number not found in array");
}
} 4. Assign numbers into an array of size 4X4. Then calculate the sum of each row total and column total. public class RowColTotal
{
public static void main()
{
int a[][]={{4,5,8,1},{2,8,7,4},{3,4,5,6},{1,2,7,8}};
int i,j,Row=0,Col=0;
for(i=0;i<4;i++)
{
Row=0;
for(j=0;j<4;j++)
{
Row+=a[i][j];
}
System.out.println("Row "+(i+1)+" total is : "+Row);
}
for(i=0;i<4;i++)
{
Col=0;
for(j=0;j<4;j++)
{
Col+=a[j][i];
}
System.out.println("Column "+(i+1)+" total is : "+Col);
}
}
} 5. Assign numbers into two array A & B of size 5. The program should create another array T that finds the common element in both the arrays. (Assume that the numbers are not repeated in same array). public class setIntersection
{
public static void main()
{
int i,j,k=0;
int a[]={1,2,3,4,5},b[]={1,4,5,7,8};
int t[]=new int[5];
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(a[i]==b[j])
{
t[k]=a[i];
k++;
}
}
}
for(i=0;i<k;i++)
System.out.println(t[i]);
}
}
{
public static void main()
{
int size=5;
int a[][]=new int[size][size];
int i,j;
a[0][0]=1;
// prepare the pascal triangle //
for(i=1;i<size;i++)
{
for(j=0;j<size;j++)
{
if((i==j) || (j==0))
a[i][j]=1;
else
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
}
// display the pascal triangle //
for(i=0;i<size;i++)
{
for(j=1;j<=15-i;j++)
System.out.print(" ");
for(j=0;j<=i;j++)
System.out.print(a[i][j]+" ");
System.out.println();
}
}
} 2. Assign 10 numbers into an array and then find the Highest and Lowest value in the array. public class maxmin
{
public static void main()
{
int i;
int a[]={1,2,3,4,5,6,7,8,9,10};
int max=a[0],min=a[0];
for(i=0;i<10;i++)
{
if(a[i]>max)
max=a[i];
if(a[i]<min)
min=a[i];
}
System.out.println("Maximum = "+max);
System.out.println("Minimum = "+min);
}
} 3. WAP to accept 10 numbers in an array. Then ask the users to enter a Number and search it in array (using binary search method). Display the position if number is found otherwise display "not found". public class binarySearch
{
public static void main(int x)
{
int a[]={4,6,10,13,15,18,20};
int l,h,m;
char ans='n';
l=0;
h=a.length-1;
m=(l+h)/2;
while(h>=l && ans=='n')
{
m=(l+h)/2;
if(a[m]>x)
h=m-1;
else
if(a[m]<x)
l=m+1;
else
{
ans='y';
break;
}
}
if(ans=='y')
System.out.println("found in position "+(m+1));
else
System.out.println("number not found in array");
}
} 4. Assign numbers into an array of size 4X4. Then calculate the sum of each row total and column total. public class RowColTotal
{
public static void main()
{
int a[][]={{4,5,8,1},{2,8,7,4},{3,4,5,6},{1,2,7,8}};
int i,j,Row=0,Col=0;
for(i=0;i<4;i++)
{
Row=0;
for(j=0;j<4;j++)
{
Row+=a[i][j];
}
System.out.println("Row "+(i+1)+" total is : "+Row);
}
for(i=0;i<4;i++)
{
Col=0;
for(j=0;j<4;j++)
{
Col+=a[j][i];
}
System.out.println("Column "+(i+1)+" total is : "+Col);
}
}
} 5. Assign numbers into two array A & B of size 5. The program should create another array T that finds the common element in both the arrays. (Assume that the numbers are not repeated in same array). public class setIntersection
{
public static void main()
{
int i,j,k=0;
int a[]={1,2,3,4,5},b[]={1,4,5,7,8};
int t[]=new int[5];
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(a[i]==b[j])
{
t[k]=a[i];
k++;
}
}
}
for(i=0;i<k;i++)
System.out.println(t[i]);
}
}
No comments:
Post a Comment