1.
#include%26lt;stdio.h%26gt;
main()
{
char a,b;
{
while('a'%26lt;'b')
printf("\n malayalam is a palindrome");
}}
.........................................
2 nd pgm
#include%26lt;stdio.h%26gt;
main()
{
int i;
while(i=10)
{
printf("\n%d",i);
i=i+1;
}
}
I am running c pgm,using while loop.but output won't terminate.what may be the prob.i wrote pgm below.?
Program 1 Corrections.
A while loop needs a condition that will eventually stop. You are comparing two characters with static values. This means that your loop will never stop. A fix would be something along the lines of:
main()
{ int a=0;//initialize a as integer with a value smaller than b
int b=10;//initialize b as your static value
while(a%26lt;b)//compare a and b and continue until a is not less than b
{
printf(\n malayalam is a palindrome"); // same as yours
a++; //increments a so loop eventually stops
}
}
Program 2
you need to initialize variable i first then use boolean operators to compare i with something until its done.
example:
main()
{
int i=0;
while(i%26lt;=10)//as long as i is less than or equal to 10 do something
{
...//code you want to execute
i++; //increment i
}
If I were you I would go take a look again at the definitions of boolean operators, variable types and while loops. These are basic things that you will need to master before you can move on to bigger and more complex programs.
Reply:u r saying they dont terminate its because 'a' is always less than 'b' so in first program the loop goes on till a%26lt;b which is always true
in second one i=10
which is true ie i is non zero and hence lopp is always running hence causing program not to terminate
Reply:Item 1:
Your while loop is always true, therefore it won't terminate. The code while('a'%26lt;'b') is telling the program to run while the ASCII code of character b is greater than the ASCII code of character a. Since ASCII code of b is 98 and ASCII code of a is 97, 'b' is always greater than 'a', therefore your loop won't terminate.
If you want to loop the program when variable b is greater than variable a, then the code should be:
while (a%26lt;b)
{
...
}
but inside the while loop there must be a statement changing the value of a or b so that at one point b will be less than or equal to a. Also you have to assign values to a and b before the while loop. On some compilers, a and b will be initialized to zero, on others they will contain rubbish so your program will be unpredictable.
Item 2:
The term inside the while loop i=10 is an assignment statement, i.e. you are assigning 10 to i. This process results in a true value which is used by the while statement.
Perhaps you want something like this:
while (i==10)
{
...
}
but this while loop will not execute if you did not assign 10 to i in the first place. This loop will only run once in this case because i will become 11 when it is incremented by i=i+1 statement.
The code that will do the loop several times and stop when count reaches 10 would be
while (i%26lt;10)
{
...
i=i+1; //or i++;
}
Again you have to initialize i to start with 0 or some number before the while loop.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment