Fifth Chapter Lesson-17: Loop Control Statement Related programs.

At the end of this lesson-

  • 1. You will be able to write a program for printing numbers from 1 to 10. Or
    • You will be able to write a program for printing the 1 2 3 4 5 6 7 8 9 10 Sequence.
  • 2. You will be able to write a program for printing numbers from 1 to n. Or
    • You will be able to write a program for printing the 1 2 3 4 – – – – n Sequence.
  • 3. You will be able to write a program for printing odd numbers from 1 to 10. Or
    • You will be able to write a program for printing the 1 3 5 7 9 Sequence.
  • 4. You will be able to write a program for printing odd numbers from 1 to n. Or
    • You will be able to write a program for printing the 1 3 5 7- – – n Sequence.
  • 5. You will be able to write a program for printing even numbers from 1 to 10. Or
    • You will be able to write a program for printing the 2 4 6 8 10 Sequence.
  • 6. You will be able to write a program for printing even numbers from 1 to n. Or
    • You will be able to write a program for printing the 2 4 6 – – – n Sequence.
  • 7. You will be able to write a program to calculate the sum of numbers from 1 to 100. Or
    • You will be able to write a program to calculate the sum of the 1+2+3+ – – +100 series.
  • 8. You will be able to write a program to calculate the sum of numbers from 1 to n. Or
    • You will be able to write a program to calculate the sum of the 1+2+3+ – – +n series.
  • 9. You will be able to write a program to calculate the sum of odd numbers from 1 to 100. Or
    • You will be able to write a program to calculate the sum of the 1+3+5+ – – +100 series.
  • 10. You will be able to write a program to calculate the sum of even numbers from 1 to 100. Or
    • You will be able to write a program to calculate the sum of the 2+4+6+ – – +100 series.

 

 

1. A program for printing numbers from 1 to 10. Or

A program for printing the 1 2 3 4 5 6 7 8 9 10 Sequence.

/* Program using for loop */ 
#include<stdio.h>
#include<conio.h>
main()
{
  int i;
  for(i=1; i<=10; i++)
  {
     printf("%d\t ",i);
  }
 getch();
}

/*Program using while loop */
#include<stdio.h>
#include<conio.h>
main()
{
  int i;
  i=1;
  while(i<=10)
  {
    printf("%d\t ",i);
    i++;
  }
  getch();
}


/*Program using do while loop*/
#include<stdio.h>
#include<conio.h>
main()
{
  int i;
  i=1;
  do
  {
    printf("%d\t ",i);
    i++;
  } while(i<=10);
  getch();
}

 

 

2. A program for printing numbers from 1 to n. Or

A program for printing the 1 2 3 4 – – – – n Sequence.

/* Program using for loop */
#include<stdio.h>
#include<conio.h>
main()
{
    int i, n;
    printf("Enter value of n: ");
    scanf("%d",&n);
    for(i=1;i<=n; i=i+1)
    {
        printf("%d\t ",i);
    }
    getch();
}

/* Program using while loop */
#include<stdio.h>
#include<conio.h>
main()
{
 int i, n;
 printf("Enter value of n: ");
 scanf("%d",&n);
 i=1;
 while(i<=n)
 {
 printf("%d\t ",i);
 i=i+1;
 }
 getch();
}

/* Program using do while loop */
#include<stdio.h>
#include<conio.h>
main()
{
 int i, n;
 printf("Enter value of n: ");
 scanf("%d",&n);
 i=1;
 do
 {
 printf("%d\t ",i);
 i=i+1;
 } while(i<=n);
 getch();
}

 

 

3. A program for printing odd numbers from 1 to 10. Or

A program for printing the 1 3 5 7 9 Sequence.

/* Program using for loop */
#include<stdio.h>
#include<conio.h>
main()
{
    int i;
    for(i=1;i<=10; i=i+2)
    {
        printf("%d\t ",i);
    }
    getch();
}



/* Program using while loop */
#include<stdio.h>
#include<conio.h>
main()
{
 int i;
 i=1;
 while(i<=10)
 {
   printf("%d\t ",i);
   i=i+2;
 }
 getch();
}


/* Program using do while loop*/
#include<stdio.h>
#include<conio.h>
main()
{
 int i;
 i=1;
 do
 {
 printf("%d\t ",i);
 i=i+2;
 } while(i<=10);
 getch();
}

 

 

4. A program for printing odd numbers from 1 to n. Or

A program for printing the 1 3 5 7- – – n Sequence.

/* Program using for loop */
#include<stdio.h>
#include<conio.h>
main()
{
    int i,n;
    printf("Enter Value of n: ");
    scanf("%d",&n);
    for(i=1;i<=n; i=i+2)
    {
        printf("%d\t ",i);
    }
    getch();
}



/* Program using while loop */
#include<stdio.h>
#include<conio.h>
main()
{
 int i,n;
 printf("Enter Value of n: ");
 scanf("%d",&n);
 i=1;
 while(i<=10)
 {
   printf("%d\t ",i);
   i=i+2;
 }
 getch();
}


/* Program using do while loop */
#include<stdio.h>
#include<conio.h>
main()
{
 int i,n;
 printf("Enter Value of n: ");
 scanf("%d",&n);
 i=1;
 do
 {
 printf("%d\t ",i);
 i=i+2;
 } while(i<=10);
 getch();
}

 

 

5. A program for printing even numbers from 1 to 10. Or

A program for printing the 2 4 6 8 10 Sequence.

 

/* Program using for loop */
#include<stdio.h>
#include<conio.h>
main()
{
    int i;
    for(i=2;i<=10; i=i+2)
    {
        printf("%d\t ",i);
    }
    getch();
}


/* Program using while loop */
#include<stdio.h>
#include<conio.h>
main()
{
    int i;
    i=2;
    while(i<=10)
    {
        printf("%d\t ",i);
        i=i+2;
    }
    getch();
}


/* Program using do while loop */
#include<stdio.h>
#include<conio.h>
main()
{
    int i;
    i=2;
    do
    {
        printf("%d\t ",i);
        i=i+2;
    } while(i<=10);
    getch();
}

 

6. A program for printing even numbers from 1 to n. Or

A program for printing the 2 4 6 – – – n Sequence.

/* Program using for loop */
#include<stdio.h>
#include<conio.h>
main()
{
    int i,n;
    printf("Enter Value of n: ");
    scanf("%d",&n);
    for(i=2;i<=n; i=i+2)
    {
        printf("%d\t ",i);
    }
    getch();
}


/* Program using while loop */
#include<stdio.h>
#include<conio.h>
main()
{
    int i,n;
    printf("Enter Value of n: ");
    scanf("%d",&n);
    i=2;
    while(i<=n)
    {
        printf("%d\t ",i);
        i=i+2;
    }
    getch();
}



/* Program using do while loop */
#include<stdio.h>
#include<conio.h>
main()
{
    int i,n;
    printf("Enter Value of n: ");
    scanf("%d",&n);
    i=2;
    do
    {
        printf("%d\t ",i);
        i=i+2;
    } while(i<=n);
    getch();
}

 

 

7. A program to calculate the sum of numbers from 1 to 100. Or

A program to calculate the sum of the 1+2+3+ – – +100 series.

/* Program using for loop */
#include<stdio.h>
#include<conio.h>
main()
{
    int i,s=0;
    for(i=1;i<=100; i=i+1)
    {
        s=s+i;
    }
    printf("Sum=%d ",s);
    getch();
}



/* Program using while loop */
#include<stdio.h>
#include<conio.h>
main()
{
    int i,s=0;
    i=1;
    while(i<=100)
    {
        s=s+i;
        i=i+1;
    }
    printf("Sum=%d ",s);
    getch();
}



/* Program using do while loop */
#include<stdio.h>
#include<conio.h>
main()
{
    int i,s=0;
    i=1;
    do
    {
        s=s+i;
        i=i+1;
    } while(i<=100);
    printf("Sum=%d ",s);
    getch();
}

 

 

8. A program to calculate the sum of numbers from 1 to n. Or

A program to calculate the sum of the 1+2+3+ – – +n series.

/* Program using for loop */
#include<stdio.h>
#include<conio.h>
main()
{
    int i,n,s=0;
    printf("Enter Value of n: ");
    scanf("%d",&n);
    for(i=1;i<=n; i=i+1)
    {
        s=s+i;
    }
    printf("Sum=%d ",s);
    getch();
}



/* Program using while loop */
#include<stdio.h>
#include<conio.h>
main()
{
    int i,n,s=0;
    printf("Enter Value of n: ");
    scanf("%d",&n);
    i=1;
    while(i<=n)
    {
        s=s+i;
        i=i+1;
    }
    printf("Sum=%d ",s);
    getch();
}



/* Program using do while loop */
#include<stdio.h>
#include<conio.h>
main()
{
    int i,n,s=0;
    printf("Enter Value of n: ");
    scanf("%d",&n);
    i=1;
    do
    {
        s=s+i;
        i=i+1;
    } while(i<=n);
    printf("Sum=%d ",s);
    getch();
}

 

 

9. A program to calculate the sum of odd numbers from 1 to 100. Or

A program to calculate the sum of the 1+3+5+ – – +100 series.

/* Program using for loop */
#include<stdio.h>
#include<conio.h>
main()
{
int i,s=0;
for(i=1; i<=100; i=i+2)
{
    s=s+i;
}
printf("Sum=%d ",s);
getch();
}


/* Program using while loop */
#include<stdio.h>
#include<conio.h>
main()
{
int i,s=0;
i=1;
while(i<=100)
{
    s=s+i;
    i=i+2;
}
printf("Sum=%d ",s);
getch();
}


/* Program using do while loop */
#include<stdio.h>
#include<conio.h>
main()
{
int i,s=0;
i=1;
do
{
    s=s+i;
    i=i+2;
} while(i<=100);
printf("Sum=%d ",s);
getch();
}

 

 

10. A program to calculate the sum of even numbers from 1 to 100. Or

A program to calculate the sum of the 2+4+6+ – – +100 series.

/* Program using for loop */
#include<stdio.h>
#include<conio.h>
main()
{
    int i,s=0;
    for(i=2;i<=100; i=i+2)
    {
        s=s+i;
    }
    printf("Sum=%d ",s);
    getch();
}



/* Program using while loop */
#include<stdio.h>
#include<conio.h>
main()
{
    int i,s=0;
    i=2;
    while(i<=100)
    {
        s=s+i;
        i=i+2;
    }
    printf("Sum=%d ",s);
    getch();
}



/* Program using do while loop */
#include<stdio.h>
#include<conio.h>
main()
{
    int i,s=0;
    i=2;
    do
    {
        s=s+i;
        i=i+2;
    } while(i<=100);
    printf("Sum=%d ",s);
    getch();
}

11. Write a program to calculate factorial of a positive number.

#include <stdio.h>
main()
{
    int n, i;
    long int fact = 1;
    printf("Enter an integer: ");
    scanf("%d",&n);
    if(n<0)
        printf("Error! Factorial of a negative number doesn't exist.");
    else
    {
        for (i=1; i<=n; i++)
        {
            fact=fact * i;
        }
        printf("Factorial= %ld", fact);
    }
}

 

Practice

  • 1. Write a program for printing the 5 9 13 17 – – -n series.
  • 2. Write a program for printing the 1 4 9 16 25 – – -n series.
  • 3. Write a program for printing the 1 8 27 64 – – -n series.
  • 4. Write a program for printing the 1 4 27 256 – – -n series.
  • 5. Write a program to calculate the sum of odd numbers from 1 to n. Or
    • Write a program to calculate the sum of the 1+3+5+ – – +n series.
  • 6. Write a program to calculate the sum of even numbers from 1 to n. Or
    • Write a program to calculate the sum of the 2+4+6+ – – +n series.
  • 7. Write a program to calculate the sum of the 12+22+32+42+- – -+ n2 series.
  • 8. Write a program to calculate the sum of the 13+23+33+43+- – -+ n3 series.
  • 9. Write a program to calculate the sum of the 11+22+33+44+ – – -+nn series.

 

Lesson Evaluation-

Knowledge Based Questions:

Comprehension Based Questions:

Creative Questions:

Multiple Choice Questions:

 


Written by,

Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *