[go: up one dir, main page]

0% found this document useful (0 votes)
37 views7 pages

Assignment No 1

This document contains 7 questions and answers related to Java programming. It includes programs to extract a portion of a string, count character occurrences in a string, count word occurrences, merge two sorted arrays, reverse digits of a number, output the value of a calculation based on static variables, and print the first 20 Fibonacci numbers.

Uploaded by

AFNAN Waheed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views7 pages

Assignment No 1

This document contains 7 questions and answers related to Java programming. It includes programs to extract a portion of a string, count character occurrences in a string, count word occurrences, merge two sorted arrays, reverse digits of a number, output the value of a calculation based on static variables, and print the first 20 Fibonacci numbers.

Uploaded by

AFNAN Waheed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

Modern Programming Language CS-432

Assignment No: 01

Name:AFNAN WAHEED Reg No.:18-ARID-


5101

Qno 1: Write a JAVA program to extract a portion of a


character string and to print the extracted portion and the
remaining portion of the string. Assume that m characters are
extracted, starting with the nth character
ANS:- class P21
{
    public static void main(String args[ ])
    {
        String str1 = new String();
        String str2 = new String();
        int m=0,n=0;
        DataInputStream in = new DataInputStream(System.in);

        try


        {
            System.out.print("Enter String : ");
            str1 = in.readLine();
            System.out.println(" String is : "+str1);
            System.out.print("Enter no. of chracters to be
extracted from string : ");
            m = Integer.parseInt(in.readLine());
            System.out.print("Enter starting index : ");
            n = Integer.parseInt(in.readLine());
        }
        catch(Exception e) {  System.out.println("I/O Error");   }

       
        str2=str1.substring(n,(m+n));
        System.out.println(" Extracted String is : "+str2);
    }
}
Qno 2: Write a java program to count the number of
occurrences of a particular character in entered string.
class GFG
{
    // Method that return count of the given
    // character in the string
    public static int count(String s, char
c)
    {
        int res = 0;
 
        for (int i=0; i<s.length(); i++)
        {
            // checking character in string
            if (s.charAt(i) == c)
            res++;
        }
        return res;
    }
     
    // Driver method
    public static void main(String args[])
    {
        String str= "geeksforgeeks";
        char c = 'e';
        System.out.println(count(str, c));
    }
}

Qno 3: Write a java program to count the number of


occurrences of a word in a string.
public class WordCount {  
      static int wordcount(String string)  
        {  
          int count=0;  
      
            char ch[]= new char[string.length()];     
            for(int i=0;i<string.length();i++)  
            {  
                ch[i]= string.charAt(i);  
                if( ((i>0)&&(ch[i]!=' ')&&(ch[i-1]==' ')) || ((ch[0]!
=' ')&&(i==0)) )  
                    count++;  
            }  
            return count;  
        }  
      public static void main(String[] args) {  
          String string ="    India Is My Country";  
         System.out.println(wordcount(string) + " words.");   
    }  
}  

Qno 4: Given are two one-dimensional arrays, which are


sorted in ascending order. Write a program to merge them into a
single sorted array that contains every item from both the arrays in
ascending order.
ANS:- // Java program to merge two sorted arrays
import java.util.*;
import java.lang.*;
import java.io.*;

class MergeTwoSorted
{
// Merge arr1[0..n1-1] and arr2[0..n2-1]
// into arr3[0..n1+n2-1]
public static void mergeArrays(int[] arr1, int[] arr2, int n1,
int n2, int[] arr3)
{
int i = 0, j = 0, k = 0;
// Traverse both array
while (i<n1 && j <n2)
{
// Check if current element of first
// array is smaller than current element
// of second array. If yes, store first
// array element and increment first array
// index. Otherwise do same with second array
if (arr1[i] < arr2[j])
arr3[k++] = arr1[i++];
else
arr3[k++] = arr2[j++];
}

// Store remaining elements of first array


while (i < n1)
arr3[k++] = arr1[i++];

// Store remaining elements of second array


while (j < n2)
arr3[k++] = arr2[j++];
}

public static void main (String[] args)


{
int[] arr1 = {1, 3, 5, 7};
int n1 = arr1.length;

int[] arr2 = {2, 4, 6, 8};


int n2 = arr2.length;

int[] arr3 = new int[n1+n2];

mergeArrays(arr1, arr2, n1, n2, arr3);

System.out.println("Array after merging");


for (int i=0; i < n1+n2; i++)
System.out.print(arr3[i] + " ");
}
}

Qno 5: WRITE A PROGRAM USNG WHILE LOOP TO


REVERSE THE DIGITS OF THE NUMBER.

Hint : use the modulus operator to extract the last digit and the
integer division by 10 to get the n-1 digit number from the n digit
number.
NO IS - 12345
ANS IS 54321

1. public class ReverseNumberExample1   
2. {  
3. public static void main(String[] args)   
4. {  
5. int number = 987654, reverse = 0;  
6. while(number != 0)   
7. {  
8. int remainder = number % 10;  
9. reverse = reverse * 10 + remainder;  
10. number = number/10;  
11. }  
12. System.out.println("The reverse of the given number is: 
" + reverse);  
13. }  
14. }  

Qno 6: What is the output of the program below?.


class MyProgram {
static final double pi = 3.15;
static double radius = 2.78;
public static void main(String[] args) {
pi = 3.14;
double area = pi * radius * radius;
System.out.println("The Area is " + area);
}
}
ANS:- The Area is 24.267176
Qno 7: Write a program that prints the first 20 Fibonacci
numbers.
The Fibonacci numbers are defined as follows:
The zeroth Fibonacci number is 1.
The first Fibonacci number is also 1.
The second Fibonacci number is 1 + 1 = 2.
The third Fibonacci number is 1 + 2 = 3.
In other words, except for the first two numbers each Fibonacci
number
is the sum of the two previous numbers.
1. class FibonacciExample1{  
2. public static void main(String args[])  
3. {    
4.  int n1=0,n2=1,n3,i,count=10;    
5.  System.out.print(n1+" "+n2);//printing 0 and 1    
6.     
7.  for(i=2;i<count;++i)//loop starts from 2 because 0 and 
1 are already printed    
8.  {    
9.   n3=n1+n2;    
10.   System.out.print(" "+n3);    
11.   n1=n2;    
12.   n2=n3;    
13.  }    
14.   
15. }}  

You might also like