Thursday, October 23, 2014

Posted by Dollar Dhingra
Find the number of triangles in the given figure.



Answer: 14

Solution:
The figure may be labelled as shown.


The simplest triangles are AHG, AIG, AIB, JFE, CJE and CED i.e. 6 in number.

The triangles composed of two components each are ABG, CFE, ACJ and EGI i.e. 4 in number.

The triangles composed of three components each are ACE, AGE and CFD i.e. 3 in number.

There is only one triangle i.e. AHE composed of four components.

Therefore, There are 6 + 4 + 3 + 1 = 14 triangles in the given figure.



Friday, October 3, 2014

Posted by Dollar Dhingra
Hello there !! This is the second part of the Pattern Programming Questions Series.
I recommend you to check out the First Part if you are beginner.

Following Patterns Will be covered

Pattern 1

A
A B
A B C
A B C D
A B C D E

Pattern 2

1 2 3 4 5
1 2 3 4 
1 2 3
1 2
1


Pattern 3

1 3 5 7 9
3 5 7 9
5 7 9
7 9
9

Pattern 4

* * * * *
   * * * *
      * * *
         * *
            *

Pattern 5

       *
     ***
   *****
 *******

Pattern 6

    1
   232
  34543
 4567654
567898765



Solutions:

Before going through any of the solutions, I strongly recommend you all to first try it for yourself, because "you cannot learn logic, you can only develop it". Zyada ho gya :D :P


Pattern 1

A
A B
A B C
A B C D
A B C D E


The logic here is same as we discussed in many of the questions in Part 1 of this series.
The main difference here is - the letters. You can display letters instead of numbers easily.



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include <stdio.h>

int main()
{ int i,j;

for(i='A'; i<='E'; i++)//outer loop runs from character A to character E
{
    for(j='A'; j<=i; j++)
    {
        printf("%c ",j);//%c prints the character value

    }
    printf("\n");
}
    return 0;
}

Notice two things here:
1. In line number 6, Outer loop runs from 'A' to 'E'
2. In line number 10, We are printing the character value of j by writing %c, i.e A, B and so on... If we would have written %d, 65 would have been printed in the place of A.

You can use the same idea in any of the patterns having Alphabetical letters.

Pattern 2

1 2 3 4 5
1 2 3 4 
1 2 3
1 2
1

This pattern will have the reverse logic. i.e. the outer loop will run from 5 to 1.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include <stdio.h>

int main()
{ int i,j;

for(i=5; i>0; i--)//outer loop runs from 5 to 1
{
    for(j=1; j<=i; j++)
    {
        printf("%d ",j);

    }
    printf("\n");
}
    return 0;
} 

Pattern 3

1 3 5 7 9
3 5 7 9
5 7 9
7 9
9

 This one is a little tricky, but its a variation of the above.



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include <stdio.h>

int main()
{ int i,j;

for(i=9; i>0; i=i-2)//outer loop runs from 9 to 1 with a decrement of 2
{
    for(j=1; j<=i; j=j+2)//increment of 2
    {
        printf("%d ",j);

    }
    printf("\n");
}
    return 0;
}

Pattern 4

* * * * *
   * * * *
      * * *
         * *
            *

Enough of simple ones containing 2 loops !
Just kidding ;) now this involves 3 loops. Why?

look closely





The red triangle represents the pattern of stars, and the purple triangle represents the pattern of blank spaces..
So we have 3 loops, one for purple triangle, one for red triangle, and an outer loop.


Also, we can imagine it as
0 spaces, 5 stars
1 spaces, 4 stars
2 spaces, 3 stars
3 spaces, 2 stars
4 spaces, 1 stars



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>

int main()
{ int i,j,k;

for(i=0; i<5; i++)//outer loop runs from 5 to 1
{
    for(j=0; j<i;j++)
    {
        printf(" ");//printing spaces
    }
    for(k=5;k>i; k--)
    {
        printf("*");//printing stars

    }
    printf("\n");
}
    return 0;
}


Pattern 5

      *
    ***
  *****
*******

Now comes 4 for loops :D but I guess you are comfortable now.. now think about this, how many patterns does it contains ??

This contains 3 patterns in it.





You can have a fair idea how excellent I am in Designing by looking at this picture :P

Look closely, we have 3 triangles now, red for spaces, purple for stars, and  green for more stars.
 









 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>

int main()
{ int i,j,k,l;

for(i=0; i<5; i++)//outer loop runs from 5 to 1
{
    for(j=5; j>i;j--)
    {
        printf(" ");//printing spaces
    }
    for(k=0;k<=i; k++)
    {
        printf("*");//printing stars of the 2nd triangle

    }
    for(l=1;l<=i;l++)
    {
        printf("*");//printing stars in 3rd triangle

    }
    printf("\n");
}
    return 0;
}

Pattern 6

    1
   121
  12321
 1234321 
123454321 

If you have done the above questions properly then you can attempt this one on your own. It may take some time, but you will figure it out.


Imagine the above diagram again in the 3 triangles.





 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>

int main()
{ int i,j,k,l;

for(i=0; i<5; i++)//outer loop runs from 0 to 5(number of rows)
{
    for(j=4; j>i;j--)
    {
        printf("  ");//printing spaces
    }
    for(k=1;k<=i; k++)
    {
        printf(" %d", k);//printing numbers of the 2nd triangle

    }
    for(l=k;l>=1;l--)
    {
        printf(" %d", l);//printing numbers in 3rd triangle
    }
    printf("\n");
}
    return 0;
}



For any doubts, suggestions or corrections please type in the comments below.

It takes a lot of time and effort, please support us by sharing this and liking us on Facebook.
This will help us grow and will inspire us in growing. happy coding. :)

Friday, August 15, 2014

Posted by Anmol Sharma
In the previous article, we discussed about two basic file handling functions in Java including searching for an existing created file in the system and creating a file using java’s inbuilt function.

In this article we will cover up the other two file handling operations which includes reading from a file and writing to a file using java’s own methods. In this program we create a file by specifying a file name, open it at the same time and add records to it as per custom format. After writing to the file, it is good practice to close it.

import java.io.*;
import java.lang.*;
import java.util.*;

public class FileHandlingTrial {

       private Formatter file;
      
              public void openFile (){ //creating & opening a file
                    
                     try{
                           file= new Formatter("New Text Document.txt"); //will be created in default workspace folder else specify a path
                     }
                    
                     catch(Exception e){
                           System.out.println("Illegal file operation");
                                 
                           }
                     }
                    
                     public void addRecords(){// writes to the created file
                           file.format("%s %s %s", "99", "MCA", "IP University");
                    
                     }
                    
                     public void closeFile(){ //good practice to close the file after use
                           file.close();
                     }
      
}


class FileHandlingTrial1 {
      
       public static void main(String[] args){
             
         FileHandlingTrial asp = new FileHandlingTrial();
        
         asp.openFile();
         asp.addRecords();
         asp.closeFile();
             
       }
}


In this last program of file handling we will read the content added by the user.
Here also we create three methods in which first opens the file, second reads the content of the file and third one finally closes the file after reading the contents.


import java.io.*;
import java.util.*;



public class FileHandlingTrial {

private Scanner file;

public void openFile(){ // to open the file
      
       try{
              file = new Scanner(new File("new.txt"));
             
             
       }
      
       catch(Exception e){
             
              System.out.println("File not found");
       }
}

public void readFile(){ // to read data from the file
      
       while(file.hasNext()){
              String a=file.next();
              String b=file.next();
              String c=file.next();
              String d=file.next();
             
              System.out.printf("%s %s %s %s\n", a, b, c, d);
             
}

}

public void closeFile(){
file.close();
}
}


class FileHandlingTrial1 {
      
       public static void main(String[] args){
             
         FileHandlingTrial asp = new FileHandlingTrial();
        
         asp.openFile();
         asp.readFile();
         asp.closeFile();
             
       }
}

Screenshot // click to open a higher resolution version





Thursday, August 14, 2014

Posted by Dollar Dhingra
Hello There, this is the first part of the series Pattern Programming Questions.
The First Part is specially for beginners who are new to programming and have basic understanding of loops and printing functions. In this part, I will walk you through some basic patterns.

Pattern Questions are not only the frequently asked questions in the interviews but also help you in developing the basic logic in programming. I will explain you some of the basic ones, and leave the rest for you to try.

Following patterns will be covered:

Pattern 1:

*****
*****
*****
*****


Pattern 2:

12345
12345
12345
12345

Pattern 3:

11111
22222
33333
44444

Pattern 4:

11111
22222
33333
44444

Pattern 5:

1
12
123
1234
12345

Pattern 6:

1
22
333
4444
55555

Pattern 7:

1
2   3
4   5   6
7   8   9   10
11 12 13 14 15


Pattern 1:
Lets start with the very basic one.

*****
*****
*****
*****

In the above  pattern there are 5 columns (left to right) and 4 rows(top to bottom).

We take rows as r and columns as c and we will use 2 loops for this, one for printing the rows, and other for columns.

 pseudocode----

r=5, c=5;
for(i=0;i<r;i++)
 {
    for(j=0;j<c;j++)
      {
         printf("*");//this prints star 5 times(c times)
      }
     printf("\n");//for coming to a new line after star is printed
 }

 the printf("\n") is executed 4 times hence 4 rows will be there.

 complete program:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include <stdio.h>  
  int main()  
  {  
   int r,c,i,j;  
   printf("Enter number of rows and columns: \n");  
   scanf("%d %d",&r, &c);  
  for(i=0;i<r;i++)  
   {  
    for(j=0;j<c;j++)  
     {  
       printf("*");//this prints star 5 times(c times)  
     }  
     printf("\n");//for coming to a new line after star is printed  
   }  
    return 0;  
  }  


 OUTPUT:
Enter number of rows and columns:
4 5

*****
*****
*****
*****


Pattern 2:

12345
12345
12345
12345

Now, we just have to make a small change to achieve this one:

Pseudo:
for(i=0;i<r;i++)
 {
    for(j=0;j<c;j++)
      {
         printf("%d", j+1);//printing the value of j+1 
      }
     printf("\n");
 }



 complete program:



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include <stdio.h>  
  int main()  
  {  
   int r,c,i,j;  
   printf("Enter number of rows and columns: \n");  
   scanf("%d %d",&r, &c);  
  for(i=0;i<r;i++)  
   {  
    for(j=0;j<c;j++)  
     {  
       printf("%d", j+1);//this prints the value of j+1 i.e: 1 2 3 4 5   
     }  
     printf("\n");//for coming to a new line after star is printed  
   }  
    return 0;  
  }  



 OUTPUT:
Enter number of rows and columns:
4 5

12345
12345
12345
12345


Pattern 3:

11111
22222
33333
44444


You guessed it right ! We will now print the value of i in place of j

Pseudo:
for(i=0;i<r;i++)
 {
    for(j=0;j<c;j++)
      {
         printf("%d", i+1);//printing the value of j+1 
      }
     printf("\n");
 }


 complete program:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
  #include <stdio.h>  
  int main()  
  {  
   int r,c,i,j;  
   printf("Enter number of rows and columns: \n");  
   scanf("%d %d",&r, &c);  
  for(i=0;i<r;i++)  
   {  
    for(j=0;j<c;j++)  
     {  
       printf("%d", i+1);   
     }  
     printf("\n");//for coming to a new line after star is printed  
   }  
    return 0;  
  }  

 OUTPUT:
Enter number of rows and columns:
4 5

11111
22222
33333
44444


Pattern 4:

*
**
***
****
*****


Now this is the last one that I will explain.

Firstly, there will be 2 for loops(for printing rows and columns)

Second thing to be noticed is that there are N number of stars in Nth row,
i.e. 1 star in 1st row
      2 stars in 2nd row
      3 stars in 3rd row and so on...


so, in the second for loop we have to restrict j (number of columns) till i (number of rows)

Pseudo:
for(i=0;i<r;i++)
 {
    for(j=0;j<=i;j++)//restricting the value of j till i
      {
         printf("%d", i+1); 
      }
     printf("\n");
 }



 complete program:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
  #include <stdio.h>  
  int main()  
  {  
   int r,c,i,j;  
   printf("Enter number of rows and columns: \n");  
   scanf("%d %d",&r, &c);  
  for(i=0;i<r;i++)  
   {  
    for(j=0;j<=i;j++)  
     {  
       printf("*");   
     }  
     printf("\n");  
   }  
    return 0;  
  }  


 OUTPUT:
Enter number of rows and columns:
5 5

*
**
***
****
*****
Now you can try the following patterns, It is recommended that you minimize the the browser and try them before looking at the solutions.

 

Pattern 5:

1
12
123
1234
12345



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
  #include <stdio.h>  
  int main()  
  {  
   int r,c,i,j;  
   printf("Enter number of rows and columns: \n");  
   scanf("%d %d",&r, &c);  
  for(i=0;i<r;i++)  
   {  
    for(j=0;j<=i;j++)  
     {  
       printf("%d", j+1);   
     }  
     printf("\n");  
   }  
    return 0;  
  }  



Pattern 6:

1
22
333
4444
55555


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
  #include <stdio.h>  
  int main()  
  {  
   int r,c,i,j;  
   printf("Enter number of rows and columns: \n");  
   scanf("%d %d",&r, &c);  
  for(i=0;i<r;i++)  
   {  
    for(j=0;j<=i;j++)  
     {  
       printf("%d", i+1);   
     }  
     printf("\n");  
   }  
    return 0;  
  }

 

Pattern 7:

1
2   3
4   5   6
7   8   9   10
11 12 13 14 15


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
  #include <stdio.h>  
  int main()  
  {  
   int r,c,i,j, count=0;  
   printf("Enter number of rows and columns: \n");  
   scanf("%d %d",&r, &c);  
  for(i=0;i<r;i++)  
   {  
    for(j=0;j<=i;j++)  
     { count++; 
       printf("%3d", count); // "%3d" is used in printf for better indentation   
     }  
     printf("\n");  
   }  
    return 0;  
  }  


I will get back to you with some more complex patterns.


 Please share our blog and like our Facebook Page, This will help us grow. :)