Using while loop we will calculate the square value of different integer numbers. This process will continue until the user wants to discontinue the loop.
#include < stdio.h >
void main ()
{
char ch;
int num, sq;
clrscr ();
printf ("Want to display the square value? (y/n)\n");
scanf ("%c", &ch);
while (ch!='n')
{
printf ("Enter an integer\n");
scanf ("%d", &num);
sq=num*num;
printf ("The square of %d", num);
printf (" is: %d\n", sq);
fflush (stdin);
printf ("Any more\n");
scanf ("%c", &ch);
}
getch ();
}
Here in this program the user can enter integer values as long as he wants. At the end of input the program will display the total value of the inputs, their average and the total number of inputs.
#include < stdio.h >
void main ()
{
char ch;
int num, sum, n;
clrscr ();
sum=0;
n=0;
printf ("Want to start? (y/n)\n");
scanf ("%c", &ch);
while (ch!='n')
{
printf ("Enter an integer\n");
scanf ("%d", &num);
sum=sum+num;
n++;
fflush (stdin);
printf ("Any more\n");
scanf ("%c", &ch);
}
printf ("The sum is: -%d\n", sum);
printf ("Average is: -%.2f\n", (float) sum/n);
printf ("Total number of input is:-%d\n", n);
getch ();
}
C program on palindrome checking
#include < stdio.h >
void main ()
{
int i, x, a, j=0;
clrscr ();
printf ("Enter the value");
scanf ("%d", &x);
a=x;
while (x>10)
{
i=x%10;
x=x/10;
j=(j*10)+i;
}
j=(j*10)+x;
if (j==a)
printf ("The number is palindrome ");
else
printf ("not");
getch ();
}
In the next exercise we can take maximum 100 inputs from the user. The user can enter any double value and the square root of the value will be displayed. User can stop the loop by entering 10000.If the user enters any negative value, then the input will not be counted and the program will ask for another input. At the end of the loop, the program will display the total number of valid inputs and the total number of negative inputs by the user.math.h is a header file which contains several math functions.sqrt () is a function of math.h file. This function takes a value (integer, float or double) and returns it's square root value.
#include < stdio.h >
#include < math.h >
void main ()
{
int count, negative;
double number, sqroot;
clrscr ();
printf ("Enter 10000 to stop\n");
count=0;
negative=0;
while (count<=100)
{
printf ("Enter a number-\n");
scanf ("%lf", &number);
if (number==10000)
break;
if (number<0)
{
printf ("The number is negative.\n");
negative++;
continue;
}
sqroot=sqrt (number);
printf ("Number=%.2lf\nSquare root = %.2lf\n", number, sqroot);
count++;
}
printf ("Total valid inputs:-%d\n", count);
printf ("Total negative inputs:-%d", negative);
getch ();
}
Program to calculate a to the power of n using while loop.
#include < stdio.h >
void main ()
{
int count, a, result, n;
clrscr ();
result=1;
count=1;
printf ("Enter two integer values:");
scanf ("%d%d", &a, &n);
while (count<=n)
{
result=result*a;
count++;
}
printf ("a= %d; n=%d; a to the power n=%d", a, n, result);
getch ();
}
do-while loop
do-while loop or do loop is an exit control type loop. That means that the condition of the loop is evaluated at the end of the loop body. In for and while loop we found that the loop control variable was evaluated against a value before the start of the loop body. In case, the condition is false, the loop body won't be executed at all. This is not the case with do-while loop.
As the condition is evaluated at the end of the loop body, the loop will be executed unconditionally for the first time. At the end of the first iteration if the condition is found true, then the loop will be executed again. Otherwise the loop will stop execution after the first iteration.
The syntax of do-while loop is: -
do
{
body of the loop
} while (condition);
Any Question ? Put Your comments
#include < stdio.h >
void main ()
{
char ch;
int num, sq;
clrscr ();
printf ("Want to display the square value? (y/n)\n");
scanf ("%c", &ch);
while (ch!='n')
{
printf ("Enter an integer\n");
scanf ("%d", &num);
sq=num*num;
printf ("The square of %d", num);
printf (" is: %d\n", sq);
fflush (stdin);
printf ("Any more\n");
scanf ("%c", &ch);
}
getch ();
}
Here in this program the user can enter integer values as long as he wants. At the end of input the program will display the total value of the inputs, their average and the total number of inputs.
#include < stdio.h >
void main ()
{
char ch;
int num, sum, n;
clrscr ();
sum=0;
n=0;
printf ("Want to start? (y/n)\n");
scanf ("%c", &ch);
while (ch!='n')
{
printf ("Enter an integer\n");
scanf ("%d", &num);
sum=sum+num;
n++;
fflush (stdin);
printf ("Any more\n");
scanf ("%c", &ch);
}
printf ("The sum is: -%d\n", sum);
printf ("Average is: -%.2f\n", (float) sum/n);
printf ("Total number of input is:-%d\n", n);
getch ();
}
C program on palindrome checking
#include < stdio.h >
void main ()
{
int i, x, a, j=0;
clrscr ();
printf ("Enter the value");
scanf ("%d", &x);
a=x;
while (x>10)
{
i=x%10;
x=x/10;
j=(j*10)+i;
}
j=(j*10)+x;
if (j==a)
printf ("The number is palindrome ");
else
printf ("not");
getch ();
}
In the next exercise we can take maximum 100 inputs from the user. The user can enter any double value and the square root of the value will be displayed. User can stop the loop by entering 10000.If the user enters any negative value, then the input will not be counted and the program will ask for another input. At the end of the loop, the program will display the total number of valid inputs and the total number of negative inputs by the user.math.h is a header file which contains several math functions.sqrt () is a function of math.h file. This function takes a value (integer, float or double) and returns it's square root value.
#include < stdio.h >
#include < math.h >
void main ()
{
int count, negative;
double number, sqroot;
clrscr ();
printf ("Enter 10000 to stop\n");
count=0;
negative=0;
while (count<=100)
{
printf ("Enter a number-\n");
scanf ("%lf", &number);
if (number==10000)
break;
if (number<0)
{
printf ("The number is negative.\n");
negative++;
continue;
}
sqroot=sqrt (number);
printf ("Number=%.2lf\nSquare root = %.2lf\n", number, sqroot);
count++;
}
printf ("Total valid inputs:-%d\n", count);
printf ("Total negative inputs:-%d", negative);
getch ();
}
Program to calculate a to the power of n using while loop.
#include < stdio.h >
void main ()
{
int count, a, result, n;
clrscr ();
result=1;
count=1;
printf ("Enter two integer values:");
scanf ("%d%d", &a, &n);
while (count<=n)
{
result=result*a;
count++;
}
printf ("a= %d; n=%d; a to the power n=%d", a, n, result);
getch ();
}
do-while loop
do-while loop or do loop is an exit control type loop. That means that the condition of the loop is evaluated at the end of the loop body. In for and while loop we found that the loop control variable was evaluated against a value before the start of the loop body. In case, the condition is false, the loop body won't be executed at all. This is not the case with do-while loop.
As the condition is evaluated at the end of the loop body, the loop will be executed unconditionally for the first time. At the end of the first iteration if the condition is found true, then the loop will be executed again. Otherwise the loop will stop execution after the first iteration.
The syntax of do-while loop is: -
do
{
body of the loop
} while (condition);
Any Question ? Put Your comments
printing those numbers that are divisible by 3 or 7 between 1 to 100
ReplyDeleteThe loop will be as follows:
ReplyDeletefor(i=1 i< =100;i++)
{
if(i%3==0 || i%7==0)
printf(" %d",i);
}
printing numbers that are diviible by 3 and 10.
ReplyDelete?
#include< stdio.h>
ReplyDeletevoid main ()
{
int i,n;
clrscr();
printf("\nenter the range:");
scanf("%d",&n);
printf("\nNumbers divisible by 3 and 10 are as follows\n"");
for(i=1;i< =n;i++)
{
if(n%3==0 && i%10==0)
printlf(" %d",i);
}
getch();
}
sir,
ReplyDeletewhat will be the program if we have to show odd numbers between 0-20 using while???
#include< stdio.h>
ReplyDeletevoid main()
{
int i=1;
while(i< =20)
{
printf("\n%d",i);
i=i+2;
}
getch();
}
sir can u help me?i need a program in c for this:
ReplyDeleteThe program should meet the following objectives:
1. To determine the number of items purchased per customer (it should be in the range of 1-10)
2. To input price per item.
3. To determine if there are new customers and repeat steps 1 and 2.
4. To calculate and output total expenses spent by all customers visiting the shop on a particular day
5. To determine and output the number of customers
6. To calculate and output the average spent by a customer.
can uhelp me with a c++ menu base program for 11 standard
ReplyDeleteCheck my recent post
ReplyDeletevoid main()
ReplyDelete{
int n,cus=0;
float price,avg,sum=0;
char ans1='y';
clrscr();
while (ans1!='n')
{
cus++;
printf("\nEnter the no of items");
scanf("%d",&n);
printf("\nEnter the price");
scanf("%f",&price);
sum=sum+price*n;
printf("\nAny More(y/n):");
ans1=getche();
}
avg=sum/cus;
printf("Averege spend by a customer=%.2f",avg);
printf("Number of customers visited=%d",cus);
printf("Total collection=%.2f",sum);
getch();
}