Showing posts with label while loop. Show all posts
Showing posts with label while loop. Show all posts

Wednesday, 25 May 2011

Program to count words and characters in a sentence

#include<stdio.h>
#include<conio.h>
void main()
{
 int wordcnt=0;
 int charcnt=0;
 char ch;
 clrscr();
 printf("Type a sentence: ");
 while((getche()) != '\r')
  {
   charcnt++;
   if(ch==' ')
   wordcnt++;
  }
 printf("\nTotal characters are: %d",charcnt);
 printf("\nTotal words are: %d",wordcnt+1);
 getch(); 


}

Tuesday, 24 May 2011

Program to count characters in a Phrase

#include<stdio.h>
#include<conio.h>
void main (void)
{
 int count=0;
 clrscr();
 printf("Enter a phrase:\n");
 while(getche() != '\r')
 count++;
 printf("\n");
 printf("Total Characters are %d",count);
 getch();
}




If you have any question, you can ask me freely!

Monday, 23 May 2011

The while Loop

The second kind of loop available in C is the while loop. Here is theexample code:


included files
void main()
{
 int count=0;
 int total=0;
 clrscr();
 while(count<10)
 {
  total=total+count;
  printf("count=%d, total=%d",count++,total): 
 }
 getch();
}


The output of the program will be:
count=0, total=0
count=1, total=1
count=2, total=3
count=3, total=6
count=4, total=10
count=5, total=15
count=6, total=21
count=7, total=28
count=8, total=36
count=9, total=45




What is this?