Monday 23 January 2017

Finding the power of a number from 0 up to its 10th power using a function in a C program (c programming examples)(c program examples)

Finding the power of a number from 0 up to its 10th power using a function in a C program 

The above program is an example which is written on the base of syntaxes of basic C programming, for loop and functions.


If you want to see this code with a perfect indentation, copy the code into "sublime text editor" and make sure that the type of code is set to 'c' at the bottom right of the window. After pasting the code press the command "ctrl+shift+P", you get a search box. Type "indentation" you get an option like this below the search box "Indentation: Reindent lines". Clink on that to get the code with indentation. 


The program is as follows:

#include <stdio.h>
int power(int base, int expo)
{
int product=1;
for(expo=expo;expo>0;expo--)
product=product*base;
return product;
}
int main()
{
int i,a;
printf("enter the number to find its powers upto 10\n");
scanf("%d",&a);
for(i=0;i<10;i++)
printf("%d %10d\n",i,power(a,i));
return 0;
}


The output is as follows:







Entire detail of each step of the program is as follows:

---------------------------------------------------------------------------------
#include <stdio.h>

     In C programming there are header files. Header files are the set of functions written in the C directory to perform certain activities in the code. The '#'  symbol denotes that it isn't a code line. The compiler includes the functions written in that header file. The 'stdio.h' means standard input and output. The functions 'printf' & 'scanf' are included in the process of compilation of the code. After including the 'stdio.h' header file the computer knows to scan the input given by the user by using a function in that header file and printing the output of the result after the compilation by using printf function in the header file.

Here are some of the functions in 'stdio.h' 

     printf(), scanf(), getc(), gets(), getchar(), puts(), putchar()

 In our program we use 'printf()'  and 'scanf()' functions

     printf():- This function is used to print the output.
     scanf():- This function is used to read the input given by user according to the declaration of a type of variable in the code.
--------------------------------------------------------------------------


int power(int base, int expo)
{
int product=1;
for(expo=expo;expo>0;expo--)
product=product*base;
return product;
}

     This is a function and we named it as power. The two arguments represent the type of variables which is taken from the main() function and they names by base and expo.

     The symbol '{' denotes the begin of the body.  The function should return a value and according to the type of variable it returns, we name that type before the function name as 'int power'. 

     'int product=1' this line declares a new variable named as product and assign its value as '1'.

     The next line indicates a loop. Loop is a kind of operation repeated several times as long as the condition is satisfied. 'For loop' contains (declaration;condition;increment or decrement)
Firstly the value expo is taken from the main() function. We do not change the value so is written as expo=expo. Then condition is checked whether the body of loop execute or not. The loop is executed up to the value of 'expo=1'. When expo gets the value 0, the loop terminates and it will enter into the next line of code for execution. After every iteration expo value is decreased by '1'.

      product=product*base;
   
   This line is called logic to get the desired output for a given input. For every iteration, the value of 'product' is multiplied by 'base' and 'product' is assigned the new value. 


     return product;
   
     This line returns the value of the product to main() function.

--------------------------------------------------------------------------
int main()
{
int i,a;
printf("enter the number to find its powers upto 10\n");
scanf("%d",&a);
for(i=0;i<10;i++)
printf("%d %10d\n",i,power(a,i));
return 0;
}

  
      In C programming we use main() function to represent it as the main of the program. The word 'int' defines the variable as an integer type. And those variables take the integer values only.

     Printf statement prints the line "enter the number to find its powers up to 10" and then it waits for the user to enter a value. After entering the value by the user the scanf() function assigns the given value into the variable 'a'  then the execution process enters into the next line.Here again 'for loop' is used to get a series of values as long as the condition is satisfied. In the body of the loop "printf("%d %10d\n",i,power(a,i));" calls the power function by sending the variable values 'a' & 'i' and it gets the returned value from the power function. 
C is a general-purpose programming language. It has been closely associated with the UNIX system where is was developed, since both the system and most of the programs that run on it are written in C. The language, however, is not tied to any one operating system or machine; and although it has been called a “system programming language” because it is useful for writing compilers and operating systems, it has been used equally well to write major programs in many different domains.(c programming examples)(c program examples)
Many of the important ideas of C stem from the language BCPL, developed by Martin Richards. The influence of BCPL on C proceeded indirectly through the language B, which was written by Ken Thompson in 1970 for the first UNIX system on the DEC PDP-7.(c programming examples)
BCPL and B are “typeless” languages. By contrast, C provides a variety of data types. The fundamental types are characters, and integers and floating point numbers of several sizes. In addition, there is a hierarchy of derived data types created with pointers, arrays, structures, and unions. Expressions are formed from operators and operands; any expression, including an assignment or a function call, can be a statement. Pointers provide for machine-independent address arithmetic.(c programming examples)(c program examples)
C provides the fundamental control-flow constructions required for well-structured programs: statement grouping, decision making (if-else), selecting one of a set of possible cases (switch), looping with the termination test at the top (while, for) or at the bottom (do), and early loop exit (break).(c programming examples)(c program examples)
Functions may return values of basic types, structures, unions, or pointers. Any function may be called recursively. Local variables are typically “automatic,” or created anew with each invocation. Function definitions may not be nested but variables may be declared in a block-structured fashion. The functions of a C program may exist I separate source files that are compiled separately. Variables may be internal to a function, external but know only within a single source file, or visible to the entire program.(c programming examples)(c program examples)
A preprocessing step performs macro substitution on program text, inclusion of other source files, conditional compilation.(c programming examples)(c program examples)
C  is a relatively “low level” language. This characterization is not pejorative; it simply means that C deals with the same sort of object that most computers do, namely characters, numbers, and addresses. These may be combined and moved about with the arithmetic and logical operators implemented by real machines.(c programming examples)(c program examples)
C provides no operations to deal directly with composite objects such as character strings, sets, lists, or arrays. There are no operations that manipulate an entire array or string, although structures may be copied as a unit. The language does not define any storage allocation facility other than static definition and the stack discipline provided by the local variables of functions; there is n heap or garbage collection. Finally, C itself provides no input/output facilities; there are no READ or WRITE statements, and no built-in file access methods. All of these higher-level mechanisms must be provided by explicitly called functions. Most C implementations have included a reasonably standard collection of such functions.(c programming examples)(c program examples)
    
Printing a Vertical Histogram of words in a string representing the size of each word using an example in C program

1 comment :