0% found this document useful (0 votes)
23 views7 pages

C++ While Loop Examples and Usage

The document contains several C++ code snippets demonstrating the use of while loops for various tasks, including displaying the first 10 numbers, generating a multiplication table for 2, calculating the sum of the first 100 numbers, and computing the factorial of a given integer. Each code example includes necessary headers and uses basic input/output functions. The overall focus is on illustrating the syntax and functionality of while loops in programming.

Uploaded by

atelkartik
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views7 pages

C++ While Loop Examples and Usage

The document contains several C++ code snippets demonstrating the use of while loops for various tasks, including displaying the first 10 numbers, generating a multiplication table for 2, calculating the sum of the first 100 numbers, and computing the factorial of a given integer. Each code example includes necessary headers and uses basic input/output functions. The overall focus is on illustrating the syntax and functionality of while loops in programming.

Uploaded by

atelkartik
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

/*

while loop
syntax :

in aliza on of loop/index/control variable; (set


counter)

while(condi on)
{
body of loop;

increment / decrement of loop varibale;


}
*/

/*

#include<iostream.h>
#include<conio.h>

// to display 1st 10 numbers

void main()
{
clrscr();
int i = 1;

while(i<=10)
{
cout<<i<<endl;
i++;
}
getch();
}

*/

/*

#include<iostream.h>
#include<conio.h>

// to display mul plica on table of 2


void main()
{
clrscr();
int i = 1;

while(i<=10)
{
cout<<i * 2<<endl;
i++;
}
getch();
}

*/

/*

#include<iostream.h>
#include<conio.h>

// to display sum of 1st 100 numbers


void main()
{
clrscr();
int i = 1;
long sum = 0;

while(i<=100)
{
sum = sum + i;
i++;
}

cout<<"Sum = "<<sum;
getch();
}

*/
/*

#include<iostream.h>
#include<conio.h>

// to display sum of 1st n numbers

void main()
{
clrscr();
int i = 1, n;
long sum = 0;

cout<<"How many numbers?";


cin>>n;

while(i <= n)
{
sum = sum + i;
i++;
}

cout<<"Sum = "<<sum;
getch();
}

*/

#include<iostream.h>
#include<conio.h>

// factorial

void main()
{
clrscr();

int i = 1, n; // n! = 1 * 2 * 3 * 4 * ..... n
long product = 1; // 5! = 1 * 2 * 3 * 4 * 5 =
120

cout<<"Enter any int number: ";


cin>>n;

while(i <= n)
{
product = product * i;

i++;
}

cout<<"factorial = "<<product;

getch();

You might also like