Saturday, February 27, 2016

Print pattern in c - explore new perspectives in pattern printing

pattern in c

Print pattern in C

You will be able to :

  • >> Design any kind of pattern
  • >>Understand element of pattern designing
  • >>Understand patterns
  • >>Understand loop in c
  • >>Understand Sequence analysis

What is Pattern?

A pattern is set of  same or different kind of things that represent definite geometrical shapes or size.

Geometrical shape can be 
  • Triangle
  • Square
  • Pramid
print pattern in c
If you look above picture than difinetly you will find repetition of shapes or color in definite manner .

 It's example of pattern.
For Creating sucessfull Pattern , You must know

  • Loop
  • Element of pattern
  • Sequence analysis of pattern

Loop

loop are the control statement .

They are used when programmer needs to execute certain part of program for definite repetation

Different kinds of loop are
  • For loop
  • While loop
  • Do while loop
Among these for loop is used when the number of repetition is known.

Although while and do-while loop can be used while number of loop is known but it is a bit complex

As above explained Pattern contain certain repitation and that is known , so we will use for loop for building patterns

Syntax- for loop

 for ( init; condition; increment ) 

{

 statement(s);

}
Here is the flow of control in a 'for' loop −

  •  The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.
  • Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and the flow of control jumps to the next statement just after the 'for' loop.
  • After the body of the 'for' loop executes, the flow of control jumps back up to the increment statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.
  • The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the 'for' loop terminates.

Flow Diagram-for loop
for loop

Element of Pattern

There are number of element that should be well understood before building a pattern.

Number of row and column

Number of column represent number of repetition of first loop first loop with five-time repetition.
star pattern
Code of above program contain

#include  
int main()
{ 
 int i, j;
 for(i=5;i>=1;i--)
 {    //other code 
 }
 return 0;
}

Similarly each row are handled in each repetation of first loop.

Character used

Number of different types of character denote the number of loop inside first loop.i.e nested loop.
star pattern
In above pattern we have to print only '*' , so it will have only one nested lopp.
pyramid pattern
But in above pattern it contain two nested loop with other for easy.

Sequence analysis

star pattern
It is very important and easy to analysis pattern

This is the combination of * and blank space.  we have to  identify that in which position we need to print *and don't worry about the blank spaces.

 we can analysis like this

pattern in c
[ "-" represent a blank space][ and n=9]

Step By Step Solution

1.this program is a shape of matrix. so we need to use two for loop to solve this problem.
 for(i=1;i<=n;i++)
           {
             for(j=1;j<=n;j++)
              {
                  if(condition)
                         printf(" *");
                        else
                         printf("  ");//blank space
                   
              }
             printf("\n");
           }
2. ith 1st and n(9)th index jth 1st and n(9)th index you see. each position we need to print '*' . in side loop condition we can write it as..
      if(i==1 || i==n || j==1 || j==n)
         printf(" *");
       else
        printf("  ");
then output come like box.
star pattern
3. now we need to draw like:
star pattern
here we can see it's printing * where-where ith index and jth index are equals except middle mean [(n/2)+1=5] so we can write code for this:
        if(i==j && j!=(n/2)+1)
         printf(" *");
       else
         printf("  ");
4. Now last pattern, reverse of step(iii) pattern.. ex:
pattern in c

5. so here we can analyse it's decreasing order of j index 9,8,7,6,4,3,2,1 except middle means [n/2+1=5] For this we can write the condition:-


       if(j==(n+1)-i && j!=(n/2)+1)
          printf(" *");
        else
          printf("  "); 


In above example we declared the some variation of pattern based on condition... Even we combined 3 and 4 condition then we will get output like:

pattern in c


condition:
       if(i==j && j!=(n/2)+1 || j==(n+1)-i && j!=(n/2)+1)
          printf(" *");
        else
          printf("  ");


And finally Even we combined this 5 with 2 then we will get output like: Final Example:

star pattern
so final code is:


#include
#include

int main()
{
 int n , i ,j;
 
 printf("Enter number: ");
  scanf("%d",&n);
//first loop
 for( i=1;i<=n;i++)
 {
  for(int j=1;j<=n;j++)
   {

   if(i==1 || j==1 || i==n|| j==n || i==j && j!=(n/2)+1 || j==((n+1)-i) && j!=n/2+1)
   {
        printf(" *");
     }
    else
         printf("  ");
  }
  printf("\n");
//next line 
}
//pattern end
 getch();
}
pattern code download

SUMMERY

  1. A pattern of numbers, star or characters is a way of arranging these in some logical manner or they may form a sequence
  2. Some of these patterns are triangles which have special importance in mathematics. Some patterns are symmetrical while other are not.
  3. Creating a pattern involves how to use nested loops properly, some pattern may involve alphabets or other special characters. Key aspect is knowing how the characters in pattern changes.
  4.  pattern is set of  same or different kind of things that represent deifinite geometrical shhapes or size.
  5. the important element of pattern is it'e order or sequence.
  6. you must be able to recognize order or sequence of pattern inorder to build it.
    Below video can be helpful for understanding Pattern
    Download above tutorial for offline use:
    c pattern download

    DO NOT MISS OTHER C PROGRAMMING TUTORIAL

    * indicates required


    You can Browse related article below for more information and program code related to pattern

    Does above is helpful , Post you views in comment

    Tuesday, February 23, 2016

    what everybody ought to know about string in c.


    String in C

    String


    In C programming, array of character are called strings. A string is terminated by null character /0.


    Array is the collection of similar data under common name.


    A common name is the name of string or array.For example:

     char stringname  = "Hello";
    
     Here stringname is common name .We will study String Decleration below Section.


     C  compiler represent above string as below image (string representation)


    Declaration of String


    It is declared as:


     char string_name[size];


    E.g: char name[10];


    Strings can also be declared using pointer.
    char *p

    Initialization of String


    It can be initialized when they are declared


    char c[]="abcd";
         OR,
    
    char c[5]="abcd";
    
         OR,
    
    char c[]={'a','b','c','d','\0'};
    
         OR;
    
    char c[5]={'a','b','c','d','\0'}; .


    String can also be initialized using pointers

    char *c="abcd";


    String Handling Function


     There are different libraries or built-in function in C for string manipulation.These function are defined in header file <string.h>

    All string manipulation can be done manually by the programmer but, this makes programming complex and large.

    To solve this, the C supports a large number of string handling functions.
        strlen()  -  Calculates the length of string
    
        strcpy() -   Copies a string to another string
    
        strcat()  -  Concatenates(joins) two strings
    
        strcmp() -   Compares two string
    
        strlwr()  -  Converts string to lowercase
    
        strupr()  -  Converts string to uppercase
    
    

      1.  strlen():


        This is used to return the length of string

        It returns the number of characters in the string without including null
        character

        Syntax:

     integer_variable = strlen(string);

      2  strcpy() :



        It copies one string to another

        Syntax:

     strcpy(destination_string , source_string);


        e.g strcpy(a,b) i.e the content of b is copied to a

      
      3. strcat():


        It joins  to strings together


        It stores the content of second string at the end of the first one.


        E.g: strcat (string1 , string2  ); i.e string1 = string1 + string2;

      
      4. strcmp():


        It  compares two string


        It takes two strings as parameters and returns the integer value.

          >> 0 if both string are same
          >> less than zero if  the first string is less than the second one.
           >>Greater than zero if the first string is more than the second

       5. strrev () :


        It is used to reverse all character of string except the null character

        Syntax:
        strrev ( string );


        E.g strrev(str) i.e it reverses the characters in string str and stores the reversed string in the str

    SUMMERY

    1. Strings are actually one-dimensional array of characters terminated by a null character '\0'.
    2. Here, "c string tutorial" is a string. When, compiler encounters strings, it appends null character at the end of string.
    3. Here "onlineclab" is a string of length 10 but  in c it's length is 11 because c add '\0' at the end.Actually, you do not place the null character at the end of a string constant. The C compiler automatically places the '\0' at the end of the string when it initializes the array.
    4. The terminating null character  '\0' is important because it is only way the string handling function can know where the string ends
    5. Strings can also be declared using pointer.
    6. Strings are declared in C in similar manner as arrays. Only difference is that, strings are of char type. Array store all type of data but string store only character.
    7. string.h is collection of functions for string manipulation


    8. No standard operators for string assignment and comparisons!(remember: strings are arrays!)
    9. String can be passed to function in similar manner as arrays as, string is also an array
    10. Functions gets() and puts() are two string functions to take string input from user and display string respectively
    11. strlen()     Calculates the length of string
    12. strcpy()    Copies a string to another string
    13. strcat()    Concatenates(joins) two strings
    14. strcmp()   Compares two string
    15. strlwr()    Converts string to lowercase
    16. strupr()    Converts string to uppercase
    17. All string are array but all array are not string.   
    Below video can be helpful for understanding string
    Download above tutorial:

    DO NOT MISS OTHER C PROGRAMMING TUTORIAL

    * indicates required


    You can Browse related article below for more information and program code related to string

    Does above is helpful , Post you views in comment

    Friday, February 19, 2016

    C- Computer programming language-field of study

    Computer software and programming language
    C- Computer programming language-field of study

    C- Computer programming language-field of study

    After reading this tutorial  ,  you will be able to :

        >> Define software and programming language
        >> Have knowledge about type of software and programming language
        >> Have knowledge about advantage and disadvantage of different programming language

    Software

    Wikipedia describe software as " Computer software also called a program or simply software is any set of instructions that directs a computer to perform specific tasks or operations " .In simple word software is a set of coded commands or programs that tells a computer what tasks to perform.It consists of computer programs, libraries and related non-executable data (such as online documentation or digital media)

    Types of Software

    System Software

     Software that directly operates the computer hardware, to provide basic functionality needed by users and other software, and to provide a platform for running application software.It's main purpose is to help run the computer system .It controls all the parts of the computer system . Without system software computer system can not perform it's task . It includes
    1. Operating System :

        They primary purpose of the os is to act as an interface between the computer and user.OS are essential collections of software that manage resources and provides common services for other software that runs "on top" of them. Supervisory programs, boot loaders, shells and window systems are core parts of operating systems.In general sense , it can be said that the operating system is the overall in-charge of the whole computer.e.g: DOS , Window , Linux etc
    2. Device driver etc


  1. Software types
    Software types

  2. Application Software

    Application software, or simply applications, are often called productivity programs or end-user programs because they enable the user to complete tasks such as creating documents, spreadsheets, databases, and publications, doing online research, sending email, designing graphics, running businesses, and even playing games! A sub-class of computer software which directly employs the capabilities of a computer to a task that user wishes to perform is called application software . Their capabilities ranges from entertainment field 
    to official tasks such as producing bills in office . They are specially designed and developed for end users .E.g:Microsoft Offices , games etc.It is classified into following category:
    1. Tailored Software :

        Developed for solving particular problems only.
    2. Packaged Software :

      Different kinds of programs related focus application are combined to form packaged .For example : MS-Word
    3. Utility Software :

      Special types of application software which helps us to fine tune the performance of computer , prevent unwanted actions or perform system related task such as checking for virus and removing virus etc.


    Programming Language

    Wikipedia describes programming language as  " A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer"  . It is the communication language between computer and user.

    Classification of Programming language

    1. Low-level language :

      Low-level language is a programming language in which each statement or instruction is directly translated into single machine code ,,Generally this refers to either machine code or assembly language. The word " low " refers to the small or nonexistent amount of abstraction between the language and machine language; because of this, low-level languages are sometimes described as being " close to the hardware " It's type are:

      1. Machine-level language/First Generation Language   :

        It is a language that computer directly understand . In other words , it is sequence of instruction written in form of binary numbers .
      2. machine level language advantage and disadvantage
        machine level language advantage and disadvantage
      3. Assembly Language / Second Generation Language ( 2GL ) :

        Assembly language is the symbolic representation or mnemonics of machine level language .The codes written in assembly language must be translated into machine level language before execution and assembler is used for this translation process.
      4. assembly language advantage and disadvantage
        assembly language advantage and disadvantage
    2. High Level Language :

    3. High level language is a programming language that is more user friendly , to some extent platform-independent and abstract from low level computer processor operations such as memory access . High level languages are easier to read .High level languages are machine independent and are problem-oriented.

       Advantages :

    • Easy to use 
    • Machine independent
    • Easy to detect error
    • Easy and fast development of software

    Classification of high level language :



    1. Procedure Oriented Language / Third Generation Language ( 3GL ) :
      Procedural language is the most common high-level language in which one or more Procedural language that perform some complete function are grouped together into a program procedure .It allows the program to be broken into small part. Eg : COBOL , FORTRAN , PASCAL etc
    2. Problem Oriented Languages / Fourth Generation Language ( 4GL ) :
      They are designed to solve specific problems or develop specific applications by enabling the user to describe what they want rather than step by step procedure for getting there . Database is the example of fourth generation language.
    3. Natural Languages / Fifth Generation Languages ( 5GL ) :
      Natural languages are those  which we use in our daily life . They are still in the development stage .A fifth generation programming language (abbreviated as 5GL) is a programming language based on solving using constraints given to the program, rather than using an algorithm written by a programmer.

    SUMMARY :

    >>Set of instruction that direct computer to do user defined task is called software

    >>There are two type of software 1)system software 2)Application software

    >> Operating system is the communication medium between computer and user

    >> Programming language is a formal constructed language to instruct computer

    >> Machine codes look like 01010001010.

    >>High-level language are easier to read and understand


    DO NOT MISS OTHER C PROGRAMMING TUTORIAL

    * indicates required


    Download a free PDF version of this article
    Yes! Give me my PDF
    PDF version contains all of the content and resources found in the web-based guide

    Do you think above article is important or useful then download it for offline use DOWNLOAD NOW >>   or  if you have any problem related to c-programming then  let us know about it . Post it in comment below

    Tuesday, February 16, 2016

    Cube sum program of individual digit of number in c with description

    What is cube sum program written in c ?

    In cube sum program , we  find cube of individual digit of a number and find their sum .for example: 123 =  1^3 + 2 ^3 + 3^3.
    Cube of individual digit have to be found for that we have to extract each digit .As we know last digit of number can be extract using modulus division (i.e 123%10 = 3 ) , And if we do integer division of given number by 10 , we can get other digit excluding last digit .


    Code of  cube sum program:

    // Cube sum program  of individual digit of number in c start
    #include <stdio.h> 
    #include <conio.h>
    int main()
    {

       int number;


       printf("enter a number to find cube sum");

     
       scanf("%d", &number); 


       int n2,cube,cubesu=0;

       while(number!=0)
         {

            n2 = number%10;

            cube = n2*n2*n2;

            cubesu = cubesu + cube;

            number = number/10; 

          }

        printf("cube sum is %d\t", cubesu);
        getch();
        return 0 ;

    // Cube sum program  of individual digit of number in c start
    }

    How cube sum program works?

    To learn this cube sum program we need to know how to explode a given number(i.e 345 = 3 , 4 ,5 ), Here we need modulus division which provide reminder (i.e last digit ) e.g
    23%10 = 3 ,so by using modulus division we can extract end digit until one digit remind using loop.If we want to extract first digit we can do integer division e.g
    23/10 = 2.By using integer division we can also extract fist digit without last digit e.g 234/10 = 23.

    Here for above program:we extract end digit and find cube of end digit  and store in a variable(i.e cubsum) after that we extract first numbers beside last digit ,
    loop continue until number is equal to one.

    At last we print the cube sum.

    Output of  cube sum program in c?

    Output of cubesum program c
    On screen output of cube sum program

     Download cube sum program of individual digit free



    If you have any problem in understanding this cube sum program written in c , please feel free to post in comment about cube sum program .And if you have any other technique to have Cube sum program  of individual digit of number

    Cube sum program using function in c

    #include <stdio.h>
    #include <conio.h>
    int main( )
    {
        int number;
        printf("enter a number to find cube sum");
        scanf("%d", &number);
        cubesum(number);
    }
    cubesum(number)
    {
        int n2,cube,cubesum1=0;
        while(number!=0)
        {
            n2 = number%10;
            cube = n2*n2*n2;
            cubesum1 = cubesum1 + cube;
            number = number/10;
       
           
           
        }

        printf("cube sum is %d\t", cubesum1);

        getch();
        return 0 ;
    }


    If you have any problem in understanding this cube sum program written in c using function , please feel free to post in comment about cube sum program .And if you have any other technique to have Cube sum program  of individual digit of number using function in c