-->

Friday, January 8, 2016

Program x to power y in C Language

Introduction 

The meaning of x to the power of y is , a variable x is multiply for y time. Lets take a simple example a variable x is hold 2 and y hold 5. Now the answer is 2 multiply for 5 times such as..
z=2*2*2*2*2;
z=32

Method-1: 

Design a Algorithm in c

Step-1 : Take two value in variable x and y by user input.
Step-2 :  Use pow function in  "C" , which is available in math.h header file.
Step-3 :  Take another variable Z for holding answer of the x to the power of y.
Step-4 : Print z 
Step-5 : End of the program.

Program :

#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
int x,y,z;
printf("enter X value");
scanf("%d",&x);
printf("Enter Y value");
scanf("%d",&y);
z=pow(x,y);
printf("output of the x power y is%d",z);
getch();
}

Output
Program x to power y in C


Method-2 : Using recursion

Algorithm of the program

Step-1: Step-1 : Take two value in variable x and y by user input.
Step-2: Create a function  and pass values of x and y  to the function.
Step-3 : Call same function for y times 
Step-4 : Return function 

Program

#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
int x,y,z;
clrscr();
printf("enter X value");
scanf("%d",&x);
printf("Enter Y value");
scanf("%d",&y);
z=fun(x,y);
printf("output of the x power y is%d",z);
getch();
}
int fun(int a,int b)
{
if(b>=1)
{
return a*fun(a,--b);
}
else
return 1;
}




Output
Program x to power y in C

Method -3:Using third variable

Algorithm:
Step-1: Take two value in variable x and y by user input.
Step-2: Run for loop for y times.
Step-3 : Take third variable z with 1 value
Step-4 : Multiply x with z for y times in for loop
Step-5 : Take output value in z variable
Step-6 :  End of the program

Program


#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
int x,y,i,z=1;
clrscr();
printf("enter X value");
scanf("%d",&x);
printf("Enter Y value");
scanf("%d",&y);
for(i=0;i<y;i++)
{
z=x*z;
}
printf("output of the x power y is%d",z);
getch();
}

Output

Program x to power y in C

Structure of a C program

The structure of a C program is nothing but the way of framing the group of statements while writing a C program. We put the general structure of a C program first as shown below:

[preprocessor directives]
[global declarations]

returning_type main( )
{
[Declaration Section (local)]

[Executable Section]
}

[User defined functions]

Note : The bold faced characters such as main( ) in one line along with the left parenthesis '(' and the right parenthesis ')' should be typed as they are. The Declaration section and Executable section enclosed within '{' and '}' is called the body of the main function. The data or value returned by the function main( ) is indicated by' returning_type'. Normally main( ) doesn't return any value. So, the returning type void is used. The function main( ) can be preceded by global declarations and preprocessor directives.





Preprocessor directives : The preprocessor statements starts with '#' symbol. These statements instruct the compiler to include the specified file in the beginning of the program. One important point about preprocessor directives is that these statements are never terminated by ';' for example,


#include<stdio.h>  /* Only one file is permitted for one #include */
#include<math.h>


are the files that the compiler includes into the user program. These two files "stdio.h" and "math.h" are called header files and hence the extension '.h' . Using the preprocessor directive the user can define the constant. For example,

#define SIZE 100 /*Only one constant can be defined using one #define */
#define N 50
Here , Size and N are called symbolic constants. Their value is not changed during program execution.

Global declarations: The variables that are declared before all the functions are called global variables. All the functions can access these variables. By default the global variables are initialized with '0'.
main( ) : Each and every C program should have a function main( ) and there should be one and only one function by name 'main( )' . This function is always executable first. The function main( ) may call other functions. The statements enclosed within left and right curly braces are called body of the function main( ).

Declaration section : The local variables that are to be used in the function main( ) should be declared in the declaration section. The variables declared are identified as identifiers by the C compiler. The variables can also be initialized. For example, consider the declarations shown below:


int sum=0 ; /* The declared variables is initialized to 0 */
int a;           /* The declared variable contains garbage(unwanted) value */
float b,c,d;  /* More than one variables can be declared by single statements */

Executable section : This section contains the building blocks of the program. The blocks containing executable statements represent the directions given to the processor to perform the task. A statements may represent an expression to be evaluated, input/output operation , assignment operation, etc. The statements may be even control statements such as if statements , for statement , while statement,do-while statement,etc. Each executable statement of a block should end with a ';' .The symbol ';' is also called as statement terminated or separator.

User defined functions: This is the last optional section of the program structure. The functions written by the user to perform particular or special task are called defined functions. These are called as sub-programs. the basic structure of the user defined function resembles that of the function main( ).

Now, let us write a small program to display the message " dotprogramming":
#include <stdio.h>
main( )
{
printf("dotprogramming");
}

Principles of Programming in C language

Introduction to Programming
Remember your last visit to the super market. You might have purchased many items. What did you do after picking all the items and putting them into the carriage? Probably you met with the billing clerk to make the payment. Have you observed his way of preparing the bill? He picks the items one by one and enters something into the computer repeating the same task for all the items.
Within a count of few minutes or even may be within few seconds he gives the bill , you pay and come out carrying all the required things. So what made him to process the bill so fast? It is nothing but the “program” running in the computer’s memory. If you want to become a billing clerk, then what is needed is to just learn the method to use the program that helps in the billing the items.
Stop, don’t think in that way. You should dream something high! To design a program that helps the billing clerks to prepare the bill fast and in a most efficient way.

Program Concept
By and large computers are used either to run the designed programs or to design the program itself. As an upcoming programmer you are going to use the computer to design the programs. That should be your main dream and aim. Keeping that in mind, let us see “what is program?”
Definition:  A program, strictly, a computer program is a collection of coded instructions to direct a computer to perform a desired set of operations. So, coding the instructions to make a program is an art. This art of making or designing a program is called programming. The person who writes such programs is called programmer.

Another definition of program:

Algorithm + Data Structure = Program

Definition : A program can be defined as the combination or clubbing of algorithm and data structure together into single unit. Here the algorithm refers to the procedure containing primitive steps to solve a particular task. A primitive step implies an easily understandable one. Data structure refer to the modeling of the required data to solve the task.

A programmer can design the program as per the customer's needs. The customer's need is collected as problem . The problem is analyzed to arrived at a solution. This solution provided using certain tools is called program. Whenever a problem arises , the programmer can design a program. The designed program can also be used to solve some related problems with the little modification. Changes is the nature! The new demands, requirements , etc. rise as the time passes. The programmer should react properly to these and should come out with a new solution from the existing solutions. Such existing solutions along with the new solution frame a program library. Now, let us see " what is program library?"

Definition: A library , a store or collection of computer programs is called program library . Each and every program in the library is designed to solve a certain type of problem.

What is canned program?

Definition: A program in a library of computer programs is called canned program. Existence of libraries of programs that are easy to use and designed to solve very general problems is important. The Turbo C compiler, you are going to learn, is the best example that contains many canned programs.
Turbo C compiler container many small programs in the form of built-in-functions contained in header tiles. With the help of such canned programs you can become a very good programmer. So, you should practice using such canned programs. Such canned programs are designed and added to the library. The canned programs are established and maintained by various computer manufacturers and centers.
For example, you will use a function scanf ( ) to read the input for almost all the programs you write and learn throughout the learning of this course. Similarly, you will use printf ( ) to display the messages and values or data on the output screen. Using such library functions or canned programs you build your own programs to solve general purpose or system-oriented problems. These two functions are canned in a header file “stdio.h”.
The C language that you are learning, contains nearly 27 such header files to design a program. All the header files inclusively contain hundreds of built-in functions. The header file “stdio.h” contains 56 built-in functions to solve many problems related to input and output operations. The following list provides a rough idea of number of built-in functions available in C:
“stdio.h” - 56, “conio.h”- 29,
“stdlib.h” - 42, “string.h” - 37,
“math.h” - 30, “ctype.h” - 17, etc.

Wednesday, May 6, 2015

Demerits of bottom-up technique in c language

In my previous article i explained about:

  1. Features of top-down technique
  2. Merits of bottom-up technique
Now, today i will learn about Bottom up technique demerits:

  • The application developed using this technique cannot be tested as a whole before the development of main solution.
  • The sub-solutions are added or linked to the main solution without knowing the details of its coding.
  • The design of the sub-solutions are coded without the idea of their linking in the main solution.
  • The integration testing may cause complications because it is done in the later stage of application development.
You may also learn about : Top Down Technique

Wednesday, April 8, 2015

Unstructured technique of programming in C language

Definition : An approach of designing a program simply using series of statements is called unstructured programming.
In such programming technique every task is solved using series of simple statements only. Branching and repetitions are achieved through a 'goto; statement. Jumping to and out of a block of statements is achieved through 'goto' statement. So, a technique using 'goto' statement is called unstructured technique of programming.

Example:
Problem: Design a program to find factorial of a number
The program required is to find n!=1*2*3*4.....*(n-1)*n. If negative number is entered a proper message is to be displayed. So, the repetition and branching is achieved in unstructured programming technique using 'goto' statement. The statement 'goto' in C is used to take the control from one point to another irrespective of the location of transfer. It takes control to the specified label.

#include<stdio.h>
main( )
{
int n;
unsigned long int fact =1;
ReadAgain:     /* first label to transfer the control */
printf("Enter a number:");
scanf("%d", &n);
if(n<0)
{
printf("Negative number \n");
goto ReadAgain;    /* Control transferred */
}

Repeat:      /* second label to transfer the control  */
fact = fact*n;
n--;
if(n>=1) goto Repeat;  /* Control transferred to label Repeat */
printf(" The factorial is %lu", fact);
}

Features of unstructured technique

  • Simple statements are used to solve a task.
  • The statements used are basic and understandable.
  • This type of programming is called liner programming.
  • The approach is straight forward.
  • To branch from one point to other only a simple 'goto' statement is used.
  • Even to repeat a part of the program 'goto' statement is used.
  • Every logic of the program is developed using only 'goto' statement.
Merits of unstructured technique 
The unstructured technique of programming although having some merits is not at all entertained to design the programs. The merits to just list are:
  • The program designed is simple.
  • The logic is simple and straight forward.
  • Only one statement 'goto' is used for branching as well as looping.
Demerits of unstructured technique
The unstructured technique of programming is very rarely used. It is never entertained because of its demerits. The demerits of the unstructured technique are:
  • To follow the logic of the program is very difficult.
  • There may be abrupt transfer from one point to another.
  • This technique can be applied only to small-scale programs.
  • Difficult to keep track the logic of the program.
  • Excessive use of 'goto' statements causes confusion in program flow.
  • The quality of the program decreases as the number of 'goto' statements increases.
  • Program designed using this technique is not clear for the others and also at times to the designer of the program.
  • It is difficult for the programmer to understand the logic at the later stage.
  • Because of excessive use of 'goto' statements tracing the error is very difficult.
  • Testing of program consumes lots of time.
  • Redesigning a program is much more difficult.
  • Unstructured programs consist of statements that are not grouped for specific tasks.
  • The logic of such programs is messy with details and therefore difficult to follow.

Monday, March 9, 2015

Tips for program designing in C language

Now, let us see "What are the tips to design a good program?"
Although the program design involves several stages, the following are the few important tips to design a good program:

I Tip: Make a program readable. It is a combination of the following:

  1. Careful choice of data structure or data type.
  2. Careful choice of variable names.
  3. Generous use of remarks or comments.
  4. Program indentation.
  5. General program design


II Tip: Stepwise refinement of a solution

  1. Break a problem into a sequence of relatively self-contained or independent sub-problems.
  2. Follow orderly flow of stepwise refinement.


III Tip: Avoid 'goto' statement to jump around, forward and backward, into and out of loops or blocks of statements. The blocks are also called compound statements.

  1. The programs using 'goto' are difficult to read and debug.
  2. The programs using 'goto' are called unstructured programs. Always hate such unstructured programs.


© Copyright 2013 Computer Programming | All Right Reserved