Showing posts with label for_loop. Show all posts
Showing posts with label for_loop. Show all posts

How to remove any character from string in c

 In this I will tell you how to remove any character from the given String in C . So without any further delay let's get started .

Code:

#include<stdio.h>
#include<string.h>
//how to remove any character from string in c
int main(){
    char str[100];
printf("Enter the string \n");
    scanf("%[^\n]s",str);
  int j = 0;
    for(int i=0;str[i]!='\0';i++){
           if (str[i] == 'a')
        {
         str[j++]=str[i+1];  
         i++; 
        }
        else{
         str[j++]=str[i];
        }
        
    }
str[j]='\0';
    printf("%s  ",str);
    return 0;
}

Logic:

Now that we have see the code let's understand one by one . First of all we created a String of length 100 . And named it str . Then with the help of printf function we print user message to Enter the string . And then with help of scanf function  taking  String  as input. From the user .  And then we use a for loop and  run it till the end of the String. And we also declare variable j and initialize it from 0. For indexing of the same String after that character is removed . Then we take use   if-else  and in if condition we check for str[i]== 'a' if found then character after that is copied  in str[j++] else it copy before it is copied in str[j++]=str[i]  . And then after doing all this we end the String with '\0'.

Output:


And here you can see that our program work correctly and remove character 'a' from the String . Try this program by yourself and please provide me feedback by commenting your opinion . I hope that you find this helpful in your work. 

                                                              Thanks for reading  








Customer bill in C program

 In this post I will let you know how to make bill proper bill program for ration shop or any other store .     Made your own Algebraic Calculator, So, without any further delay let's get started .

Code:

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

int main()
{
    int  noi;          //noi is for number of items
    int  itemnum[100]; // it  is for number of items
    float price[100];   //price of one item
    float total = 0;
    char name[40];
    char date[10];
     char product_name[20][20];
    printf("Enter name of Customer \n");
    gets(name);
    printf("Enter date\n");
    gets(date);
    printf("Enter number of products purchased \n");
    scanf("%d",&noi);
    for (int i = 0; i < noi; i++)
    {
        printf("Enter  item number  \n");
        scanf("%d", &itemnum[i]); 
        printf("Enter name of product\n");
        scanf("%s",product_name[i]); 
        printf("Price of item first %d \n", i + 1);
        scanf("%f", &price[i]);
    }
   for(int i=0;i<noi;i++){
       total=total+price[i];
   }
   printf("Customer name:\t%s\tDate:\t%s\n",name,date);
   printf("Product name\tProduct number\tProduct price \n");
   for(int i=0;i<noi;i++){
       
        printf("%s\t\t",product_name[i]);
        printf("\t%d\t",itemnum[i]);
      printf("\t%f\t\n",price[i]);
   }
   printf("----------------------------------------\n");
   printf("Total price :\t%f\n",total);
   printf("Thanks for shopping\n");

    return 0;
}

Logic:

Here we first we declare  noi (number of items) of type int .Then we make one array of type float that is  price . And itemnum (item number) array of type int.And then we  declare two array of type char name (to get name of customer) ,and date (for which date bill is make ). And then we make two dimensional string of product_name (to take name of item purchased). Then using for loop we take input of three arrays namely itemnum , price, product_name. And it take input  till it is less than noi .As I told you above .Then we again use for loop to get total bill of customer . And in last for loop we print given value by user . Then total bill of customer . 

Output:


Here you can see output and I think you may 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 = 0; i < 5; i++)
    {
        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