-->

Thursday, February 12, 2015

Formatted Output Function Printf() in Turbo C Language

The data stored in the memory area can be displayed on the monitor or any other output device. This can be performed using printf() function in C. Using this function programmer can display numerical values such as integer, floating point number, character and strings.

Syntax
int printf(“format string”,var1,var2,var3,….varn);
where

  • “format string” consists of one or more format specifiers. Each format specifier start with % sign.
  • var1, var2, var3, ……, varn represent the values of variables in given series

Return Type value: The function will return an integer value. This integer value is the number of items that have been displayed successfully on the screen. But generally in our programs we will not use this return value.

The various format specifiers along with the meaning associated with them are shown below. These format specifiers are same as in earlier post. The following table shows the given values, for the statement to be executed and the output obtained on the screen:

Formatted Output Function Printf() in Turbo C Language

Field width specification for integers:

The printf() function uses the field width to know the number of columns used on the screen while printing a value.
The specification is provided by means of a format string like “%wk” where

  • w indicates total number of columns required to print the value or specifies the width.
  • k is the format given in the above table earlier for int.

The table below shows the given values, the statement and the output obtained on the screen:

Formatted Output Function Printf() in Turbo C Language

Field width specification for float:

The floating point or real number can be displayed using printf() statement with format specification. The syntax is shown below:
“%w.xf” where

  • w indicates total number of columns required to print the value.
  • x is the number of columns used after the decimal point.

The following table shows the given values, the statement to be executed and the output obtained on the screen:

Formatted Output Function Printf() in Turbo C Language

Field width specification for string:

The string (series of character) can be displayed using printf() statement with format specification. The syntax is shown below:
“%w.x” where

  • w indicates the total number of columns reserved to print the string.
  • x is the number of columns used to print the string in right justified way. The rest of the characters are ignored.

The below table shows the given values, the statements and the obtained output on the screen for
str = “Dot Programming”

Formatted Output Function Printf() in Turbo C Language

Note: In the last printf() statement all 15 columns are free and the cursor will be place in the 16th column.

Formatted Input Functions

Mathematical Functions in Turbo C Language

The C language provides a number of library functions that carry out mathematical operations such as square root, sine, cosine, tan, and various other actions. These functions, which are part of C mathematical library are called mathematical library functions. They are also called mathematical built in functions. 
Brief description about each mathematical function is available in your program's header file called “math.h”. Therefore, anyone who uses mathematical functions should include “math.h” in the beginning of the program. The various mathematical functions which are commonly used are provided below:

Function with return type
Operations performed
abs(int i)
Returns absolute value of an integer.
cos(x)
Returns the cosine value of x.
acos(x)
Returns the inverse cosine of an angle where x is in radians.
sin(x)
Returns the sine value of x.
asin(x)
Returns the inverse sine of an angle where x is in radians.
tan(x)
Returns the tangent of an angle where x is in radians.
atan(x)
Returns the inverse tangent of an angle where x is in radians.
exp(x)
Returns the value of e to the power x.
fabs(x)
Returns the absolute value of floating point number x.
log(x)
Returns the natural logarithm of x.
log10(x)
Returns the logarithm of x to the base of 10.
pow(x,y)
Returns the value of x to the power y.
sqrt(x)
Returns the square root of x.

 These functions are given here to just start with programming. In most of the general or mathematical problems such built in functions are regularly used. We will discuss about examples about each of these functions later.

Formatted Input/Output Functions

Nested If or If-Else Statement in Computer Programming: C Language

In any computer programming, a variation of if or if-else statement is known as nested if statement. When an if or if-else statement is used within another if or if-else statement then this statement is called nested if statement. When an action has to be performed based on many decisions, then this statement is used. So, it is called multi-way decision statement. There can be many variations of nested if statement. A sample is given below:

if(condition)
{
    if or if-else statement
}
else                           
{
    if or if-else statement
}

The syntax of nested if shown below:
if(condition1)
{
if(condition2)
    {
      Statement(s);
     }
else
     {
       Statement(s);
     }
}
else
{
  Statement(s);
}

In this statement the conditions are evaluated from the top to bottom. If the outermost condition i.e., condition1 is true the statement(s) associated with this condition are executed as the inner if part is executed here. If condition2 is true the statement associated with this condition are executed otherwise the else (condition2’s false part) part’s statement(s) are executed. If the condition1 of outermost if statement is false then, else part of this if statements are executed, as shown in the above syntax.

For example, consider the following statements:
if(salary>=25000)
  {
If(age<=65)
    Write: ‘Increment in salary 25%’
else
    Write: ‘Increment in salary 24%’
  }   

In the above example the if-else statement is inside if statement, hence the above statement is called the nested if statement. Here, if salary >= 25000 and age is less than or equal to 65, then the output is "Increment in salary 25%". If salary is >= 25000 and age is above 65 then the output will be "Increment in salary 24%".

Algorithm:
Step1:    Read:A, B, C
Step2:    if(A>B)
if(A>C)
Write: “A is largest”
else
Write: “C is largest”
          [End of if]
else
if(B>C)
Write: “B is largest”
    else
Write: “C is largest”
         [End of if]
    [End of if]
Step3:    Exit

Nested If statement in computer programming, c language

I am writing a C program to find largest number among the three given number.

main()
 {
intA,B,C;
clrscr();
printf(“Enter any three integer number here:\n”);
scanf(“%d%d%d”,&A,&B,&C);
if(A>B)
     {
if(A>C)
    printf(“Number %d is largest”,A);
else
    printf(“Number %d is largest”,C);
      }
else
     {
if(B>C)
    printf(“Number %d is largest”,B);
else
    printf(“Number %d is largest”,C);
     }
getch();
}

Nested If statement in computer programming, c language

Advantages:

  • There are situations involving series of decisions where we have to use if or if-else statement in another if or if-else statement. In such situations, nested if statements are used.
  • Each and every condition may be evaluated to true or false and may involve expressions of various data types.

Disadvantages:

  • The nested if-statements are difficult to understand and modify.
  • As the depth of nesting increases, the readability of the program decreases.
Nested If or If-Else Statement in Computer Programming: C Language

Switch Statement in Computer Programming, C Language

There is another important branching statement known as the switch statement in computer programming. We know that (discussed earlier), in situations involving series of decisions one after the other, the if-else-if ladder can be used. Each and every condition may involve expressions which results in various data types such as float, double, char, integer etc.

Here, if-else-if ladder or nested if can be used. But, in situations involving series of decisions one after the other, each decision condition may involve expressions which result in integer value. This integer value may be used for comparison using equality operator "==". In these situations, where the result is of integer data type and series of integer values are used in equality decisions, the usage of switch statement is recommended, because it improves readability of the program.

The switch statement provides a way to select a choice out of several choices based on the selection of choice. The choice can either be an integer value or a character value. The choice can also be an expression which results in an integer value. Based on this integer value, the control is transferred to a particular case value.

The syntax of the switch statement is as follows:
switch (expression/choice)
{
  case value1:
      statement(s);
    break;
case value2:
    statement(s);
    break;
case value3:
    statement(s);
    break;
    .
    .
    .
default:
    statement(s);
}
The expression or choice in switch statement is evaluated to an integer value. The integer value thus obtained if matches with any of the values value1, value2, value3……, value n, then the control is transferred to the appropriate case statement(s). During execution of a particular case, if break is not encountered, then control goes to the subsequent case and the statement(s) under that case will be executed till the break statement is encountered.
If the value of the expression does not match with any of the cases values value1, value2,… value n then, control comes out of the switch statement in the absence of default. If default case is part of the switch then the statement(s) in default block will be executed.

Here, we are writing a program to simulate the calculator using switch statement. The input is an expression of the form P op Q Where P and Q are the operands and the op is an operator. For example, a + b, 10 * 20 etc. Based on the operator, the operations is performed and the result is displayed.

Algorithm:
Step1:    Read p, op, q;
Step2:    switch(op)
    case ‘+’:    result = p + q;
    case ‘-’:    result = p - q;
    case ‘*’:    result = p * q;
    case ‘/’:    result = p / q;
    default:    Write: ‘Invalid operator’
    [End of switch]
Step3:    Write: result
Step4:    Exit
   
C program of the above problem in C language:
main()
{
int p,q,result;
char op;
clrscr();
printf(“Enter an expression here:\n”);
scanf(“%d %c %d”,&p,&op,&q);
switch(op)
{
 case ‘+’:
 result = p + q;
break;
case ‘-’:
 result = p - q;
break;
case ‘*’:
 result = p * q;
break;
case ‘/’:
 result = p / q;
break;
default:
printf(“Invalid expression.\n”);
}
printf(“The result of the expression %d %c %d = %d”,p,op,q,result);
getch();
}
Switch Statement in Computer Programming, C Language

Advantages:

  • Improves readability of the program
  • More structure way of writing  the program

Disadvantage:

It can be used only if the expression used for checking the conditions result in integer value or character value. If the result of expression is not integer value, switch statement cannot be used.


Tuesday, February 10, 2015

Range ASP.NET validator control in asp.net c#

Introduction

The Range ASP.NET validator control checks whether or not the value of an input control is inside a specified range of values . It has the following four three properties:
  • ControlToValidate - Property- Contains the Input control to validate
  • MinimumValue - Property- Holds the minimum value of the valid range
  • MaximumValue - Property - Holds the maximum value of the valid range
If one of these properties is set, then the other property must also be set . Do not forget to set the Type property to the data type of the values .  The following data types can be used for the values:

String - A string data type
Integer - An integer data type
Double - A double data type
Date - A date data type
Currency- A currency data type

Application of Range ASP.NET validator control are:


  • Specify age range between the two numbers in register form
  • Use in Date of Birth selection. 
Let see the range asp.net validator control video for implementation:



Public Properties of the Range ASP.NET validator Class
MaximumValue : Obtains or sets the maximum value of the validation range for the RangeValidator control.

MinimumValue : Obtains or set the minimum value of the validation range for the RangeValidator control.

Example
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="rangevalidator.aspx.cs" Inherits="rangevalidator" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

 
 
Enter Your Age ( 18 - 30)<br />
<asp:TextBox ID="agetxt" runat="server"></asp:TextBox>
<asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="agetxt" ForeColor="Maroon" MaximumValue="30" MinimumValue="18" Type="Integer">Enter your age between 18 to 30</asp:RangeValidator>
<br />
<asp:Button ID="Button1" runat="server" Text="submit" />
<br />
   </div>
</form>
</body>
</html>
Output
range

Related Article

Looping Statements in Computer Programming, C Language

Introduction

We have learnt about branching statements that transfer the control from one point to another point in the computer programming. In this article, we concentrate on another important constructs like loop statements, which executes a set of statements repeatedly. Sometimes, it is required to execute a set of statements repeatedly in c programming. In such cases, the looping statements are used.

The statements that enables the programmer to execute a set of statements repeatedly till the required activity is completed, called looping statements or simply loop statements. These statements are also called repetitive or iterative statements. The statements within a loop may be executed for a fixed number of times or until a certain condition is reached. The various types of loop statements that are supported by the C language are as follows:

For Loop

A set of statements may have to be repeatedly executed for a specified number of times. In such situations, we use for loop statement. This type of loop is defined as an iterative statement that causes a set of statements to be executed repeatedly for a fixed number of times. If we know well in advance as how many times a set of statements have to be executed repeatedly, then for loop is the best choice.

The syntax of for loop as follows:
for (initialization; condition; increment/decrement)
{
    statement1;
    statement2;
    statement3;
}

Where

  • for: is the reserve word or keyword
  • Initialization: used to provide starting value to the variable. It should end with semicolon (;).
  • Condition: used to determine whether value of variable has reached the number of repetitions desired. It should end with semicolon (;).
  • Increment/Decrements: It is used to update the variable value by increment or decrements.

Note: Initialization, condition and updating statement can be multiple statements separated by comma (,).

In for loop some points have to keep in mind by c programmer, which are:

  • The initialization statement executes first and only once when the execution begins.
  • After this, condition is executed. If the condition is evaluated as the True then the body of for loop is executed. After executing the body of the loop the increment/decrements statement is executed. It is normally update the variable value by the specified step size. Then termination condition statement is executed and the process is repeated.
  • If the condition is evaluated to False, then the control comes out of the loop without executing the body of the loop. Later, the rest of the code is executed.

As long as condition is evaluated to True, the body of for loop and the updating statement are executed.
For example

for(i=1; i<=5;i++)
{
  printf(“%d”,i);
}
printf(“\nOut of the loop”);

In the above example,

  • For loop is executed for the first time and initialize the value of i =1.
  • Then, condition is checked and it is true since i = 1.
  • So, control enters into body of the loop and displays 1 on the screen using printf statement.
  • Then i is incremented by 1 and condition is checked again. The loop is repeatedly executed for i=1, 2, 3, 4 and 5, all these values are displayed one after other.
  • Finally, when i will be 6, the condition fails and control comes out of the loop and the message "Out of the loop" is displayed on the screen.

Here's an example to find sum of N natural numbers using For loop.

Algorithm:
Step1:    [Read the number of terms]
Read: N
Step2:    [Initialization]
    Sum = 0
Step3:    for i = 1 to N in steps of 1 do
    Sum = Sum + i
    [End of for]
Step4:    Write: Sum
Step5:    Exit

Looping Statements in Computer Programming, C Language


C program to find sum of natural number

main()
{
  int i,n,sum=0;
  printf("Enter the number of terms here:\n");
  scanf("%d",&n);
  for(i=1; i<=n; i++)
   {
     sum=sum+i;
   }
printf("The sun of n %d term = %d",n,sum);
getch();
}

Looping Statements in Computer Programming, C Language

Branching Statements in C

Do-While Loop with Example in C language

In Computer programming, do-while loop is similar to while loop and used when a set of statements may have to be repeatedly executed until a certain condition is reached. When we do not know exactly how many times a set of statements have to be repeated, do-while can be used. The syntax of the do-while loop is shown below in C:

do
{
 statement1;
 statement2;
 statement3;
   .
   .
statementn;
} while (exp);

Where
  • do and while – are reserve words or keywords
  • exp – is the expression which is evaluated to TRUE or FALSE. The semicolon indicates the termination of the loop.
Working of do-while loop:
The following sequences of the operations are carried out during the execution of the do-while loop statement:
  • The body of the do-while loop consisting of statements statement-1, statement-2,….statement-n are executed.
  • Then, the exp is evaluated. If the expression exp is evaluated to TRUE, the body of the loop is executed again and the process is repeated.
  • If the exp is evaluated to FALSE, control comes out of the do-while loop and the statements after (outside) the do-while loop are executed.

Since the expression exp is evaluated to TRUE or FALSE at the bottom of the do-while loop, so, the do-while loop is called bottom testing loop or exit controlled loop. Here, the body of the loop is executed at least once.

Difference between while & do-while loop

Do-While Loop with Example in C: Computer Programming

Here's an example to get sum of 1 to given no.s
Algorithm:

Step1:    [Input the number of terms]
    Read: n
Step2:    [Initialization]
    sum=0
    i=0
Step3:    do
    sum = sum+i
    i = i+1
    while (i<=n)
Step4:    Write: sum
Step5:    Exit

Do-While Loop with Example in C: Computer Programming

C program
main()
{
int n,i,sum;
clrscr();
printf("Enter the value of n here: \n");
scanf("%d",&n);
sum=i=0;
do
{
sum=sum+i;
i++;
}while(i<=n);

printf("Sum of the series is %d",sum);
getch();
}

Do-While Loop with Example in C: Computer Programming, output

Read Also: Jump statements in C

© Copyright 2013 Computer Programming | All Right Reserved