Skip to main content

Learning While LOOP


While LOOP

In this part you will learn:
1. While LOOP
2. C syntax
3. Showing output

In this tutorial I will teach you how to use while loops in C. We have already covered for loops and now we know what are loops

What is a WHILE LOOP ?

First of all we will talk about what is while loop. For a loop we need three statement initialization, condition and increment. In a while loop we only write the condition within the round brackets and increment within the body of the loop. We initialize the variable outside the loop.

Checking for a prime number

Basic Step:
Open Dev C++ then File > new > source file and start writing the code below.

  1. #include<stdio.h>
  2. #include<conio.h>

These are the header files that we need for coding. The main header is stdio.h which provides us with most of the functions for coding in C and the second header files is just for using a function which can pause the screen until the user hits any key.

  1. int main()
  2. {
  3. int num,i=2;

For finding whether a number is prime or not we will simply take input in a number and make a loop counter to test whether it is divisible by numbers from 2 to 1 less than the number if yes then it is a prime number.

  1. printf("Enter number :");
  2. scanf("%d",&num);

Now taking input from the user.

  1. while ( i<num){
  2. if (num %i==0 ){
  3. printf("\nNumber is not a prime number\n ");
  4. break;
  5. }
  6. i+=1;
  7. }

Now we write the while loop. Note that we already initialized the i variable with 2 ( because we will divide the number from 2 to 1 less than the number in order to find whether the number is prime or not.). After that we write the conditional statement If the remainder of number divided by loop counter is 0 then it is not a prime number. After the condition we increment the variable.

  1. if (i== num)
  2. printf("\nNumber is a prime number\n ");
  3. else
  4. printf("\nNumber is not a prime number\n ");
  5. _getch();
  6. return 0;
  7. }

If the loop is finished without the condition becoming true and the incremented value becomes equal to the number then that number is a prime number. Otherwise not.

Execute > compile
then Execute > run
Output
output

SUM OF DIGITS
File > new source file

  1. #include<stdio.h>
  2.  
  3. int main()
  4. {
  5. int num,temp=0,sum=0;
  6.  
  7. printf("Enter the number :");
  8. scanf("%d",&num);
  9. }

For calculating the sum of digits of number we need three variables number, temporary variable and sum. We take the input in number.
  1. while(num>0)
  2. {
  3. temp = num%10;
  4. sum +=temp;
  5.  
  6. printf("\nDigit is : %d\n",temp);
  7. num=num/10;
  8. }
  9. printf("Sum of digits is : %d\n",sum);
  10. return 0;
  11. }

Now we write the loop condition that run until the number is greater than 0 because we will start separating the digits form Least significant to most significant. We take the remainder of number with 10 to separate the digit and simple store it in our temp variable and add that variable to sum until the number is greater than 0. In the end we simply print the sum and return.

Execute > compile
then Execute > run

Output:
Output

Tags

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.