Powered By Blogger

Tuesday, 17 March 2020

C Programme to Reverse a Number using While Loop And Arrray functions


Print numbers in Reverse Order In ‘C’ programming using While Loop & Array functions.............


Programming Code for printing Reverse Number in 'C
Reverse number printing in 'C'



Welcome viewers to my new blog on how to print numbers in reverse order in ‘C’ programme. Today we will deal with this interesting programme and coding with ‘C’. This is also a simple programme in ‘C’ but it needs some understanding in the compilation of the programme. So, without wasting time lets start the programme.

In this programme, we will use the “Modulus(%)” function to some extent. The property of the function will be described in its respective position.

  • First, we are coming to the variable part :
We have taken three variable for this programme i.e num,num1 and rev.
‘num’ denotes ‘number’ which we will take the input from the user.
‘num1’ is another variable to apply a modulus function,. We will see its use in the programming part.
‘rev’ denotes ‘reverse’ which saves the Reverse Value of the given number.

  • Second, The programming part :

Before starting let's talk about the ‘Modulus’ function in ‘C’ briefly.
‘Modulus’ function is nothing but the remainder of a division process.
Let's take an example:
If we take a number – 1234 and divide it with 10.
Then, 1234%10==4 that is the remainder of the division.
If we divide 542 with 5, then 542%5==2.
i.e. in programming  int x;
                                     x=542%5;
                                     printf(“%d”,x);
              Output:   2

Here, we have used a ‘while loop’ to perform this programme. It is given with a condition “num!=0”.
It means until the value of ‘num’ become equals to zero, the loop iterates.

Inside the loop, we have given an argument  “num1=num%10”  It means after the calculation of num%10, the value will be stored as num1 and each time the while loop iterates the value of the variable ‘num’ changes according to the argument given inside the loop.  This argument is given because we are trying to print the given number into reverse order, so we use modulus function it gives us the remainder of the division and thus giving us the last most digit of the number as we are the dividing the number with 10. It is discussed clearly in the next argument section.

Next, argument is “num=num/10”. It should be noted that the variable ‘num’ is taken as an integer value. So, after diving a number with 10 it will save only the integer value, not floating-point value.
If we take a number – 5421
Then 5421/10==542(integer value) not 542.1(floating point value).
This condition plays a vital role in the ‘while loop’ iteration and also in performing the previous argument.
Before going to the third argument part, we will take some examples of how the above mentioned two arguments are working in this programme.
If we take a number- 1347. So, num=1347
Then,
1st argument: “num1=num%10”
So, num1=1347%10==7. That means we are getting the last most digit of the given number by the ‘Modulus function’.
Moving to the next argument
2nd argument: “num=num/10”
So, num=1347/10==134 (the only integer value is stored as num)
After completing of the first loop iteration, the while condition is checked i.e ‘num!=0’  (‘!’- not equal to), if the condition is true the loop iterates otherwise it gets out of the loop.
For the taken example loop will iterate a total of 4 times.
For 2nd iteration: The present value of ‘num’=134
1st argument: num1=134%10==4. That means we are getting the second digit from the backside of the number.
2nd argument: num=134/10==13 (not 13.4)
For 3rd iteration: The present value of ‘num’=13
1st argument: num1=13%10==3. That means we are getting the third digit from the backside of the number.
2nd argument: num=13/10==1
For 4th and the last iteration: The present value of ‘num’=1
1st argument: num1=1%10==1. That means we are getting the fourth digit from the backside of the number or the first digit of the number.
2nd argument: num=1/10==0

And after the completion of 4th iteration the value of ‘num’==0, so ‘while loop’ condition is violated and thus the iteration of loop stops.
Now, coming to the most important the 3rd argument. It is basically doing the job making the number in reverse order. Let's see the argument

rev=num1+(rev*10)
For 1st  iteration of ‘while loop’ :  num=1347 , num1=7
So, rev=7+(0*10) . In the variable declaration section we have taken the initial value of ‘rev’=0.
rev==7
For 2nd  iteration of ‘while loop’ :  num=134 , num1=4 ,rev=7
rev=4+(7*10)==74
For 3rd  iteration of ‘while loop’ :  num=13 , num1=3 ,rev=74
rev=3+(74*10)==743
For 4th and the last iteration of ‘while loop’ :  num=1 , num1=1 ,rev=743
rev=1+(743*10)==7431
Then the value of num becomes zero as 1/10==0( again remember num is taken as an integer value)
‘while loop’ condition now becomes false and it stops to iterate.

Thus, the final value of ‘rev’ becomes 7431 i.e the reverse order of the given number 1347.
This is how the whole process proceeds. The entire programming code is given below:

C programme to reverse a number using while loop
Programming Code for printing Reverse Number in 'C'




This programme can be done with the use of ‘ARRAY’. Stay tuned for it. Hope, you understand the explanation and the programme. Please, share and comment if this helps you. If any question, please comment below. Thank you for reading.......

Sunday, 15 March 2020

Pyramid printing pattern in 'C' programming with ALPHABETS


Hello, viewers welcome to my new blog on pattern printing in ‘C’ language with alphabets. So, let's start.........

Before going into the coding part let us have a look at the pattern that we want to print in ‘C’.


printing pattern in 'C' programming
Printing Pattern of Pyramid with Alphabets

So, today we will print the above pattern in ‘C’ and as we can see that the printing pattern is based on alphabetical order, so we need some knowledge about ASCII values. We will discuss it meanwhile.

We can see the printing pattern is formed of alphabets but there’s an interesting thing happens that in every line or rows the alphabets become printed in their sequential order but after the printing of alphabet in the middle part of a line, the alphabets then started to print in opposite order i.e. back counting order. For example, if we take the line or row no:3, then we can see that up to the middle of the line, alphabets are printed in their sequential order i.e ABC. But when we proceed further, it is seen that alphabets have become started to print in opposite sequence i.e. previous-ABC + now- BA (ABCBA). So, this is an important part of this printing pattern. 

Now, let's see what is ASCII values?

In simple words, it is nothing but the converted integer values of character, strings or some special character by which compiler understands the respective character or string etc.
For upper case letters , ASCII value starts from 65 to 90 (A==65 ,B==66,C==67 to Z==90) and for lower case letters, ASCII values starts from 97 to 122 (a==97,b==98,c==99 to z==122).
It will be more clear if we take the following example.....
if we write a print function: int x=65;
                                                    printf(“%d”,x);
then the output will be: 65
but if we change the format specifier from %d to %c, it will print its corresponding ASCII character.
i.e.         printf(“%c”,x);
output: A                          
Hope you understand. Now, lets take a look at our programming part.

  1. Outer "For Loop":-We have used here a total of 4 ‘for loops’ (1 outer and 3 inner ‘for loops’). For first ‘for loop’ we have made a condition that will make the loop iterate for ‘n’ times which means it will print ‘n’ number of lines or rows according to the input of the user.            int i,j,k,n;
    printf("enter the value of n: ");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
  2. Nested "For Loop":-
  • First 'Inner Loop':   
           Then, the first inner’ for loop’ begins with a condition of printing spaces up to (n-i) times for each iteration of the outer loop. So, when the value of i=1,n=4 the loop will print (n-i)=(4-1)=3 spaces for first row or line,when i=2,n=4 the loop will print 4-2=2 spaces for second-row or line and so on.

for(j=1;j<=n-i;j++)

{
printf(" ");
}
  • Second 'Inner Loop':  
         For the second inner ‘for loop’, there’s the condition for alphabet printing in sequential order up to ‘i’ times for each time the outer loop iterates. When the value of i=1, it prints only one alphabet(A), when i=2, it prints two alphabets(AB), when i=3 it prints three alphabets (ABC) in sequential order and so on. Thus, the second inner ‘for loop’ completes its iteration.


for(k=1;k<=i;k++)
{
    printf("%c",k+64);
}
  • Third 'Inner Loop':
          For the third inner ‘for loop’, we need to remember after the iteration of 2nd inner for loop, the 3rd inner loop immediately starts to print alphabet in opposite order i.e.(if the 2nd loop prints “ABCD” then the 3rd loop will print “CBA”). So, the condition is given when k=i-1 and k>0. We can see, when i=1,k becomes k=(i-1)=1-1=0, so it will not print any alphabet, when i=2, k=2-1=1, so it will print one alphabet(i.e. A), when i=3, k=3-1=2, so it will print two alphabets(i.e. BA) but in the opposite order, as 'k' is given as a decrement operator(k--) and check the condition until K>0. 


for(k=i-1;k>0;k--)
{
printf("%c",k+64);
}

Thus, the entire “Nested For Loop” goes on.

NOTE: In the print section, we have added 64 with the value of ‘k’ to get the ASCII values of upper case alphabet(when k=1,k+64=65==A; when k=2,k+64=66==B). If we want to get lower case alphabet then, we need to 96 with ‘k’ as lower case letters start from ASCII values of 97.

Thus, the entire Programming Code is as follows:



Hope you understand the printing pattern and programming code. Please share and comment if this helps you. Thank you very much.

Thursday, 12 March 2020

Different types of Pattern and Tree structure in 'C' programming (Making A "Pine Tree")


Welcome viewers  to another new ‘C’ structure or pattern ........
In this blog, we will print a "
Pine tree" with the help of ‘for loop’,’ while loop and some ‘if-else’ function. So, let's start...
First, let's look at the pattern of the tree.
TREE STRUCTURE IN 'C' PROGRAMMING
TREE STRUCTURE IN 'C' PROGRAMMING

We can see the tree has three same types of ‘Head part’ i.e upper part. And then followed by the five stared ‘Stock part’ and at the base of it, there is also five “=” character.
So, we need to first with the ‘head part’. As this part continues for three times, so we can make a ‘while loop’ with a condition of looping three times and printing the same pattern.
For ‘Stock part’ we need to use a simple  ‘for  loop’ that will print a ‘*’ with newline character at the time of each star printing.
For ‘Base’ there is also a simple ‘for loop’ that will iterate 5 times to print the “=” characters.
Now, let's see how the program is going on :
PROGRAMMING CODE IN 'C' TO PRINT A TREE PATTERN
PROGRAMMING CODE IN 'C'

(i)”HEAD PART” or “UPPER PART”:- 

                                                                Here, we have first set a while loop that will iterate 3 times as stated earlier. Then we have made a for loop whose value will start from 1 and it will check the condition i<=n(here,n=3) which means the loop will iterate until the loop is executed 3 times. Then an inner is set with a condition j<=n-i(for first iteration n=3 and i=1, for second i=2, third i=3 and so on) for printing the spaces before printing the stars to give the proper shape of a tree. Here, the loop will print spaces until j<=n-i (for the first case it prints 2 spaces as j=3-1=2; for the second case it prints 1 space as j=3-2=1; for third, it will print any space as j=3-3=0).

Then the next ‘for loop’ comes for the printing of stars according to the pattern. Here the condition will be different i.e. k<=(2*i-1) as we need to print stars in ‘odd order’. For first case i=1;k=1; it prints only one star (it should be noted, the spaces are printing at their respective positions before printing stars as per previous ‘for loop’), for second case i=2;k=3, prints 3 stars and for third case i=3;k=5 prints 5 stars.
Now, as the while loop is given so the same will iterate or execute 3 times and thus printing the “head part” of the ‘TREE’.

while(x<=3)
{
    for(i=1;i<=n;i++)
    {
    for(j=1;j<=n-i;j++)
    {
    printf(" ");
    }
    for(k=1;k<=2*i-1;k++)
    {
        printf("*");
    }
  printf("\n");
    }
  x++;
    }

(ii)”STOCK PART”:-
                                  It is a simple part. Here we have for loop with a ‘if-else’ function. The loop is executed a total of 10 times(5 times for the stock part and the rest for the base part).
"If statement" is given to check the condition that the loop is iterated less than 6 times for ‘stock printing’ and inside if statement there is another for loop that will print spaces and then stars with a new line the character at the time of each iteration.
Thus, the ‘stock part’ is printed.

for(i=1;i<=10;i++)
   {
  if(i<6)
  {
     for(j=1;j<=2;j++)
     {
     printf(" ");
     }
     printf("*\n");


(iii)”BASE PART”:-
                                In the previous ‘if statement’ we get the ‘else part’ also, so it is basically for base part printing(“=”). The for loop will execute another 5 times thus completing its total 10 iterations (5 in ‘if’+5 in ‘else’) and printing the’=’ characters . Thus printing the base part.

 for(i=1;i<=10;i++)
   {
  if(i<6)
  {
     for(j=1;j<=2;j++)
     {
     printf(" ");
     }
     printf("*\n");
      }
      else
        printf("=");
   }
}


This is all about printing a “TREE” using ‘C’ Programming. It can also be done in another way (stay tuned). Thank you..............


Wednesday, 11 March 2020

Let's Learn some Pyramidal structures using 'C' programming (i.e. printing number pyramid with '*')

Hello viewers, your heartiest welcome to my blog.
Today in this blog I am going to discuss a number pyramid structure using 'c' language.
I think you all programmers or beginners have already known the simple '*' triangle printing, don't worry if it is not well understood by you. I will try my best to make you habituated with all these things.
So, let's start.
Before going into the complex structure, we first try to understand the simple triangle in short.
In these cases of star printing we need to use for loop or while loop etc. because we need to do the same work for many times (i.e. printing '*','**','****' etc), so it is better to use loop functions. One may think about printing stars in increasing or decreasing order as given in the question by using printf function with a new line character(\n) without any looping. But when you will face a situation of printing more than 10 or 20 or 30 or >30 lines pyramid, it will not be easy to print the pyramid with only 'printf ' function and moreover, it will be a time-consuming process also. So, while printing 'star(*)' pyramid we should use some loop functions, most of the cases it is "for loop". Hope you understand why we use loop function in various structure printing in 'C'.
Let's see in the following:
You are asked to print a star triangle with one star incrementing and having five lines in the pyramid.
You might have done it earlier, for this structure we first check how many lines (rows) are there in the pyramid. It has 5 lines so, we should set a loop that will iterate(execute) 5 times,so we set a loop which will start from 1 and execute total of 5 times. That's not all, we also need to print stars according to increasing order. So, we must think with the iteration of the for loop, there should be another loop that print stars, add a new line and increment stars at their respective positions.
We must be careful about that the second loop should be inside the first 'for loop' and this type of looping is known as "Nested For Loop". Otherwise, star printing will not be in correct order.
Let's see the programming:

We have taken first for loop that will iterate 5 times, but inside it there's another loop. let's look inside the second loop. Here in the condition checking j<=i that mean when the value of i=1,j<=1, so it will print only one '*', then it goes back to first loop again. At this time the value of i=2 and then it enters in the inner loop and it checks condition j<=2(as the present value of i=2), so the inner loop will iterate two times printing 2 stars('**'). Then control goes to first loop again and i becomes i=3,then inner loop j<=3(as present value of i=3),so loop iterate 3 times printing 3stars('***'). And this process goes on until i=5.
It should be noted that every time after printing stars control goes to the next statement that is "new line character"(\n) and prints a new line each time after the completion of the inner loop. This way the whole process goes on.
There's a short program summary


#include<stdio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++)           /*Outer for loop iterate 5 times*/
{
for(j=1;j<=i;j++)  /*inner 'for loop' iterate 'i' times for every iteration of outer 'for loop'*/
{
printf("*");  /*printing '*' */
}                             /* end point of inner for loop'*/
printf("\n");        /*printing new line character*/
}                                    /*end point of outer for loop*/
}

Thank you.

Can we use pointers without specifying the data type of the pointer?

Void Pointer Yes, we can use pointers without specifying the data type of the pointers. But remember at the end of the programming, wh...