Search This Blog


Friday, November 29, 2019

Array

View chapters from top of the tab for more information.

A step up Saurav, c language by a step up
Array in C


  • It is a special variable which is used to store multiple values of same type.

  • It share common name for all values.

  • It is also known as subscripted variable.

  • The element of array is accessed with the help of index value . An index value is positive (+ve) integer value.

  • The lower limit of array is zero “ 0 “ and upper limit is ( size -1 ) .

  • The index value of array must be started with zero “ 0 “ .

  1. It is user defined data type.

➤Advantage of array :-


  1. It holds multiple value at a time.
  2. It provides simple operation as insertion , deletion, searching , sorting , etc.
  3. It shares common name for all values.

➤Disadvantage of array :-


  1. It supports static memory allocation.
  2. It is a technique in which memory is allocated during compile time . So in  this case memory is wasted due to fixed size of array . It is major drawbacks of  array but it can be avoided with the help of pointer.
  3. It stores same type of value but it can be removed with the help of structure.


⟹example:- int  a[10]



10
20
30
40
50
60
70
80
90
100
 0           1          2         3         4         5          6         7         8         9


in   the above table “ 0 “ is lower limit / lower bound

and “ 9 “ is the upper limit / upper bound


➤Types of array :-


  • One dimensional array / single dimensional array

  • Two dimensional array

  • Multi  dimensional array

➤One dimensional array :-


  • This type of array is use to hold multiple values in a single row.

  • The array consists of only one subscript which represents size of array.

⏩syntax:-


<data type> <variable> [size] , <variable>[size],…..

⟹example:-


int a[10] , b[20] ;

float  f[10];

char  str[20];

⏩Initialization :-


⏩Syntax: during declaration time



<data type> <variable name> [size] = { val1, val2 , val3,..}

“here size is optional”



⟹Example:-


int a[5] = { 10, 20 , 30 , 40 , 50 };

int a[] = { 5 , 7 , 22 , 77 , 44 , 99 };

char  s[] = { ‘S’ , ‘A’ , ‘U’ , ‘R’ , ‘A’ , ‘V’ };

char  s[] = “SAURAV” ;



⏩Syntax : after declaration



<variable name> [index] = <value> ;

⟹Example:-


int a[10];

a[0] = 10;

a[1] = 30;

a[2] = 50;

……….

……….

a[10] = 100;




➧Accessing:-


⏩Syntax:-


 <variable> [index]

⟹Example:-


 int a[10] = { 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 , 100 }

i.e, 

10
20
30
40
50
60
70
80
90
100
 0          1          2         3          4         5         6          7         8          9



a[5] = ? , that is 60.

a [1] = ? , that is 20.

a[ 9] = ? , that is 100.

a[0] = ? , that is 10.

a[3] = ? , that is 40.






P.Example:- Program to input 10 numbers after that printf it.



#include<stdio.h>

#include<conio.h>

void main ()

{

  clrscr();

int a[ 10 ] , i ;

printf( “ enter the 10 numbers “ ) ;

  for ( i = 0 ; i < 10 ; i ++ )

   {

     scanf( “ %d “ , &a[i] );

   }

  printf( “ All elements are :- “ );

   for ( i = 0 ; i < 10 ; i ++ )

   {

   printf( “ %d \n “ , a[ i ] ) ;

    }

  gethc() ;

}



P.example:- Program to input 10 numbers after that print in reverse order.


#include<stdio.h>

#include<conio.h>

 void main()

{

  clrscr();

  int a[ 10 ] , i ;

printf( “ enter the 10 numbers “ ) ;

for ( i = 0 ; i < 10 ; i ++ )

 {

  scanf ( “ %d “ , &a[ i ] ) ;

 }

printf( “ All elements are  \n “  ) ;

 for ( i = 9 ; i >= 0 ; i-- )

{

  printf( “ %d \n “ , a[ i ] );

  }

getch();

}


Wednesday, November 20, 2019

Recursion

View chapters from top of the tab for more information.

Note: If you haven't study the function yet visit now . Because you are not going to understand this recursive function unless you have completed your function topic clearly.

Subscribe my youtube channel, link is given below.

click on this ⇒⇒A step Up



a step up by saurav singh
Recursion in C


  • It is a process in which problem is defined in terms of itself.

  • It solved problems by repeatedly breaking it into smaller and smaller problems. And that is similar in the nature of the original problems.

  • It is a type of function which calls itself in the same function .

  • To implement the recursion technique in the programming, that function should be capable of calling itself.

Syntax:-


int main(void)

{

   ….

  recursion();

   ….

 }

  void recursion()

  {

   ……

   recusrcion();

   ……

  }

➤How to write a recursive function?



Before writing a recursive function , we should able to define the solution of the problems in terms of a similar type of similar problems. Recusrion is all about a way of thinking about the problems.

The two main steps to keep in mind before writing the recursive function is that:-


  1.           Identification of the base case and its solution.
     2.       Identification of the general case or the recursive case or the case in which recursive function  will be made.


It is very important to identifying the base case because without base case ,the function calls itself infinitely and will never terminated .


Flow of control in recursive function :-


main()
{ 1…..
 2…..
 3 rec(5);
 4….. }

          
                             now n is 5

void rec(int n)
{ 1…..
  2 if(n==1)
  3 return;
  4……..
  5 rec(n-2);
  6……}



 now n is 3


void rec(int n)
{ 1…..
  2 if (n==1)
  3 return;
  4……..
  5 rec(n-2);
  6……. }

                               













          now n is 1

 void rec(int n)
{ 1……
  2 if (n==1)
  3 return;
  4…….
  5 rec(n-2);
  6…… }





1.       At initial main() function calls the rec() function , so at first instance value of formal parameter n is 5. After that rec() is called ,then it checked the terminating condition and since it is false we don’t return.  Now next line is executed and after that rec() is again called with argument 3.


2.       Now controls transfer to the second instance of rec(). value of n is 3. then it checked the terminating condition and since it is false we don’t return.  Now next line is executed and after that rec() is again called with argument 3.

     3.     Now controls transfer to the third  instance of rec(). value of n is 1. then it checked the terminating condition and since it is true ,now we return.


       4.     We now return to the second instance of the rec() at the next line. and after executing the next line we return back to the first instance.


       5.   We are now inside first instance of rec(). And after executing the next line we return to the main() at next line.



The number of times that a function calls itself is known as recursive depth of that function.

➤Winding and Unwinding phase:-


All recursive function generally work in two phases i.e, so called winding and unwinding phase.

Winding phase starts when the recursive function calls for the first time, and each recursive call continues their winding phase . In this phase the function keeps in calling itself and no returns statements are executed . And when the terminating condition is true then this phase is terminated.



After this unwinding phase is started , in this phase the recursive function call start returning in reverse order till the first instance is met and return to the main() function. In this unwinding phase the control returns through each instance of function.



Examples 1. factorial



/* Program to find the factorial of a number by recursive method */




#include<stdio.h>

long int fact(int n);

int main(void)

{

  int num;

   printf(“enter a number : “);

  scanf(“ %d “, &num );

  if( num<0 )

   printf( “ No factorial for negative numbers \n “);

  else

     printf( “ Factorial of %d is %ld \n “ , num , fact(num) );

   return 0;

  }

 long int fact(int n)

  {

   if(n==0 )

   return 1;

    return ( n * fact ( n-1 ) );

  }



 Practice your function related programs

Friday, October 18, 2019

Practice program on function

View chapters from top of the tab for more information.

Subscribe my youtube channel, link is given below.

click on this ⇒⇒A step Up

c language by a step up
Practice program on function


P1. Program to input any no. after that find sum of all digit by function .


#include<stdio.h>
#inclued<conio.h>
 void findsum_digit();
 void main()
  {
   clrscr();
   findsum_digit();
  getch();
  }
void findsum_digit()
  {
   int a , d , sum = 0 ;
   printf( “ Enter the no. ” );
   scanf( “ %d “, &a );
  while ( a > 0 )
   {
    d = a % 10 ;
    sum = sum + d;
    a= a/10;
   }
 printf( “ Sum of all the digits are = %d “, sum );
  }

P2. Program to input length and breadth after that find area and perimeter of rectangle by function .


#include<stdio.h>
#inclued<conio.h>
 void input();
 void findarea();
 void findperi();
 int l , b ;
 void main()
  {
   clrscr();
   input();
   findarea();
   findperi();
  getch();
  }
void input()
  {
   printf( “ Enter the length and breadth  ” );
   scanf( “  %d  %d  “, &l , &b  );
  }
void findarea()
  {
   int ar ;
   ar = l * b ;
    printf( “ Area of the rectangle = %d \n “ , ar );
   }
void findperi()
   {
   int peri ;
   peri = 2 * ( l + b );
   printf( “ Perimeter of rectangle = %d “ , peri ) ;
  }

P3. Program to input any no. after that print the table by implementing extra function.


#include<stdio.h>
#inclued<conio.h>
 void input();
 void table();
 int n ;
  void main()
  {
   clrscr();
   input();
    findtable();
  getch();
  }
void input()
  {
   printf( “ Enter the length and breadth  ” );
   scanf( “  %d  %d  “, &l , &b  );
  }
void table()
  {
   int i , table ;
   for ( i = 1 ; i <= 10 ; i ++ )
  {
   table = n * i ;
   printf ( “ table = %d “ , table );
  }
 }

P4. Program to create a function as named findsmallest and to input three no.  as parameter after that find smallest no.


#include<stdio.h>
#inclued<conio.h>
 void findsmallest( int , int , int );
 void main()
  {
   clrscr();
   findsmallest ( 20 , 30 , 5 ) ;
   getch();
  }
void findsmallest ( int a , int b , int c )
   {
    int smt ;
    smt = a < b && a < c ? a : ( b < c ? b : c ) ;
    printf ( “ smallest no. = %d “ , smt ) ;
  }


One more process to access parameter


 void main ()
  {
     clrscr ();
     int a , b , c ;
     printf ( “ enter the three no. “ );
    scanf( “ %d %d %d “ , &a , &b , &c ) ;
   findsmallest ( a , b , c );
  getch () ;
  }

P5. Program to create a function as named “ chckpalindrome() “. And to input no. as parameter after that check no. is palindrome or not.


#include<stdio.h>
#inclued<conio.h>
 void input();
 void checkpalindrome();
 void main()
  {
   clrscr();
   int a ;
   input();
   checkpalindrome();
  getch();
  }
void input()
  {
   printf( “ Enter the number  ” );
   scanf( “  %d “, &a  );
  }
 void checkpalindrome ( int a )
   {
   int rev = 0 , d ;
  num = a;
   while ( a > 0 )
    {
     d = a % 10 ;
   rev = rev * 10 + d ;
    a = a / 10 ;
    }
   if ( num == rev )
    printf ( “ Palindrome “ ) ;
   else
   printf ( “ Not palindrome “ ) ;
   }

P6. Program to input two number as parameter after that check both numbers are amicable by implementing function .

#include<stdio.h>
#inclued<conio.h>
  void input();
  void checkamicable( int , int );
  void main()
  {
   clrscr();
   int a , b ;
   input();
   checkamicable( a , b ) ;
  getch();
  }
 void input()
  {
   printf( “ Enter the two number  ” );
   scanf( “  %d  %d  “, &a, &b  );
  }
 void checkamicable ( int a , int b )
   {
    int   i , s1 = 0 , s2 = 0 ;
    for ( i = 1 ; i < a ; i ++ )
  {
    if ( a % i == 0 )
     s1 = s1 + i ;
   }
    for ( i = 1 ; i < b ; i ++ )
   {
      if ( b % i == 0 )
        s2 +=  i ;
    }
   if ( s1 == b && s2 == a )
   printf ( “ Amicable no . “ ) ;
  else
   printf ( “ Not amicable no. “ );
  }

P10. Program to input any binary number after that convert into decimal using return type parametrical function .

#include<stdio.h>
#include<conio.h>
  int binarytodecimal( int n )
  {
    int d , i = 1 , sum = 0 ;
   while ( n > 0 )
   {
     d = n % 10 ;
    sum = sum + d * i ;
    i = i * 2 ;
    n = n / 10 ;
     }
     return ( sum );
  }
   void main ()
  {
    clrscr();
    int n ;
    printf ( “ Enter any binary number “ ) ;
    scanf ( “ %d “ , &n ) ;
    printf ( “ Decimal no. = %d “ , binarytodecimal ( n ) ) ;
   getch () ;
  }

Monday, October 14, 2019

Function


Key points:


  • It is most important tool in a procedural programming.
  • A function can be defined as a block of statements which is used to perform any particular task according to requirements.
  • The main objective of function is to reduce the complexity of programs.
  • In this case a large program can be divided into multiple parts, that part is called function or module.

There are some advantages of function :-


  • We can create user friendly program with the help of function. In this case the user can create separate function for each module according to requirements.
  • It provides reusability. It means a written source code can be saved multiple times.
  • It provides extendibility . It means a program can be modified and extended in future.
  • It reduces the complexity of programs.
  • It provides readability. It means function oriented program is more readable rather than non function oriented programs.
  • Function creates user friendly program because debugging process is easy.

Types of function :-

There are two types of function :

astepupss c language
Types of function in C
  • Predefined function (inbuilt function)
  • User defined function ( customized function )


Predefind function :-



This type of function is already defined in programming language. We cannot change the behaviour of function.

example:-

  
   printf() , scanf() , clrcr() , getch() , strlen() , etc.

User defined function :-


This type of function is created with the help of user according to requirements.

example:-


findarea() , findfact() , even() , etc.

Function prototype :-


  • It represents architechture of function.
  • It must be required if main function is defined before user defined function.
  • It must be written after declaration of header file.

example:-


#include<stdio.h>
void findsum();
void main()
{
   findsum();
 }
void findsum()
 {
   statements;
  }

How to call a function :-

syntax :-

   
  function name();

example :-


 void main()
  {
    findsum();
   }

Function architecture / pattern :-


It represents type of function as return type , non-return type , parameterised or non-parameterised.

There are four architecture of function :-


  1. A function with no return type and no parameter / argument.
  2. A function with return type but no parameter / arguments.
  3. A function with no return type but with parameter / arguments.
  4. A function with return type and with parameter / arguments.

How to create user defined function :-

1. function with no return type and no parameter

syntax :-


void <function name> ()
 {
    block of stmts;
   }
c language by a step up
function with no return type and no parameter syntax

example :-


 void display()
{
  printf( “ hello” );
 } 

 2. Function with return type but no parameter


syntax:-


   <data type> <function name> ()
{
   block of stmts;
  }
c language by a step up
function with return type but no parameter syntax

example:-


 int findadd()
  {
  int a = 10, b = 20 ;
 add = a + b ;
  return (add) ;
}

 Parameter:- 


Parameter is a special variable which can be supplied during a function and calling a function.

    There are two types of parameter:


        1. formal parameter :- Those parameters which are supplied during creating a function called formal parameter . It is treated as local variable.

        2. actual parameter :- Those parameters which are supplied during  calling a function.  It is also called as argument.

3. A function with no return type but with parameter

  

syntax:-


void <function name> ( <data type> <parameter 1>, ..)
 {
  block of stmts;
  }
c language by a step up
function with no return type but with parameter syntax

 example:-


 void findadd( int a, int b)
{
   int add ;
  add = a + b;
  printf( “ addition = %d “, add);
 }

4. A function witth return type and with parameter


syntax:-


 <data type> < function name > ( parameter list )
 {
   block of stmts;
   return (variable);
 }
c language by a step up
function with return type and with parameter syntax

P.example:- Program to input any no. after that find factorial value using function.


#include<stdio.h>
#include<conio.h>
void findfact()
 {
  int a, fact=1 , i ;
 printf( “ enter the no “ );
 scanf( “ %d “, &a );
  for ( i = 1 ; i <= a ; i ++ )
  {
    if( a % i == 0 )
   fact = fact * i ;
  }
 printf( “ factorial = %d “, fact );
}
void main()
 {
  clrscr ();
 findfact ();
getch ();
 }
We practice more program on this topic on the next blog.