- Construct 100 variables to abundance allotment marks acquired by 100 altered students, i.e. anniversary capricious absolute student's marks.
- Construct capricious (called arrangement or array) able to autumn or captivation all the hundred values.
Obviously, the additional another is better.This is wahat we call array.
ARRAY INITIALISATION:
Arrays can be initialised as followed:
int a[6] = { 2, 4, 12, 5, 45, 5 } ;
int b[ ] = { 2, 4, 12, 5, 45, 5 } ;
float c[ ] = { 12.3, 34.2 -23.4, -11.3 } ;
main( )
{
int m, f ;
printf ( "\nEnter number " ) ;
scanf ( "%d", &m ) ;
f = factorial ( m ) ;
printf ( "Factorial = %d", f ) ;
}
factorial ( int a )
{
int fact = 1, j ;
for ( j = a ; j >= 1 ; j-- )
fact = fact * j ;
return ( fact ) ;
}
OUTPUT:
Enter number 4
Factorial = 24
FACTORIAL OF A NUMBER USING RECURSION:
main( )
{
int m, fact ;
printf ( "\nEnter any number " ) ;
scanf ( "%d", &m ) ;
fact = recr ( m ) ;
printf ( "Factorial = %d", fact ) ;
}
recr ( int a )
{
int z ;
if ( a == 1 )
return ( 1 ) ;
else
z = a * rec ( a - 1 ) ;
return ( z ) ;
}
OUTPUT:
Enter any number 3
Factorial = 6
main( )
{
int x = 20, y = 12 ;
swap ( x, y ) ;
printf ( "\nx = %d y = %d", x, y ) ;
}
swap ( int a, int b )
{
int z ;
z = a ;
a = b ;
b = z ;
printf ( "\na = %d b = %d", a, b ) ;
}
OUTPUT:
a = 12 b = 20
x = 20 y = 12
main( )
{
int r ;
float a, peri ;
printf ( "\nEnter radius of a circle " ) ;
scanf ( "%d", &r ) ;
aperi ( r, &a, &peri ) ;
printf ( "area = %f", a ) ;
printf ( "\nperimeter = %f", peri ) ;
}
aperi ( int r, float *a, float *p )
{
*a = 3.14 * r * r ;
*p = 2 * 3.14 * r ;
}
OUTPUT:
Enter radius of a circle 10
Area = 314.000000
Perimeter = 62.800000
main( )
{
int a = 3 ;
int *b ;
b = &a ;
printf ( "\nAddress of a = %u", &a ) ;
printf ( "\nAddress of a = %u", b ) ;
printf ( "\nAddress of b = %u", &b ) ;
printf ( "\nValue of b = %u", b ) ;
printf ( "\nValue of a = %d", a ) ;
printf ( "\nValue of a = %d", *( &a ) ) ;
printf ( "\nValue of a = %d", *b ) ;
}
OUTPUT:
Address of a = 65512
Address of a = 65512
Address of b = 65522
Value of b = 65512
Value of a = 3
Value of a = 3
Value of a = 3
POINTER DECLARATION:
Declaring pointers can be actual ambagious & difficult sometimes (working with structures & arrow to pointers). To acknowledge arrow capricious they have to accomplish use of * abettor (indirection/dereferencing operator) before the capricious identifier & afterwards ability type. Arrow can alone point to capricious of the aforementioned ability type.
Syntax:
- Call by value
- Call by reference
Call by value basically means passing values between functions when they are called i.e. user provides values to the functions when they are called in the MAIN() function.THese values can either be any constants or variables.
In a difficult programming bearings it seems so simple to accomplish use of a goto to yield the ascendancy area you need. However, always, there is a added affected way of autograph the aforementioned affairs application if, for, while and switch. These constructs are far added analytic and aboveboard to understand.
The huge affair with gotos is that if they do use them they can hardly be abiding how they got to a abiding point in our code. They abstruse the breeze of control. The GOTO keyword is basically used to get over a certain set of instructions instantly.a program illustrating this is shown below:
main( )
{
int n = 27 ;
switch ( n )
{
case 1239 :
printf ( "i am in case 1239 \n" ) ;
break ;
case 56 :
printf ( "I am in case 56 \n" ) ;
break ;
case 29 :
printf ( "I am in case 29 \n" ) ;
break ;
default :
printf ( "I am in default \n" ) ;
}
}
OUTPUT:
I am in default
Syntax for switch statement:
switch ( integer expression )
{
case constant 1 :
do this ;
case constant 2 :
do this ;
case constant 3 :
do this ;
default :
do this ;
}
/*Demonstration of break statement*/
main( )
{
int n, count ;
printf ( "Enter a number " ) ;
scanf ( "%d", &n ) ;
count = 2 ;
while ( count <= n - 1 )
{
if ( n % count == 0 )
{
printf ( "Not a prime number" ) ;
break ;
}
count++ ;
}
if ( count == n )
printf ( "Prime number" ) ;
}
General syntax for DO-WHILE loop :
do
{
this ;
and this ;
and this ;
and this ;
} while ( this condition is true ) ;
/* Execution of a loop for many number of times */
main( )
{
char other ;
int n ;
do
{
printf ( "Enter number " ) ;
scanf ( "%d", &n ) ;
printf ( "square of %d is %d", n, n * n ) ;
printf ( "\nWant to enter other number y/n " ) ;
scanf ( " %c", &other ) ;
} while ( other == 'y' ) ;
}
/* Demonstration of nested loops */
main( )
{
int a, b, s;
for ( a = 1 ; a <= 3 ; a++ ) /* outer loop */
{
for ( b = 1 ; b <= 2 ; b++ ) /* inner loop */
{
s= a + b ; printf ( "a = %d b = %d sum= %d\n", a, b, s) ;
}
}
}
/* Calculation of simple interest for 4 sets of pi, t and rate */
main ( )
{
int pi, t, i ;
float rate, si ;
for ( i = 1 ; i <= 4 ; i = i + 1 )
{
printf ( "Enter values of pi, t, and rate " ) ;
scanf ( "%d %d %f", &pi, &t, &rate ) ;
si = pi * t * rate / 100 ;
printf ( "Simple Interest = Rs.%f\t", si ) ;
}
}
The for bend specify things about a bend in a individual line:
(a) Setting a bend adverse to an basic value.
(b) Testing the bend adverse whether its amount has accomplished the no. of repititions desired.
(c) Increasing the account of bend adverse anniversary time the affairs articulation aural the bend has been executed.
Syntax for FOR loop:
The versatility of the computer lies in its adeptness to accomplish a set of instructions repeatedly. This involves repeating some allocation of the affairs either a defined amount of times or until a specific action is getting satisfied.
Various loops availble in C are as follows:
- a for statement
- a while statement
- a do-while statement
The codicillary or conditional operators ? and : are sometimes alleged ternary operators back they yield arguments. In fact, they anatomy a affectionate of foreshortened if-then-else. Their accepted anatomy is :
expression 1 ? expression 2 : expression 3
If announcement one is accurate (that is, if its amount is non-zero), again the account alternate will be announcement two, contrarily the account alternate will be announcement 3. Example showing conditional operator:
main( )
{
int a = -4, b, n ;
b = ( n < 0 ? 0 : n * n ) ;
printf ( "\n%d",b ) ;
}
Various logical and arithmetic operators useful in c are give below as:
Operators | Type |
! | Logical NOT |
* / % | Arithmetic and modulus |
+ - | Arithmetic |
< > <= >= | Relational |
== != | Relational |
&& | Logical AND |
|| | Logical OR |
= | Assignment |
#include <stdio.h>
#include <math.h>
main()
{
float a, b, c, x1, x2, q, d, x1r, x2r, x1i, x2i;
printf("Input coefficients of square equation a,b,c:");
scanf("%f %f %f", &a, &b, &c);
d=b*b -4.0*a*c;
if (d > 0)
{
/* results are real numbers */
q = pow (d, 1./2);
x1 = (-b + q)/(2*a);
x2 = (-b - q)/(2*a);
printf ("X1=%f X2=%f\n", x1, x2);
}
else if (d == 0)
{
/* there’s only one result */
x1 = -b/(2*a);
printf ("X1=X2=%f\n", x1);
} else
{
/* results are complex numbers */
q = pow(-d, 1./2);
x1r = -b/(2*a) ;
x2r = x1r;
x1i = q/(2*a);
x2i = -x1i;
printf ("X1 = (%f,%f)\n", x1r, x1i);
printf ("X2 = (%f,%f)\n", x2r, x2i);
}
}
Nested if else means if else into if else i.e. multiple if else statements included in one if else statement.Below is the program demonstrating if else :
/* A demo of nested if-else */
main( )
{
int m ;
printf ( "Enter either 0 or 1" ) ;
scanf ( "%d", &m ) ;
if ( m == 0 )
printf ( "this is zero" ) ;
else
{
if ( m == 1 )
printf ( "this is one" ) ;
else
printf ( "wrong choice" ) ;
}
}
The if account by itself will assassinate a individual statement, or a accumulation of statements, if the announcement afterward if evaluates to true. It does annihilation if the announcement evaluates to false.
SIMPLE IF ELSE PROGRAM :
/* Calculation of gross salary */
main( )
{
float base, gross, da, hra ;
printf ( "Enter basic salary " ) ;
scanf ( "%f", &base ) ;
if (base < 2000 )
{
hra =base * 20 / 100 ;
da = base * 80 / 100 ;
}
else
{
hra = 100 ;
da = base * 90 / 100 ;
}
gross = base + hra + da ;
printf ( "gross salary = Rs. %f", gross ) ;
}
main( )
{
int n ;
printf ( "Enter a number less than 20 " ) ;
scanf ( "%d", &n ) ;
if ( num <= 20 )
printf ( "THIS IS AN ACCURATE PROGRAM" ) ;
}
The keyword if tells the compiler that what follows is a alarm ascendancy instruction. The action afterward the keyword if is consistently amid aural a brace of parentheses. If the condition, whatever it is, is true, again the account is executed. If the action is not accurate again the account is not executed.
Various conditional operators used for if statement are as follows:
- Greater than(>)
- Less than(<)
- Equal to(= =)
- Greater than equla to(>=)
- Less than equal to(<=)
- Not equal to(!=)
Many a times,we would like a set of instructions to be accomplished in situation, and an absolutely altered set of instructions to be accomplished in addition situation. This array of bearings is dealt in C programs application a alarm ascendancy instruction.various decision control stements in C are as follows:
- The if statement
- The if-else statement
- The conditional operators
There are fundamentally three types of instructions in C:
- Type Acknowledgment Instruction
- Arithmetic Instruction
- Control Instruction
The purpose of anniversary of these instructions is accustomed below:
1) Type acknowledgment instruction
To acknowledge the blazon of variables acclimated in a C program.
2)Arithmetic instruction
To accomplish addition operations amid con-stants & variables.
3)Control instruction
To ascendancy the arrangement of beheading of assorted state-ments in a C program.
To accomplish the affairs accepted the affairs itself care to ask the user to accumulation the ethics of p, n & r through the keyboard in the coursework of execution. This can be accomplished application a action alleged scanf( ). This action is a counter-part of the printf( ) function. printf( ) outputs the ethics to the awning admitting scanf( ) receives them from the keyboard. This is illustrated in the affairs apparent below:
/* Calculation of simple interest */
main()
{
int a, b;
float c, si ;
printf ( "Enter values of a, b, c" ) ;
scanf ( "%d %d %f", &a, &b, &c ) ;
si = (a * b * c )/ 100 ;
printf ( "%f" , si ) ;
}
Our first C program would simply calculate simple interest for a set of values representing principle, number of years and rate of interest.
/* Calculation of simple interest */
main( )
{
int a, b;
float c, si ;
a = 1000 ;
b = 3 ;
c = 8.5 ;
/* formula for simple interest */
si = (a * b * c) / 100 ;
printf ( "%f" , si ) ;
}
Variable names are names accustomed to locations in memory. These locations can accommodate integer, absolute or appearance constants. In any language, the categories of variables that it can abutment depend on the categories of constants that it can handle. This is because a specific blazon of capricious can authority alone the aforementioned blazon of constant. For example, an accumulation capricious can authority alone an accumulation constant, a absolute capricious can authority alone a absolute connected & a appearance capricious can authority alone a appearance constant.
There are basically three data types in c language:
1) Constants-
A constant is an entity that doesn’t change whereas a variable is an entity that may change.
Types of C Constants:
C constants can be divided into two major categories:
(a) Primary Constants
These are again divided into three types as:
- Real constants
- Integer constants
- Character constants
(b) Secondary Constants
These are again further divided into :
- Arrays
- Pointers
- Structures
- Union etc
alphabets | A, B, ….., Y, Z a, b, ……, y, z |
digits | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 |
special symbols | ‘ ! @ # % ^ & * ( ) _ - + = | \ { } [ ] : ; " ' < > , . ? / |
C is a programming accent developed at AT & T's Bell Laboratories of USA in 1972. It was advised and accounting by a man called Dennis Ritchie. In the backward seventies C began to alter the added accustomed languages of that time like PL/I, ALGOL, etc. No pushed C. It wasn't fabricated the official Bell Labs language. Thus, after any advertisement C's acceptability advance and its basin of users grew. Ritchie seems to accept been afraid that so lots of programmers adopted C to earlier languages like FORTRAN or PL/I, or the newer ones like Pascal and APL. But, that is what happened.
Blog Archive
-
▼
2011
(50)
-
▼
July
(38)
- INTRODUCTION TO C
- DATA SETS IN C
- DATA TYPES
- VARIABLES
- SIMPLE C PROGRAM
- PROGRAM USING SCANF() AND PRINTF()
- C INSTRUCTIONS
- INTRODUCTION TO DECISION CONTROL STRUCTURES
- IF STATEMENT
- IF STATEMENT PROGRAM
- IF ELSE STATEMENT
- NESTED IF ELSE
- DETERMINING ROOTS OF AN EQUATION
- ELSE IF STATEMENT
- LOGICAL AND ARITHMETIC OPERATORS
- CONDITIONAL OPERATORS
- LOOPS
- THE WHILE LOOP
- The for bend specify things about a bend in a ind...
- EXAMPLE OF FOR LOOP
- NESTING OF LOOPS
- THE ODD LOOP
- THE DO-WHILE LOOP
- BREAK STATEMENT IN C
- THE CONTINUE STATEMENT IN C
- SWITCH STATEMENT IN C
- SIMPLE PROGRAM SHOWING SWITCH STATEMENT
- THE GOTO STATEMENT IN C LANGUAGE
- FUNCTIONS IN C LANGUAGE
- CALLING FUNCTIONS IN C LANGUAGE
- CALL BY REFERENCE IN C LANGUAGE
- POINTERS IN C LANGUAGE
- SIMPLE PROGRAM SHOWING POINTER IN C LANGUAGE
- PROGRAM FOR FUNCTION CALL BY REFERENCE IN C
- SWAPPING OF TWO NUMBERS IN C
- RECURSION IN C LANGUAGE
- FACTORIAL OF A NUMBER WITHOUT RECURSION IN C
- ARRAYS IN C LANGUAGE
-
▼
July
(38)