Showing posts with label while_loop. Show all posts
Showing posts with label while_loop. Show all posts

C program to count the number of lower upper case letters and digits in a given file named data.txt

 In this I tell how to write a C program that count number of letters and digits from a given file. C program to display text from file on output. So, without wasting our time let's get started .

Code:

#include<stdio.h>
#include<stdlib.h>

int main(){
  FILE *ptr;//this we make to point towards file 
  ptr=fopen("data.txt","r");//we are opening the file  in reading mode 
  char ch;//to store char variable 
  int int_count=0;      //we need to initilize counter from zero 
  int letter_count=0;   //we need to initilize counter from zero 
  while (1)
  {
  ch=fgetc(ptr);
  if((ch>=65 && ch<=90) || (ch>=97 && ch<=122)){
      letter_count++;
  }
  else if(ch>=48 && ch<=57){
      int_count++;
  }
  else if(ch==EOF){
      break;
  }
  }
  printf("Number of letters :  %d\n",letter_count);
  printf("Number of Integers :  %d\n",int_count);
  fclose(ptr);//this will close file  


    return 0;
}

Screenshot of Text file:

                      Here we write alphabets and Integers in Text file

Logic:

for our program we need to create a text file . Here I create file name data.txt. Here we first make pointer which points towards file ptr.  And a variable ch of type character so that it can get character from file in it .And we also declare two variable int_count and letter_count and initialize both form 0 And then we open a file using Fopen  in reading mode . And then we run a infinite loop and control only come out form it only when ch is equal to EOF(end of file) and break take him out of loop  . And we give condition for it in last else if. Now let's talk about other two if condition . In first if we check whether ch comes between or equal(65 to 90) is Ascii value for small alphabet .Then we use || (or) and give Ascii value for big alphabet ( 97 to 122). And if condition comes true then increase value of letter_cout  by one . And if not then it comes in else if and we check ch  for Integers using their Ascii value(48 to 57). And this condition is true then increase value of int_count by one. Then we print using printf number of letters and number of integers . Then we release our buffer that is reversed by file using fclose.

Output:


  Here you can see the output that it give number of letters 40 and number of integers 6 . And we get our desired output . Try this program by self and please provide me feedback by commenting your views. I hope you find this helpful  in your  work . 
                                                   Thanks  for reading



C program to count number of lowercase and Upper case letters in String

 In this post I will tell you how to make a program that count number of uppercase letters and lowercase letters in a string  Know about counting integers . So, without any further delay let's started.

Code:

#include<stdio.h>
#include<string.h>

void numberLetter(char s[]){
    int i=0;
    int j=0;
    int k=0;
    while(i<strlen(s)){
      if(s[i]>=65 && s[i]<=90){
             j++;
      }
      else if(s[i]>=97 && s[i]<=122){
          k++;
      }
      i++;
    }
    printf("Number of Upper case letters : %d\n",j);
    printf("Number of Lower case letters : %d\n",k);
}
int main(){
char str[100];
printf("Enter any String \n");
scanf(%[^\n]s",str);//take the input till user press enter 
numberLetter(str);
    return 0;
}

Logic:

Here first we declare a String of name str[100] that will take up to hundred characters. Then we use printf statement to said user to enter string . And using scanf we take input . And you see I give some space between  double quote and % because we are telling compiler that only read string not white space.  Here we ^\n use this operator to tell compiler take input till user press Enter. Then we define a function called numberLetter which take string as its arguments. And in that we define three variable of int i , j ,k and initialize it from 0 . Then we run a while loop till the length given of string and we gets its length using strlen() function and that is why we include string.h our header file .  Then we use if condition and check each character of string between  and equal to 65 and 90 . Because they are their Ascii value . If this condition is true then j++  increase value of j. And for lower case we check between and equal to 97 and 122. And if this condition is true then k++ will increase value of k. Then we give i++ which will get in work till our loop condition. And value of j and every time when condition is true.  And in end we use printf to print the output . Know about basic C. Then we call numberLetter function in main and give str as parameter. 

Output:


You see that it works very well as we think according to logic and code. You must try this by yourself and become better programmer . I hope that you find this helpful in your work .                                     Thanks for reading.


Sum of Five digit number

 In this I will tell you how to do sum of five digit number . So, without any further delay let's get started .Like our previous post we here going to use all three loops in C .

Code:

Using for loop

#include <stdio.h>
//sum of five digit number using loop
int main()
{
    int number;
    printf("Enter any  five digit number\n");
    scanf("%d", &number);
    int sum = 0;
    int number1;
    for (int i = 0i < 5i++)
    {
        number1 = number % 10;
        sum = sum + number1;
        number = number / 10;
    }
    printf("%d \n"sum);
    return 0;
}

Using while loop 

#include <stdio.h>
//sum of five digit number using loop
int main()
{
    int number;
    printf("Enter any  five digit number\n");
    scanf("%d", &number);
    int sum = 0;
    int number1;
    int i=0;
    while(i<5){
        number1 = number % 10;
        sum = sum + number1;
        number = number / 10;
        i++;
    }
    printf("%d \n"sum);
    return 0;
}

Using do-while loop 

#include <stdio.h>
//sum of five digit number using loop
int main()
{
    int number;
    printf("Enter any  five digit number\n");
    scanf("%d", &number);
    int sum = 0;
    int number1;
    int i=0;
    do{
        number1 = number % 10;
        sum = sum + number1;
        number = number / 10;
        i++;
    }while(i<5);
    
    printf("%d \n"sum);
    return 0;
}

Logic:

First we define Variable of  type int   that is number  and we take input of five digit number from user in this . Then we define another variable of type int  that is number1 and sum=0. we give sum as value zero. In number1 we assign value number%10 that means remainder remains  after dividing five digit number with 10. And then we add it in sum variable . And Then we divide number by 10 in order to get quotient and store in same variable number .This happens five times  and loop gets exit.

Output:

 You can give different input to this program and check by yourself. And please try by yourself  .

  I hope that you find this helpful in you work . 

Thanks for reading .









Multiplication Table of any number

 In this I will let You know how to create a multiplication table of   the number you want .So, without any time waste .What we are going to use here .We here use loop. Then question come what is loop for understanding let me give you example if you want to print 1000 natural numbers for that you are  not going to  print one by one in order to save time. For this we just create a loop condition after which it exit and then increase it one by one .

Code:

Multiplication table using While loop

#include<stdio.h>
//to print multiplication table of any number
int main(){
    
    int number;
    int i=1;//we here intilize it form one 
    printf("Enter Number for Multiplication table\n");
    scanf("%d",&number);
    while(i<=10){
        printf("%d X %d = %d\n",number,i,number*i);
        i++;
    }

    return 0;
}

Multiplication Table using for loop

#include<stdio.h>
//to print multiplication table of any number
int main(){
    
    int number;
    int i=1;//we here intilize it form one 
    printf("Enter Number for Multiplication table\n");
    scanf("%d",&number);
    for(i=1;i<=10;i++){
        printf("%d x %d = %d\n",number,i,number*i);
    }

    return 0;
}

Multiplication Table using do-while loop

#include<stdio.h>
//to print multiplication table of any number
int main(){
    
    int number;
    int i=1;//we here intilize it form one 
    printf("Enter Number for Multiplication table\n");
    scanf("%d",&number);
    do{
        printf("%d x %d = %d\n",number,i,number*i);
        i++;
    }while(i<=10);

    return 0;
}

Logic:

Now you can see here we first declare Variable of type int  number .And we take input form user in it and also define i initialize i it form 1. And increase value of i every time till i less than equal to  10. After this we just do multiplication of  given number from user by value of i. And it will print us multiplication table.

Output:


You can see here I give 12 as input  and program here give us multiplication table of  12. You can check out this on your own pc .I hope you find this helpful to  you in your work .

Thanks for reading