Add

Basic For Loop program in c langauage | Top 10 loop programs for Beginners

Let's see the Top 10  Loop program in c langauage.

Best Way To Learn C Programming free

Top 10 Basic C Programs For Beginners

1.Basic for Loop Program.

Every loop consists of three parts in a sequence.

Initialization: Use to initialize the loop variable.

Condition: It is checked after each iteration as an entry point to the loop.

Updation: Incrementing the loop variable to eventually terminate the loop not satisfying the loop condition.

Syntax:

for(initialization, condition, incrementation)

{

    code statements;

}

Below is a simple program on for loop.

Here is the C language tutorial explaining for Loop → For Loop in C.

#include<stdio.h>

int main()

{

    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    /*

        Always declare the variables before using them

    */

    int i = 0;  // declaration and initialization at the same time

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

    {

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

        /*

            consequently, when i equals 10, the loop breaks.

            i is updated before the condition is checked-

            hence the value of i after exiting the loop is 10

        */

     }

    printf("\n\The value of i after exiting the loop is %d\n\n", i);

    printf("\nRemember that the loop condition checks the conditional statement

     before it loops again.\n\n");

    printf("Consequently, when i equals 10, the loop breaks.\n\n");

    printf("i is updated before the condition is checked- hence the value of i after

    exiting the loop is 10 .\n\n");

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

 

2. Simple while Loop Program.

#include<stdio.h>

int main()

{

    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    /*

        always declare the variables before using them

    */

    int i = 0;  // declaration and initialization at the same time.

    printf("\nPrinting numbers using while loop from 0 to 9\n\n");

    /*

        while i is less than 10

    */

    while(i<10)

    {

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

        /*

            Update i so the condition can be met eventually

            to terminate the loop

        */

        i++;    // same as i=i+1;

      }

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

3. Basic do while Loop Program

#include<stdio.h>

int main()

{

    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    /*

        always declare the variables before using them

    */

    int i = 10;     // declaration and initialization at the same time

    do // do contains the actual code and the updation

    {

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

        i = i-1;    // updation

    }

    // while loop doesn't contain any code but just the condition

    while(i > 0);

    printf("\n\The value of i after exiting the loop is %d\n\n", i);

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

 

 

4. Program to show use of nested for Loops.

Nested loops are usually used to print a pattern in C. They are also used to print out the matrix using a 2 dimensional array and a lot of other patterns like pyramid of numbers etc.

  • Using a loop inside another loop is called nested loop.

#include<stdio.h>

int main()

{

    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    printf("\n\nNested loops are usually used to print a pattern in c. \n\n");

    printf("\n\nThey are also used to print out the matrix using a 2 dimensional array. \n\n");

    int i,j,k;

    printf("\n\nOutput of the nested loop is :\n\n");

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

    {

        printf("\t\t\t\t");

        for(j = 0; j < 5; j++)

        printf("* ");

        printf("\n");

    }

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;  }

5. Program to print Factorial of a Number.

  •  Program to find factorial of a number using for loop.

#include<stdio.h>

#include<conio.h>

void main()

{

   int fact, i, n;

   fact = 1;

   printf("Enter the number\t");

   scanf("%d" , &n);

   for(i = 1; i <= n; i++)

   {

       fact = fact*i;

   }

   printf("Factorial of %d is %d", n , fact);

   getch();

}

 

6. Program to print the Fibonacci Series.

  •  Program to print the Fibonacci series using while loop.

#include<stdio.h>

#include<conio.h>

void fibonacci(int num);

void main()

{

    int num = 0;

    clrscr();

    printf("Enter number of terms\t");

    scanf("%d", &num);

    fibonacci(num);

    getch();

}

void fibonacci(int num)

{

   int a, b, c, i = 3;

   a = 0;

   b = 1;

   if(num == 1)

   printf("%d",a);

   if(num >= 2)

   printf("%d\t%d",a,b);

   while(i <= num)

   {

      c = a+b;

      printf("\t%d", c);

      a = b;

      b = c;

      i++;

   }

}

 

7. Program to check whether a Number is a Palindrome.

  • A palindrome is a number or a string which is similar when read from the front and from the rear.

#include<stdio.h>

#include<conio.h>

void main()

{

    int a, b, c, s = 0;

    clrscr();

    printf("Enter a number:\t");

    scanf("%d", &a);

    c = a;

   // the number is reversed inside the while loop.

   while(a > 0)

   {

      b = a%10;

      s = (s*10)+b;

      a = a/10;

   }

   // here the reversed number is compared with the given number.

   if(s == c)

   {

      printf("The number %d is a palindrome", c);

   }

   else

   {

      printf("The number %d is not a palindrome", c);

   }

  getch();

}

 

8. Program to find Sum of Digits of a Number.

#include<stdio.h>

int main()

{

            printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

            int n, sum = 0, c, remainder;

            printf("Enter the number you want to add digits of:  ");

            scanf("%d", &n);

            while(n != 0)

            {

            remainder = n%10;

            sum += remainder;

            n = n/10;

            }

            printf("\n\nSum of the digits of the entered number is  =  %d\n\n", sum);

            printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n");

            return 0;

}

 

9. Program to reverse a String.

we will learn about how to reverse a given String. If the string is "hello" then,

the output should be "olleh". We can use this concept to check the palindrome.

Because the palindrome string will have the same value even after we reverse it.

By using the new character array.

By swapping the characters of the string.

By using standard library functions.

10. Reverse a String by using the new character array:

 this example, firstly we take an input from the user, after taking an input we have to calculate the length of the string. To calculate the length we run a loop from the starting of the character array till a null character found ('\0') and in each iteration, we increase the count variable. We assign one less than it to the j because the array starts from zero. After this, we simply copy the characters from the ending one by one from the original character array to a new character array.

#include <stdio.h>

int main()

{

  char str[1000], rev[1000];

  int i, j, count = 0;

  scanf("%s", str);

  printf("\nString Before Reverse: %s", str);

  //finding the length of the string

  while (str[count] != '\0')

  {

    count++;

  }

  j = count - 1;

  //reversing the string by swapping

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

  {

    rev[i] = str[j];

    j--;

  }

  printf("\nString After Reverse: %s", rev);

}

 

 

 

 

Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.