Translate

important

What is while loop in C programming | While loop example in C | c while loop syntax

Hello friends in this tutorial I am going to share about C while loop i.e. What is while loop in C programming, while loop example in C , c while loop syntax and C while loop question and answer so without having your much time let's get started.

What is while loop in C programming


What is while loop in C programming?

A while loop in C programming repeatedly executes a target statement as long as a given condition is true, if condition becomes false the control comes out from loop.

What is infinite loop?

If the condition never becomes false the while loop keeps getting executed such a while loop is know as infinite loop .

c while loop syntax:

while(condition)

{

/*statement that will be executed repeatedly*/

/*Increment (++) or decrement(--)operation*/

}


C while loop question and answer/while loop example in C

Q.1: Write a C program to print natural number from 1 to n by accepting n value using while loop.
Ans:
#include<stdio.h>
int main()
{
int a,n;
a=1;
printf("Enter a number:");
scanf("%d",&n);
while(n>=a)
{
printf("%d\n",a);
a++;
}
return 0;
}

OUTUPT: Enter a number:10
                 1
                 2
                 3
                 4
                 5
                 6
                 7
                 8
                 9
                10

Q.2: Write a C program to print a table of the number entered by the user using C while loop.

Ans:

#include<stdio.h>
int main()
{
int a,n;
a=1;
printf("Enter the number you want table:");
scanf("%d",&n);
while(a<=10)
{
printf("%d×%d=%d\n",n,a,n*a);
a++;
}
return 0;
}

OUTPUT:Enter the number you want table:5

                5*1=5

                5*2=10

                5*3=15

                5*4=20

                5*5=25

                5*6=30

                5*7=35

                5*8=40

                5*9=45

                5*10=50

Q.3: Write a C program to print the factorial value of the entered number using a while loop.

Ans:



#include<stdio.h>
int main()
{
int n,m;
m=1;
printf("Enter a number:");
scanf("%d",&n);
while(n>=1)
{
m=m*n;
n--;
}
printf("Factorial value=%d",m);
return 0;
}

OUTPUT: Enter a number:5

                Factorial value=120

Q.4: Write a C program program to print table entered number in descending order using while loop.


#include<stdio.h>
int main()
{
int n,a;
a=10;
printf("Enter a number:");
scanf("%d",&n);
while(a>=1)
{
printf("%d*%d=%d\n",n,a,n*a);
a--;
}
return 0;
}

OUTPUT: Enter a number:10

                10*10=100

                 10*9=90

                 10*8=80

                 10*7=70

                 10*6=60

                 10*5=50

                 10*4=40

                 10*3=30

                 10*2=20

                 10*1=10

Tags

Post a Comment

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

Top Post Ad

Below Post Ad