Wednesday 8 September 2021

Introduction of python

Python is a high-level and most popular programming language. it was created by Guido van Rossum, and released on February 20, 1991.

Uses of python:-

It is used for general-purpose programming like

·         Mathematics

·         System scripting

·         Software development

·         Web development

Why python?

  •          Python works on different-different platforms like Windows, Mac, Linux, etc.
  •          It has a simple syntax.
  •          It has a syntax that allows developers to write the program with fewer lines than some other programming language.
  •          It has an object-orientated way or a functional way.

What can do python?

  •          It can connect to the database system and also read and modify files.
  •         It is used on the server to create the server-side application.
  •          It is used alongside software to create workflows.
  •          It is used to handle big data and performed complex mathematics.

Syntex in python:-

The syntax is the set of rules that defines how a Python program will be written and interpreted by both runtime systems and human readers.

  •       Python uses new lines to complete commands, as opposed to other programming languages which often use semicolons or parentheses.

  •          It relies on indentation, using whitespace, to define the scope of the loop, function, and classes. Other programming languages often use curly-brackets for this purpose.

Example:-

print("Hello Python")

Tuesday 18 May 2021

Linear Recursion in C

If a recursive function is calling itself for only once then, the recursion is linear.
Pseudocode:-
fun(int n)
{


    if (n > 0)
    {


        ...
        fun(n - 1);     // Calling itself only once


        ...
    }


}
Note: The fun() is calling itself for only once.


 
Understanding linear recursion by a simple problem
C program to find a cube of the positive number using Recursion.
#include <stdio.h>
 


int fun(int n)
{


    static int x;
    if (n > 0)


    {
        x++;


        return fun(n - 1) + x * x;
    }


}
int main()


{
    int a;


    printf("number ");
    scanf("%d", &a);


    printf("%d", fun(a));
    return 0;


}
Output:-






 Tracing the above Recursion:-













C program to find the factorial of a positive number using Recursion.


#include <stdio.h>
 


int fun(int n)
{


    if (n == 0)
    {


        return 1;
    }


    else
    {


        return fun(n - 1) * n;
    }


}
 


int main()
{


    int x;
    printf("number ");


    scanf("%d", &x);
    printf("%d\n", fun(x));


    return 0;

}


Output:-



Saturday 1 May 2021

Head recursion in C

If a Recursive function calls itself and  that recursive call is not the last statement of the recursive function then the recursion is known as Head recursion.

 

Structure of Head recursion

int fun(int n)

{

    if (n > 0)

    {

        fun(n - 1);

        ...

        ...

    }

}

 

Note:-  Some statements are executed at return time. 

C program to calculate the factorial of nth number using head recursion.

#include <stdio.h>

 

int fun(int n)

{

    if (n == 0)

    {

        return 1;

    }

    else

    {

        return fun(n - 1) * n;

    }

}

 

int main()

{

    int x;



    printf("number ");

    scanf("%d", &x);

    printf("%d\n", fun(x));

    return 0;

}

Output:-





C program to find the square of any positive number using head recursion.

#include <stdio.h>

 

int fun(int n)

{

    static int x = 0;

    if (n > 0)

    {

        x++;

        return fun(n - 1) + x;

    }

    return 0;

}

int main()

{

    int b;

    printf("number ");

    scanf("%d", &b);

    printf("%d", fun(b));

    return 0;

}

Output:- 






 

Tuesday 27 April 2021

Tail recursion in C

If a recursive function calls itself and that recursive call is the last statement in the function to execute then the recursion is known Tail recursion.

 

Structure of Tail recursion:-

int fun(int n)

{

    if( n >= 0)

    {

       ...

       ...

       fun(n-1);

    }

}

Note: All the statements in the function are executed before the recursive call.

 

Problem:-

C program to count number in descending order using Tail recursion.

 

#include <stdio.h>

 

int fun(int n)

{

    if (n > 0)

    {

        printf("%d\n", n);

        return fun(n - 1);

    }

}

int main()

{

    int a;

    printf("number");

    scanf("%d", &a);

    fun(a);

    return 0;

}

Output:-







Thursday 22 April 2021

Recursion in C

Recursion is a process in which a function calls itself. In a programming language, if a program allows calling of a function inside the same function, then the function is termed as recursive function and this  process of calling itself is called Recursion.

There are two main requirements for a function to be recursive:

1. Terminating condition -  the function must return when the terminating condition is satisfied.

2. Recursive call - the function must call itself.

Example:-

int fun()

{

    ...

    ...

    fun();

    ...

    ...

}

Here we can see that, the function fun( ) calls itself.

 

C program:-

C program to calculate the the sum of the ‘nth’ number using recursion.

 

#include <stdio.h>

 

int fun(int n)

{

    if (n == 0)

        return 0;

    else

        return fun(n - 1) + n;

}

int main()

{

    int sum = 0, x;

    printf("number");

    scanf("%d", &x);

    sum = fun(x);

    printf("%d\n", sum);

    return 0;

}




Sunday 11 April 2021

Sum of two number without using '+' operator

 

Idea 1:-

Half adder logic

A half adder has two inputs for the two bits to be added and two outputs one from the ‘sum’ and the other from the ‘carry’ into the higher adder position. A circuit is called a carrying  signal from the addition of the sum of the less significant bits from the X-OR gate the out from AND gate.




Truth table :-

A

B

SUM

CARRY

0

0

0

0

0

1

1

0

1

0

1

0

1

1

0

1

 

In the truth table.  you can see I have provided all possible combinations of  A and B. Now if you observe the truth table clearly,  the sum is nothing but A X-or  B. As we know in X-or two different bits give the output  1.

Carry is equal to A and B because we have like 1, 1 input combination the output is equal to one. Therefor

SUM = A  XOR  B

Carry = A  AND  B

C program

#include <stdio.h>

 

int main()

{

    int a, b, sum, carry;

    printf("a=");

    scanf("%d", &a);

    printf("b=");

    scanf("%d", &b);

 

    while (b != 0)

    {

        sum = a ^ b;

        carry = (a & b) << 1;

        a = sum;

        b = carry;

    }

    printf("sum = %d", sum);

    return 0;

}

Explanation of program

Step 1:  Declared two integer type variable a and b. sum and carry taken from to logic gate which is XOR and AND.

Step 2: Give the input in an integer type variable ‘a’ and ‘b’ by scanf() function.

Step 3: Use while loop check b is not equal to zero. the sum is equal to a^b then after carry is equal to a&b <<1 . and stored the value of sum in a. and stored the value of carry in b.

Step 4: Print the sum.

Output:-






Idea 2

Using while loop

C program

#include <stdio.h>

 

int main()

{

    int a, b;

    printf("a=");

    scanf("%d", &a);

    printf("b=");

    scanf("%d", &b);

 

    if (b > 0)

    {

        while (b != 0)

        {

            a++;

            b--;

        }

    }

    else if (b < 0)

    {

        while (b != 0)

        {

            a--;

            b++;

        }

    }

 

    printf("sum = %d\n", a);

    return 0;

}

Output:-





While loop in c

A while loop allows a part of the code to be executed multiple times depending upon a given Boolean condition. it called a pre-tested loop. Then the ‘While loop’ is mostly used in the case where the number of repetitions is no known in advance.

Syntax of while loop:-

          While(condition)

          {

               Statement;

               Increament/decreasement;

          }

Flowchart:-



Example:-

Problem 1

Write a C program to calculate the sum of two numbers without using the plus operator.

Algorithm

Step 1   a++;  b--;

Step 2   Repeat Step 1 until b becomes 0

Program of the above problem

#include <stdio.h>

 

int main()

{

    int a, b;

    printf("a=");

    scanf("%d", &a);

    printf("b=");

    scanf("%d", &b);

 

    while (b != 0)

    {

        a++;

        b--;

    }

    printf("sum = %d\n", a);

    return 0;

}

Output:-






Note:  This logic is design for only positive integers.

Problem 2

Write a C program to the counting of the number 1 to 10 using a while loop.

#include<stdio.h>

 

int main()

{

    int i=1;

 

    while(i<=10)

    {

        printf("%d\n", i);

        i++;

    }

    return 0;

 

}

Output:-









Problem 3

Write a C program to print the 2s table using a while loop.

#include <stdio.h>

 

int main()

{

    int a, n, i;

    printf("number");

    scanf("%d", &n);

    (i = 1);

    while (i<=n)

    {

        a = 2 * i;

        {

            printf("%d\n", a);

        }

        i++;

    }

}

Output:-




Friday 9 April 2021

Some basic C programs using Conditional Operator

Conditional operator simply returns one value when the condition is true and returns another value if the condition is false. It also knows as a ternary operator.

Syntax of a ternary operator

  (condition) ? Expression: Expression;

Example

(a>0)?1:0; evaluates to true so 1.

Now understand the ternary operator with the help of some basic problem.

Problem 1

Write a C program to check the numbers are positive or negative.

#include <stdio.h>

 

int main()

{

    int a, c;

    printf("Enter the number ");

    scanf("%d", &a);

    c = (a>0) ? printf("positive number ") : printf("Negative number ");

    // (a > 0) ? printf("positive number") : printf("Negative number");

}

Understanding the above code:

    Declared two integer type variables which are a and c.

    Input is taken in variable ‘a’ by  ‘scanf( )’ function.

    Integer type variable c is used to store the decision based upon the condition by the compiler.

Output








Using this in the above code

 (a > 0) ? printf("positive number") : printf("Negative number");








With both the code output is similar. 


Problem 2

Write a C program to determine the greatest number between the three numbers.

#include <stdio.h>

 

int main()

{

    int a, b, c, big;

    printf("a=");

    scanf("%d", &a);

    printf("b=");

    scanf("%d", &b);

    printf("c=");

    scanf("%d", &c);

    big = a > b ? (a > c ? a : c) : (b > c ? b : c);

    printf("Greatest number %d\n", big);

}

Output