Powered By Blogger
Showing posts with label Coding with 'C'. Show all posts
Showing posts with label Coding with 'C'. Show all posts

Wednesday, 31 March 2021

How to Print Fibonacci Series in 'C' Programming

Welcome viewers to my new blog. Today we will see how to get FIBONACCI SERIES using ‘C’ programming.







It is very easy programming to print ‘Fibonacci Series’ using ‘C’ programming.

First, let's see what is ‘Fibonacci Series’ ?


‘Fibonacci Series’ is a series of numbers in the way that each number is the sum of two preceding numbers.

For example:  a series-  0,1,1,2,3,5,8,13,21,.............

Its a Fibonacci series as we can see, 

the number ‘1’ comes after adding its two precrding numbers (i.e  0,1,1,......  ;  0+1=1),then the number ‘2’ comes after adding its two precrding numbers (i.e  0,1,1,2,......  ;  1+1=2),then we get the number ‘3’ by the same way(i.e   0,1,1,2,3,........;  1+2=3), 
by that way we get the following numbers-

‘5’:  0,1,1,2,3,5,...........;   2+3=5
‘8’:  0,1,1,2,3,5,8,..............;   3+5=8
‘12’:  0,1,1,2,3,5,8,13,............;   5+8=13

This is how the whole ‘Fibonacci Series’ proceed.

Now, let's move to our programming part:-



    1)   VARIABLE DECLARATION PART:  In this part, we have taken the following variables ,all of integer type.

int  ab=0c=1n;

Here, we have initialized the value of b=0 and c=1 in the declaration section. Its use will be discussed in the main programming part.

   2)  GETTING INPUT FROM THE USER:  Here is used a printf function and a scanf function to get the input from the user to know upto how many numbers, he/she wants to get the ‘Fibonacci Series’.

printf("Enter the value of 'n' upto which you want to calculate 'Fibonacci Series': ");
scanf("%d",&n);

1 3)  LOOPING AS WELL AS ARGUMENT AND MAIN CODING PART:  The coding part is very important for getting any ‘Fibonacci Series’.

                           
Here, we have used a ‘while loop’ that will iterate until the value of ‘n’(input from the user) becomes zero(0).

Now, we have given an argument as follows and this is the most important part of that programming:
                                    printf("\n\nThe 'Fibonacci Series' is as follows :-\n");
                                    while(n>0)
                                    {
                                                a=b+c;
                                                c=b;
                                                b=a;
                                                printf("%4d",c);
                                                n--;
                                      }


We have taken the arguments:-   a=b+c;  c=b  and  b=a;
In the initialization part, the value of ‘b=0’ and ‘c=1’.

Let's see the whole loop goes on:-

Let, the user given the value of the variable ‘n’=8

                                 i.            When  n=8 ,b=0 , c=1
a=b+c=0+1=1   So, a=1
Now,  Next Argument:-
c=b , current value of b=0,  So, c=0
b=a ,current value of a=1,  So,b=1

Output:-    0

                               ii.            Now,  n=7(there’s an decrement operator present  i.e  n--) , b=1 , c=0
a=b+c=1+0=1   So, a=1
Now,  Next Argument:-
c=b , current value of b=1,  So, c=1
b=a ,current value of a=1,  So,b=1

Output:-    0  1

                              iii.            Now,  n=6 , b=1 , c=1
a=b+c=1+1=1   So, a=2
Now,  Next Argument:-
c=b , current value of b=1,  So, c=1
b=a ,current value of a=2,  So,b=2

Output:-    0  1  1

                             iv.            Now,  n=5 , b=2 , c=1
a=b+c=2+1=1   So, a=3
Now,  Next Argument:-
c=b , current value of b=2,  So, c=2
b=a ,current value of a=3,  So,b=3

Output:-    0  1  1  2

                               v.            Now,  n=4 , b=3 , c=2
a=b+c=3+2=5   So, a=5
Now,  Next Argument:-
c=b , current value of b=3,  So, c=3
b=a ,current value of a=2,  So,b=2

Output:-    0  1  1  2  3

...........................................

By this way, the whole process goes on and the final output screen becomes :
0   1   1   2   3   5   8  13

  Ø    The program’s argument section can be written in another way:-
printf("\n\nThe 'Fibonacci Series' is as follows :-\n");
                                    while(n>0)
                                    {
                                                a=b+c;
                                                b=c;
                                                c=a;
                                                printf("%4d",c);
                                                n--;
                                      }

  Ø  For this type, we need to initialize the value of  b=1  and  c=0.

  Ø  Trick to remember the code:  You first write blindly the argument,  a=b+c

then, Either you take  b=c  or  c=b
·         If you take  b=c,  then the next argument should be  c=a

Mind by this arrow way:   b=c
                                          
                                         c=a    ⇒ ‘a’ always be in this position

You need the initialize the value of  c=0(on which arrow mark is placed)  and  b=1

·         If you take c=b,  then the next argument should be  b=a
Mind by this arrow way:   c=b
                                           
                                         b=a     ⇒ ‘a’ always be in this position
                                                         
You need the initialize the value of  c=1 and  b=0(on which arrow mark is placed) 


You can remember the series in that way. Hope this helps you. Please share and comment. Thank you for reading.





Sunday, 22 March 2020

EVALUATION OF AN EQUATION AND SUM USING ‘GOTO’ AND ‘POW’ FUNCTION IN ‘C’ PROGRAMMING...


EVALUATION OF THE FOLLOWING EQUATION AND SUM USING ‘GOTO’ AND ‘POW’ FUNCTION IN ‘C’ PROGRAMMING...

EQUATION FOR EVALUATION OF SUM IN 'C'
EQUATION FOR EVALUATION OF SUM IN 'C'





SUM= (1+X+X2+X3+X4+..........+Xn)

Today in this we are going to discuss the above equation and to evaluate the Sum by using ‘goto’ function and ‘pow’ function in ‘C’ programming.
So, let's start...............
Before starting let's see the programming code used here.......

PROGRAMMING CODE IN 'C' PROGRAMMING
PROGRAMMING CODE IN 'C' PROGRAMMING





INITIALIZATION PART: here, we have taken ‘sum’ and ‘x’ as float value.
Then we have taken two variables ‘n’ and ‘i’ as an integer value. Lastly, a character variable has been taken as ‘choice’.
This is the initialization part, we will see the use of the above variables in the following parts.


float sum,x;

int n,i;
char choice;

Let's move to the argument making part.

ARGUMENT MAKING  AND MAIN CODING PART:  In this part, we will see the use of ‘goto’ and ‘pow’ function. Lets what is this thing and how they work?

“GOTO” FUNCTION:  The function is already included in stdio.h header file. This function mainly works to move to the control to a position of the programme where it is set to move. That means it can move the control of the execution in any part of the program code.

For example: if we write a program
                      int n,x;
                     input: printf(“Enter the value of ‘n’”);
                     scanf(“%d”,&n);
                     if(n%2==0)
                         {           
                                printf( “The number can be divided by 2”);
                          }
                      else
                         {
                               printf(“The number can’t be divided by 2”);
                          }
                      printf(“Do you want to continue? press 0 or 1”);
                     if(x==1)
                      {
                           goto input;
                       }
                       else

                       printf(“Thank you........”);



By the above program, we can decide that the number given by the user can be divided by 2 or not.  Then we are taking a decision from the user whether he wants to continue or repeat the program again or not. So, we need here the ‘GOTO’ function. For this, we need to specify a location in the program which tells the compiler to repeat the execution of the program once more or again and again according to the input from the user. So, we have marked the location as “input”. Remember after specifying the location and taking the variable, there should be a colon after the declaration of the location i.e (input:) and the code format will be “goto(space) (name as given in the location)”.

In the given example if the user gives input ‘1’ then the control goes to the “input” named location.
Thus, repeating the execution once more.

Now, What is “pow”?
‘POW’ denotes power. It is a math function included in “math.h” header file. So, before using ‘pow’ function we must include the ‘math.h’ header file in the header section. The function mainly helps to make the power of a number.
It is as the following format:



                                                          pow((number),(power))



for example:  pow(2,4)==24==16   or  pow(3.5,2)==3.52==12.25
That how the ‘POW” function works.


let's move to our coding section.
we have used here a ‘for loop’ to evaluate the value of the sum.

        sum=1;

for(i=1;i<=n;i++)
{
sum=sum+pow(x,i);
}


As seen, inside ‘for loop’ the argument  is  “sum=sum+pow(x, i)”
taken sum=1  and ‘for loop’ starts with i=1 up to i<=n.

If user gives the input x=4 and n=3 then,
So, when i=1
sum=1+pow(4,1)==5

Now, the present value of ‘sum’=65
when i=2
sum=5+pow(4,2)==21

Now,the present value of ‘sum’=21
when i=3
sum=21+pow(4,3)=85

So, the evaluated ‘sum’ is equal to 85.

This way the argument works with ‘pow’ function by the argument  “sum=sum+pow(x,i)”

Now, the ‘for loop’ ends.

Next, we are moving towards the user’s decision making section where he/she chooses whether he wants to repeat the program once more or not.


printf("\n\nDo you want to continue ?\nPress y or n.....\n");
fflush(stdin);                  /*it is used to make the scanf function to get input from user without skipping*/
scanf("%c",&choice);            /* or a space before %c can also do the same*/
if(choice=='y'||choice=='Y')
{
    printf("\n\n");
goto input;
}
else
    printf("\n\nThank You......");



Here, we are taking input from the user in the form of “Y/y or N/n”.
NOTE: We have used a function “fflush(stdin)” to stay the input from the user at the time of asking from the user. It can also be achieved by giving a (space) before the formatting string “%c” i.e. “(space)%c”. Without it, the input from the user can’t be taken, it will skip the respective ‘scanf’ part, thus not getting any input from the user. You can try it and see what is happening without it.
Then, we have taken an ‘if’ function with the condition      
                 if(choice==’Y’||choice==’y’)
As the input from the user is saved in the variable ‘choice’ , so we have given this condition. Here the sign ‘||’ stands for “OR” meaning and remember the sign ‘&&’ stands for “AND” meaning. If the user enters capital ‘Y’ or(||)  small ‘y’ then, as per ‘GOTO’ function control moves to ‘input’ location in the program. Otherwise, the program ends with printing “Thank you......”


Hope, the given example is understandable to you. If any doubt persists please comment below and if this helps you please share the blog. Thank you so much............. 

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.......

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...