May 29, 2020

Loop's In C with Exercises and Examples

We use loops in program when we need to execute one or more statements multiple times until some conditions are satisfied. Means sometimes we want some part of code to be executed multiple times then we need to use loops.


  There are 3 types of loops in C-






Above three type of loops are used in C programming. Using this loops we can execute conditions multiple  times. below is the list of for, while and do-while loops in C programming examples.

So let's understand the the working of  loops with examples...


  Loops Exercises and Example's:


  Next: Array in C

do...while loop in C


In privious post we learned working of for loop and while loop, in this post we learn working of do....while loop.

do..while loop is similar to while loop only difference is-
do...while loop check condition at the end of loop. That means the statement inside the do...while loop are executed at least once even if condition is false.




    Syntax of do...while loop:



Syntax of do..while loop is shown above.




    Example program of do-while loop:


#include<stdio.h>
void main()
{
int i;
do 
{
   printf("You are inside the do while loop\n");
   printf("Press 1:to exit from the loop\n");
   scanf("%d",&i);
}
while(i!=1);

printf("You are out of the do while loop\n");
}


Above program shows the following output:





While loop In C


In previous post we learnt for loop, in this post we will learn working of  while loop.
While loop is  used when you want to execute a block of code repeatedly with a checked condition before making an iteration. In simple language while loop is a entry  controlled looping statement.


    Syntax of while loop -



Syntax of while loop is shown above.


    Example program of while loop: 


#include<stdio.h>
void main()
{
 int i=1;
 while(i<=10)
 {
 printf(" Hello ");
 i++;
 }
}

Here, until test counter condition is true the statements within while loop will execute, that means until i is less than or equal to 10 it will print Hello. So it will print hello 10 times. When condition becomes false control will be transferred to the next line after the loop.



Above program shows the following Output:




for loop in C


The for loop is a loop which is mostly used in any programming languages.
because for loop is easy to use and understand.
For loop is used to repeate a specific block of code a known number of times.

Let's understand with the syntax of for loop-






   Example: program to print series of  1  to 10 numbers using  for loop.


    
#include<stdio.h>
void main()
{
int i;
for(i=1;i<=10;i++)
 {
printf("%d ",i);
 }
}

Above program shows the following Output:



May 26, 2020

C program to check year is leap year or common year

Ex: Write a C program to check given year is leap year or common year. How to write a C program to check given year is leap or not. C program to check given year is leap year or common year.


  What  is leap year ?


We all know in the regular year has 365 days.  But in the leap year one day has extra. means leap year has 366 days.



Input from user:

Enter the year: 2020


Expected output:

Given year is leap year.





  Step by step logic of the program:


1. Firstly get input year  from  user. variable say year.

2.  After that use if-else statement to check given condition if(year%400==0) or(year%4==0 and year%100!=0).

3. If above conditions are true then print given year is leap year.

4. Otherwise we print given year is common year.





  C program to check year is leap year or common year :



#include<stdio.h>
int main()
{
int year;

printf("Enter the year\n");
scanf("%d",&year);

if((year%400==0)||(year%4==0 && year%100!=0))
{
printf("Given year is leap year");
}
else
{
printf("Given year is common year");
}

return 0;
}



Above program shows the following output:




C Program to check given number is zero or not

Ex: write to a C program to check given number is zero or not. C program to check given number is zero or not. How to write a C program to print it is zero or not.


Input from user:

Enter the number: 0

Expected output: 

Given number is zero.



  Step by step logic of the given program:


1. First we will accept input number from user say no.

2.  After that use if-else statement to check condition no==0 .

3. If this condition true then print given number is zero.

4. Otherwise we print given number is not zero.



  C  Program to check given number is zero or not :



#include<stdio.h>
int main()
{
int no;

printf("Enter the number\n");
scanf("%d",&no);

if(no==0)
{
printf("Given number is zero");
}
else
{
printf("Given number is not zero");
}
return 0;
}



Above program  shows the following output:






May 24, 2020

C Program to Find Maximum Between Three Numbers

EX: write a C program to check maximum number between three numbers. C program to print Maximum  between three numbers. How to write a C program to print Maximum  between three numbers.

In previous program we learn how to find maximum between two numbers. In this program we will find maximum between three numbers.



Input from user:

Enter the number 1: 100

Enter the number 2: 500

Enter the number 3: 300


Expected output:

Maximum between three numbers is: 500

So let's get explore it's logic.




  Step by step logic of the given program:


1. Accept three numbers from user say no1, no2 and no3.

2. Use if-else-if ladder and relational operators to find this .

3. Check if no1>no2 and no1>no3 if true means no1 is maximum.

4. If this condition is not true then we check no2>no1 and no2>no3 if this condition true then no2 is maximum.

5. Otherwise we will print no3 is maximum.





  C program to find maximum between three numbers:


#include<stdio.h>

int main()
{
int no1,no2,no3;

printf("Enter number 1:\n");
scanf("%d",&no1);

printf("Enter number 2:\n");
scanf("%d",&no2);

printf("Enter number 3:\n");
scanf("%d",&no3);

if(no1>no2&&no1>no3)
{
printf("Maximum number is %d",no1);
}
else if(no2>no1&&no2>no3)
{
printf("Maximum number is %d",no2);
}
else
printf("Maximum is %d",no3);

return 0;

}



Above program shows the following output:


C program to find maximum between three numbers


May 23, 2020

C program to check given number is maximum or equal between two numbers

Ex: write a C program to check number is maximum, minimum or equal between two numbers. How to write a C program to print number is maximum or equal between two numbers. C program to check number is maximum or equal between two numbers.

Input from user:

Enter the 1st number: 55

Enter the 2nd number: 25

Expected Output:

Number  55 is greater than 25




  Step by step logic of the given program:


1. First we will accept 1st number from user which is 55.


2. After that we will accept 2nd number from user which is 25.

3. After that we use if-else statement or relational operators to check number is maximum or equal.

4. If number 1 means a greater than number 2- b then we will print a is greater than b.

5. If number 2 means b is greater than a then  we print b is greater.

6. Otherwise we will print two numbers are equal.




  C program to check given number is maximum | minimum or equal between two numbers:


#include<stdio.h>
int main()
{
int a,b;

printf("Enter the 1st number\n");
scanf("%d",&a);

printf("Enter the 2nd number\n");
scanf("%d",&b);

if(a>b)
{
printf("Number %d is greater than %d",a,b);
}
else if(b>a)
{
printf("Number %d is greater than %d",b,a);
}
else
{
printf("Given Two numbers are equal\n");
}
return 0;
}


Above program shows the following output:


C program to check given number is maximum | minimum or equal between two numbers




May 21, 2020

If-Else-If statement in C

   If-else-if Statement:


In C if-else-if ladder is used to decide from among multiple options. 
It means if-else-if ladder is a way of putting multiple if's together, So by using it we can check multiple conditions.


  Syntax of if-else-if ladder:


if(condition 1)

{
     //block of statements;
}
else if(condition 2)
{
    //block of statements;
}
else if(condition 3)
{
    //block of statements;
     .
     .
     //upto n statements;

}
else
{
    // default statement;
}


Above syntax shows the working of if-else-if ladder in C programming language.


Nested If-else statement in C

   Nested If-else :

Nested if-else statement means you can use one if-else statement inside the another if-statement. It is used when you want to add conditions in if statement.


  Syntax of Nested If-else statements:


if(condition 1)

{

    if(condition 2)

    {
        //block of statements;
    }
    else
    {
       //block of statements;
    }
}
else
{
    //block of statements;
}
.
.


In above syntax we used if-else statement in the body of another if-statement. It is shows the working of  Nested If-else statement.

May 20, 2020

If-else statement in C

If-else: statements are used to control the flow of Program based on some conditions, only the difference is it is used to execute some statement code blocks. if the expression is evaluated to true, otherwise it executes else block. Which is shown in below flow chart.





  Syntax:

if(condition)
{
       //block of statements;
}
else
{
      //block of statements;
}


Above syntax shows the working of if-else statement.



If statement in C

When we need to enter the block only when given condition is true then we use if-statement. means we enter the if block only if given condition is true.



Synatx of if-statement:

if(condition)
{
               .
               .
      // block of Statements
;
}


Above syntax shows the working of if-statement. Which shows when if condition is true then it enters in if's block. Otherwise it goes outside the if block.
Let know more about if-statement with one example:


#include<stdio.h>
void main()
{

int a=15;

     if(a>10)
     {
printf("a is greater than 10");
     }

}


Above program shows the following output:




May 18, 2020

Classification of the programming languages

There are many programming languages which are used to  instruct the computer to perform various tasks.
Basically programming language defines a set of instructions that are compiled together to perform a specific task by the CPU.



Programming languages are basically devided into 3 types:

1. LOW level language

2. Middle level language
3. High level language

Which is shown in above image.


The image which is given below describes the abstraction level of hardware. Which shows  level of abstraction of languages.







  • Low level language is contains basic instructions recognised by a computer.
   Low level language is not human readable. It has basically two types:
1. Assembly language
2. Machine language 

1. Assembly language is one step closer to a high level language than machine language. In this language includes some commands such as MOV(move), ADD(add), & SUB(subtract) etc. commands are used in this type of language.

2. Machine level language is also called as low level language because data writen in this language is 0's and 1's binary format.

  • Middle level language: The best example of middle level language is C programming language because using C programming language we can do both system and application programming.

  • Hight level language is easily understoodable compared than machine level language or assembly language. It is programmer friendly language.
 Some Examples of high level language:
C, C++, Java, python etc....






May 17, 2020

What is programming


In simple words programming is a way to instruct the computer to perform various tasks.
Programming is the process of designing and building an executable computer program to accomplish a specific computing result.


That means you can use programming languages  like C, C++, java, python etc. Programming languages to create list of instructions for a computer to follow.

This means that you can provide the computer a set of instructions that are Written in any programming languages i.e, C, C++, java, python etc..etc. that computer can understand easily.

You can give any of the instructions to computer using programming languages For Example:







Like this lot of instructions you can give to a computer.

Using lot of programming languages.

May 16, 2020

C program using bitwise right shift(>>)

EX: write a C program to perform bitwise right shift operation. How to write a C program to perform bitwise right shift operation of intiger. C program to perform bitwise right shift operation of intiger.


Input from user:


Enter the number:15

Enter how many numbers you wan't to right shift:2


Expected Output:

Output after performing right shift operation is: 3






Logic to perform right shift operation of two intiger's:


 Firstly you need to know how work right shift operator.



  •  It  takes two numbers and right shift the bits of the first operand, the second  operand decides the number of places to shift.



    1. We take two intiger numbers i.e, no=15 and s=2.

    Where no shows the number which you wan't to right shift and s shows the how many bits you want to right shift.

     2. Binary representation of no>>s is-


          no    = 00001111
    ------------------------------------ 
     no>>s   = 00000011 =3(in decimal)

    3. After performing right shift operation 8 bit binary representation is 0000 0011 which is 3 in decimal.

    4. So output of the program is  3.




    Program:

    #include<stdio.h>
    void main()
    {
     int no,s;

     printf("Enter the number\n");
     scanf("%d",&no);

     printf("Enter how many bits you wan't to right shift\n");
     scanf("%d",&s);

     printf("Output after performing right-shift operation is:%d",no>>s);
    }


    Above program shows the following output: