C Notes
C Notes
Introduction:
⮚ C is a popular general purpose programming language. C language has been designed and developed by Dennis Ritchie at Bell Laboratories in 1972.
⮚ This language is associated closely with Unix operating system. The source code for the Unix operating system is coded in C.
⮚ C runs under a number of operating system includes MS DOS. C programs are efficient, fast and highly portable i.e.
⮚ C programs written on one computer can run on another with little or no modifications. C functions are building blocks in which all programming activities are incorporated.
1. Structure of a c program:
Each C program contains a number of several building blocks know as functions. Each function of it performs task independently. A function is subroutine that may consist of one or more
statements.
An C program comprises of the following section
/* Comments */
main( ) function name
{
/* Comments */
Declaration part
Executable part
}
user_defined functions
{
/* Comments */
Declaration part
Executable part
}
⮚ Include header file section: C program depends upon some header files for function definition that are used in program. Each header file by default is extended with .h . The file
⮚ Global declaration: This section declares some variables that are used in more than one function. These variables know as global variables. These variables must be declared outside
⮚ Function main: Every program written in C language must be contain main() function. Empty parentheses after main are necessary. The function main () us the starting point of every
‘C’ program. The Execution of the program always begins with the function main (). The program execution start with the opening brace “ { “ and end with the closing “ } “. Between these two
braces the programmer should declare the declaration ad the executable part.
⮚ Declaration part: The declaration part declares the entire variables that are used in executable part. The initializations of variables are also done in this section. The initialization
⮚ Executable part: This part contains the statements following the declaration of the variables. This part consists a set of statements or a single statement. These statements are enclosed
⮚ User defined functions: The functions defined by the user are called user-defined functions. These functions are generally defined after the main () function. They can also be defined
Page | 1
Introduction to Programming
⮚ Comments: Comments are not necessary in the program. However, to understand the flow of program the programmer can include comments in the program. Comments are to be
Page | 2
Introduction to Programming
Delimiters Use
4. MTHE C KEYWORDS
The C keywords are reserved words by the compiler. All the C keywords have been assigned fixed meaning. The keywords cannot be use as variable names because they have been assigned fixed
jobs. However, few C compilers allow to construct variable names, which exactly coincides with the keywords. It is suggested not to mix up keywords with variable names. The C keywords are
listed in table.
volatile while
IDENTIFIERS
Identifiers are names of variables, functions, and arrays. They are user-defined names, consisting of sequence of letters and letters and digits, with the letter as the first character. Lowercase case
letters are preferred. The upper case letters are also permitted. The “ _ “ under score symbol can be used as an identifier.
Ex:
#define N 10
#define a 15
Here, ‘N’ and ‘a’ are user-define identifiers.
5. CONSTANTS
The Constants in C are applicable to the values, which do not change during the execution of a program. There are several types of constants in C. they are classified in to the following groups as
given in table.
A.
Numerical Constants
1. C decimal
Integer constants: These are the sequence of numbers from 0 to 9 without constants
points or fractional part or any other symbols; it requires minimum two bytes and maximum four
bytes. Integer constants could either be positive or negative or my be zero.
Ex: 24, 30, +40, -13
2. Real constant: Real constants are often known as floating point constants. Integer constants are unfit to represent many quantities. Many parameters or quantities are defined not only in
integers but also in real numbers. For example, length, prize, distance. Are measure in real numbers.
|Ex: 2.6, 5.234, 3.14 etc., Numerics Constants Character Constants
B.
Character Constant
1. Single character constant: A character constant is a single character. They are also represented with single digit or a single special symbol or white space enclosed with in a pair of
single quote marks
Integer constants Single Character
Ex: ‘b’, ‘9’, ect.,
Real constants String Constants
2. String constant: String constants are sequence of characters enclosed within a double quote marks. The string may be a combination of all kind of symbols.
String Constants
Ex: “hello”, “india”, “567”, “a”
Page | 3
Introduction to Programming
Creating constants in C
We create a constant of any datatype using 'const' keyword. To create a constant, we prefix the variable declaration with 'const' keyword.
The general syntax for creating constant using 'const' keyword is as follows...
const int x = 10 ;
Example Program :
#include<stdio.h>
#include<conio.h>
void main(){
int i = 9 ;
const int x = 10 ;
i = 15 ;
x = 100 ; // creates an error
printf("i = %d\nx = %d", i, x ) ;
}
The above program gives an error because we are trying to change the constant variable value (x = 100).
We can also create constants using '#define' preprocessor directive. When we create constant using this preprocessor directive it must be defined at the beginning of the program (because all the
preprocessor directives must be written before the global declaration).
We use the following syntax to create constant using '#define' preprocessor directive...
Example
#define PI 3.14
Example Program
#include<stdio.h>
#include<conio.h>
#defien PI 3.14
void main(){
int r, area ;
printf("Please enter the radius of circle : ") ;
scanf("%d", &r) ;
Page | 4
Introduction to Programming
area = PI * (r * r) ;
printf("Area of the circle = %d", area) ;
}
Variables :
When a program is executed, may operations are carried on the data. The data types are integers, real or character constants. These data are stored in the memory and at the time of execution
different operation are performed on them. Variable is name given to the memory location.
A variable is data name used to storing a data value. Its value may be changed during the program execution. The value of the variable keeps on changing during the execution of a
program. A variable name may be declared based on the meaning of the operation. Some meaningful variable names are height, average, sum, add, mul, div etc.,
Rules for defining variables:
● They must begin with a character without spaces but underscore to permit.
● The length of the variable varies from compiler to compiler. Generally most of compilers support 8 characters excluding extension.
● The variable should not be a C keyword
● The variable names may be a combination of uppercase and lowercase characters. For example suM and sum but both are not same.
● The variables name should not start with a digit.
6. DATA TYPES
All compilers offer different data types. This enable the programmer to select the appropriate data type as per the need of application. Generally data is represented using numbers or characters. The
numbers may be the integer or real. The different compilers supports the different data types Those are given bellow
8. INITIALIZING VARIABLES
Variable declared can be assigned or initialized using a assignment operator “= “. The declaration and initialization can also be done in the same line.
Syntax :- variable_name=constant;
Data_type variable_name=constant;
Example:
int y=2; y is a variable, its integer type, and it stores value 2
int x=y=z=1;
9. EXPRESSIONS
An expression is a combination of operators and operands which reduces to be a single value. An operator indicates an operation to be performed on data that yields a value.
a.
Primary Expressions: In C the operand in the primary expression can be a name, a constant or an parenthesized Expression.
Name is any identifier for a variable. A constant is the one whose value can be changed during program execution. Any value enclosed within parenthesis must be reduced to single value.
Page | 5
Introduction to Programming
Example: (3*5+8);
(C=a=5*c);
b.
prefix: the operator used before the operand is called pre increment/decrement
Ex: + a b it is equal to a+b
c.
postfix: the operator used after the operand is called post increment/decrement.
Ex: a b + it is equal to a+b
d.
Unary Expressions: An unary expression is like a prefix expression consists of one operand and one operator and the operand comes after the operator.
Ex: ++a=a+1, -c, b++ =b+1
e.
Binary expressions: Binary expressions are the combinations of two operands and an operator. Any two variables added, subtracted, multiplied or divided is a binary expression.
Ex; a*b; c%d;
f.
Ternary Expressions: Ternary expression which consists of a ternary operator pair “? :”
Ex: exp1? exp2:exp3; => (a>b)?a=max:b=max;
In the above example, if exp1 is true exp2 is executed else exp3 is executed.
10. OPEARTORS:
In order to perform different kinds of operations, C uses different kinds of operator. An operator indicates an operation to be performed on data that yields a value. Using various operators in C one
can link the variable and constants. An operand is a data item in which operators perform the operations. Table shows the operators in C.
5 Assignment operator =
7 Comma operator ,
8 Conditional operator ?:
a.
Comma Operator:- The comma operator is used to separate two or more expressions. The comma operator has the lowest priority among all the operators.
Ex: a=5,a=b%c;
The bellow program illustrates the use of comma operator
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
//comma operator can also used for separate the variables.
clrscr();
printf("\nread a & b values : ");
scanf("%d %d",&a,&b);
//comma operator used in scanf function
c=a+b;
//comma operator used in printf function
printf("\n a = %d, b = %d, c = %d",a,b,c);
getch();
}
Output:
read a & b values : 12 24
a = 12, b = 24, c = 36
b.
Conditional operator(? :) The conditional operator contains a condition followed by two statements or values. If the conditions is true the first statement is executed otherwise second
statement. The conditional operator (?) and (:) are sometimes called ternary operators because they take three arguments. The syntax of conditional operator is as given bellow.
Syntax: condition?(expression1):(expression2);
The bellow program illustrates the use of conditional operator
Page | 6
Introduction to Programming
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,max;
clrscr();
printf("\n enter a and b values : ");
scanf("%d %d",&a,&b);
//use of the ternary operator
max=(a>b)?a:b;
printf(" maximum value is : %d",max);
getch();
}
Output 1: enter a and b values : 6 7
maximum value is : 7
Output 2: enter a and b values : 20 8
maximum value is : 20
C.
Arithmetic Operators: there are two types of arithmetic operators. They are 1. Binary operators and 2. Unary operators.
I.
Binary operators: These operators are commonly used in most of the computer languages. These arithmetic operators are used for numerical calculations between the two constant
values. They are also called as Binary Arithmetic Operators. Table shows the arithmetic operators declared in the program.
+ Addition 2+4=6
- Subtraction 5-4=1
* Multiplication 2*5=10
/ Division 6/3=2
Page | 7
Introduction to Programming
. prefix: the operator used before the operand is called pre increment/decrement
Ex: ++a or --a
. postfix: the operator used after the operand is called post increment/decrement.
Ex: a-- or a++
Program to illustrate the increment and decrement operators:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
Page | 8
Introduction to Programming
clrscr();
printf("\nprogram to implement the increment and decrement operators");
printf("\n enter the values of a and b ");
scanf("%d %d",&a,&b);
printf("before the change value of a :%d",a);
c=a++;
printf("\n afer the post increment of a, value of c = %d",c);
printf("\n after postincrement a= %d",a);
c=++a;
printf("\n after the preincrement of a, value of c = %d",c);
printf("\n after the preincremnt of a= %d",a);
printf("\n***decrement***");
printf("before the change value of b : %d",b);
c=b--;
printf("\n afer the post decrement of a, value of c = %d",c);
printf("\n after post decrement b= %d",b);
c=--b;
printf("\n after the pre decrement of a, value of c = %d",c);
printf("\n after the pre decrement of b= %d",b);
getch();
}
output :
program to implement the increment and decrement operators
enter the values of a and b 4 5
before the change value of a :4
afer the post increment of a, value of c = 4
after post increment a= 5
after the pre increment of a, value of c = 6
after the pre incremnt of a= 6
***decrement***before the change value of b : 5
afer the post decrement of a, value of c = 5
after post decrement b= 4
after the pre decrement of a, value of c = 3
after the pre decrement of b= 3
d.
Relational Operators: These operators are used to distinguish between two values depending on their relation. These operands provide the relation between the two expressions. If the
relation is true then it return a value “1”, otherwise “0 “for the false relation. Table shows the relation operators in C.
Page | 9
Introduction to Programming
printf("\n10==10 : %3d",10==10);
printf("\n10>=10 : %3d",10>=10);
printf("\n10<=100 : %3d",10<=100);
printf("\n10!=9 : %3d",10!=9);
getch();
}
output:
Condtion : return values
10!=10 : 0
10==10 : 1
10>=10 : 1
10<=100 : 1
10!=9 : 1
e.
Logical Operator: The logical relationships between the two expressions are checked with logical operators. Using these operators two expressions can be joined. After checking the
conditions it provides logical true “1” or false “0” status. Table shows the logical operators in C.
From the above table following rules can be followed for logical operators.
i. The logical AND (&&) operator true “1” when both the expressions are true otherwise “0’.
T T T 1
F T F 0
T F F 0
F F F 0
ii. The logical OR (||) operator false “0” when both the expression are false otherwise “1”.
T T T 1
F T T 1
T F T 1
F F F 0
iii. The logical NOT (!) provides “0” if the condition is true otherwise “1”
Expression A !A Return
T F 0
F T 1
Page | 10
Introduction to Programming
d=!(a>=b);
printf("\n Logical NOT d: %d",d);
getch();
}
output 1:
program to implement logical operations;
enter a ,b, c values : 12 10 13
Logical AND d: 0
Logical OR d: 1
Logical NOT d: 0
output 2:
program to implement logical operations;
enter a ,b, c values : 20 10 08
Logical AND d: 1
Logical OR d: 1
Logical NOT d: 0
output 3:
program to implement logical operations;
enter a ,b, c values : 12 15 12
Logical AND d: 0
Logical OR d: 1
Logical NOT d: 1
f.
Bitwise Operators: - C supports a set of bitwise operators as listed in the table. C supports six bit operators. These operators can operate only on an integer operands such as int, char,
short, long etc.,
~ One’s Complement
| Bitwise OR 5|6 7
Page | 11
Introduction to Programming
getch();
}
11.
List of Operators with Precedence:
Operator Operation Associativity Precedence
() Function call
. Structure Operator
+ Unary plus
- Unary minus
++ Increment
-- Decrement
! Not operator nd
Right to left 2
~ Ones complement
* Pointer operator
* Multiplication
rd
/ Division Left to right 3
% Modular division
+ Addition th
Left to right 4
- Subtraction
== Equality th
Left to right 7
!= Inequality
th
& Bitwise AND Left to right 8
th
^ Bitwise XOR Left to right 9
th
| Bitwise OR Left to right 10
th
&& Logical AND Left to right 11
th
|| Logical OR Left to right 12
th
?: Conditional operator Right to left 13
th
=, *=, - =, &= +=, |=, <<=, >>= Assignment operators Left to right 14
th
, Comma operator Left to right 15
Page | 12
Introduction to Programming
printf
“ MESSAGE “
(
Ex: printf(“My first C program “);
) ;
printf(“Let us check what c is ….”);
Syntax Diagram for printf() form 2:
printf FORMAT SPECIFIER
“ MESSAGE “ v.n )
Steps to be followed in(printf , ;
⮚ plan the actual design of the output
⮚ plan the format specifications where necessary.
⮚ FORMAT
Replace the data with designated conversion specifications. ,
b. SPECIFIER
Data Input Standards
Syntax diagram for scanf function
⮚ “scanf” It is a precompiled library function in C language prototyped in stdio.h
⮚ Used to scan data from the standard input device.
⮚ Proper conversion specifications should be used to input, characters, numbers and strings.
⮚ It is a function, which scans the data of various types that is properly recognized by C syntax and semantics.
⮚ The scanned data is placed in the proper memory location for which the address, address is supplied using the address operator, which is a unary operator represented as ‘&’
C supports some special backslash character constants that are used in output functions. For example the symbol ‘\n” stands for new-line character. A list of such backslash character constants is
given in table. Note that each one of them represents one character, although they consist of two characters. These character combinations are known as escape sequences.
2 \b Back space
3 \f Form feed
4 \n New line
5 \r Carriage return
6 \t Horizontal tab
7 \v Vertical tab
8 \’ Single quote
9 \” Double quote
10 \? Question mark
11 || Backslash
12 \0 null
What is an algorithm?
An algorithm is a step by step procedure to solve a problem. In normal language, the algorithm is defined as a sequence of statements which are used to perform a task.
Algorithms are used to convert our problem solution into step by step statements. These statements can be converted into computer programming instructions which form a program. This program
is executed by a computer to produce a solution. Here, the program takes required data as input, processes data according to the program instructions and finally produces a result as shown in the
following picture.
Specifications of Algorithms
Every algorithm must satisfy the following specifications...
1.
Input - Every algorithm must take zero or more number of input values from external.
2.
Output - Every algorithm must produce an output as result.
Page | 13
Introduction to Programming
3.
Definiteness - Every statement/instruction in an algorithm must be clear and unambiguous (only one interpretation).
4.
Finiteness - For all different cases, the algorithm must produce result within a finite number of steps.
5.
Effectiveness - Every instruction must be basic enough to be carried out and it also must be feasible.
Flowchart is a diagrammatic representation of sequence of logical steps of a program. Flowcharts use simple geometric shapes to depict processes and arrows to show relationships and
process/data flow.
Start/Stop Used at the beginning and end of the algorithm to show start and end of the program.
Stands for decision statements in a program, where answer is usually Yes or No.
Decision
A program is nothing but the execution of sequence of one or more instructions. Quire often it is desirable to alert the sequence of the statements in the program depending upon certain
circumstances. In practical applications there are a number of situations where one has to change the order of the execution of statements based on the conditions.
These conditions can be placed in the program using decision-making statements. C-language supports the control statements as listed below.
a. Simple if
b. If else
c. else if
d. switch statements
a.
Simple if statement: In a simple ‘if’ statement, a condition is tested. If the condition is true, sets of statements are executed. If the condition is false, the statements are not executed
and the control goes to the next statement that immediately follows.
Syntax:
if (condition)
{
statement (s);
}
next statement
Page | 14
Introduction to Programming
False True
Start
Example program: program to implement the simple if statements, to find the given number is +ve or not
#include<stdio.h>
#include<conio.h>
void main() Input values
{
int n;
clrscr();
printf(“\n program to find the given number is +ve or not”); Condition
}
printf(“***over***”);
getch();
} Statements
output 1:
program to find the given number is +ve or not
enter n value 24
given number is +ve stop
***over***
output 2:
program to find the given number is +ve or not
enter n value -24
***over***
b.
if else :- In simple “if” statements, when the condition is true, a set of statements are executed. But when if it is false,
there is no alternate set of statements. The statement ‘else’ provided the same. The if…else statement takes care of true as well as false conditions. It has two blocks. One block is for ‘if” and it
executed when the condition following “if” is true. The other conditions block is of “else” and it is executed when the condition is false. The “else” statement cannot be used without “if”. No
multiple else statements are allowed with one “if”.
Syntax:
if(the condition is true)
Execute the statements1;
else
Execute the statements 2;
Flowchart representing an if….else statement
False True
Start
Page | 15
Introduction to Programming
Page | 16
Introduction to Programming
Page | 17
Introduction to Programming
Output:
Enter Salary: 5000
HRA is : 650.00
DA is : 1400.00
Gross Salary is : 7050.00
Page | 18
Introduction to Programming
Output:
Enter a character a
Given character is Alphabet
Given character is Lower case character ch : a
***over***
Enter a character A
Given character is Alphabet
Given character is Upper case character ch : A
***over***
Enter a character 9
Given character is not a alphabet
Given character may be a digit or special character
***over***
d.
The switch statement:
Design a program using if statement to control the selection. However, the complexity if such program increases dramatically when the number of alternatives increases. The program becomes
difficult to read and follow. At times, it may confuse even the person who designed it. C has a built-in multi way decision statement known as a switch. The switch statement tests the value of a
given variable (0r expression) against a list of case values and when a match is found, a block of statements associated with that case is executed. The syntax of switch as follows.
Syntax:
Switch(variable or expression)
{
case constant A:
statements;
break;
case constant B:
statements;
break;
default:
statement;
}
The expression is an integer expression or characters. Value-1, value-2 .. are constants or constant expressions (valuable to an integral constant) and are know as case labels. Each of these
values should be unique within a switch statement block-1, block-2… are statement lists and may contain zero or more statements. There is no need to put braces around the blocks. Note that
case labels and with a colon (:).
When the switch is executed, the value of the expression is successively compared against the values value-1, value-2,… if a case is found whose value matches with the value of the expression,
then the block of statements that follows the case are executed.
The break statement at the end of each block signal the end of a particular case and causes an exit from the switch statement, transferring the control to the statement-x following the switch.
The default is an optional case. When present, it will be executed if the value of the expression does not match with any case values. If not present, no action takes place if all matches fail and
the control goes to the statement-x.
Program to perform the arithmetic operations using switch statements
#include<stdio.h>
#include<conio.h>
void main()
{ char ch;
int a,b,mod;
float add,sub,mul,div;
clrscr();
printf("\n1.Adition to press + ");
printf("\n2.subtraction to press -");
printf("\n3.multiplication to press *");
printf("\n4.division to press /");
printf("\n5.modulus to press %");
printf("\n**************");
Page | 19
Introduction to Programming
printf("\n***Enter Options***");
printf("\nEnter an arthmetci operator ");
scanf("%c",&ch);
printf("\n***Enter Options***");
printf("\nEnter an arthmetci operator ");
scanf("%c",&ch);
printf("\nenter a and b values :");
scanf("%d%d",&a,&b);
switch(ch)
{
case '+':
printf("\n**performing addition operation** ");
add=a+b;
printf("\nAddition of two numbers is :%.2f",add);
break;
case '-':
printf("\n**performing subtraction operation** ");
sub=a-b;
printf("\nsubtraction of two numbers is :%.2f",sub);
break;
case '*':
printf("\n**performing multiplication operation** ");
mul=a*b;
printf("\nMultiplication of two numbers is :%.2f",mul);
break;
case '/':
printf("\n**performing division operation** ");
div=(float)a/(float)b;
printf("\nDivision of two numbers is :%.2f",div);
break;
case '%':
printf("\n**performing modulus operation** ");
mod=a%b;
printf("\nmodulus of two numbers is :%d",mod);
break;
default :
printf("\n you had choicen non arithmatic chatacter ");
}
getch();
}
Output :
1.Adition to press +
2.subtraction to press -
3.multiplication to press *
4.division to press /
5.modulus to press %
**************
***Enter Options***
Enter an arthmetci operator %
enter a and b values :27
4
Page | 20
Introduction to Programming
What is a Loop?
A loop is defined as a block of statements which are repeatedly executed for certain number of times.
Steps in loop:
Loop variable: it is a variable used in the loop.
Initialization: it is the first step in which starting and final value is assigned to the loop variable. Each time the update value is checked by the loop itself
Increment/ Decrement: it is the numerical value added or subtract to the variable in each round of the loop.
The C-language supports three types of loop control statements. They are given bellow with the syntax
While loop:
The while is a loop condition statement, it test condition may be any expression. The loop statements will be executed till the condition is false i.e. the test condition is evaluated and if the condition
is true, then the body of the loop is executed. When the condition becomes false the execution will be come out from the loop.
Syntax of while:
Expression/initialization;
while(condition)
{
Statements;
Expressions;
}
Steps in while loop:
1. The test condition is evaluated and if it is true, the body of the loop is executed.
2. On the execution of body, test condition is repetitively checked and if it is true the body is executed.
3. The process of execution of the body will be continuing till test condition becomes false.
4. If the condition is false then the control is transferred out of the loop.
The execution of the while loop can be followed by the following flow chat
True False
Test
Condition
Body of loop
Statements
Page | 21
Introduction to Programming
5 1 T 5 4
4 5 T 5*4=20 3
Page | 22
Introduction to Programming
3 20 T 20*3=60 2
2 60 T 60*2=120 1
1 120 F
p)Write a program to calculate of sum of consecutive even and odd integer separately?
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,eve,odd;
clrscr();
printf("\nEnter n value :");
scanf("%d",&n);
i=0;
eve=0;
odd=0;
while(i<=n)
{
if(i%2==0)
{
eve=eve+i;
}
else
{
odd=odd+i;
}
i++;
}
printf("\n sum of even numbers is : %d",eve);
printf("\n sum of odd numbers is : %d",odd);
getch();
}
output:
Enter n value : 30
sum of even numbers is : 240
sum of odd numbers is : 225
Page | 23
Introduction to Programming
}
printf("\n Reverese of a number given number is :%d",rev);
if(rev==temp)
{
printf("\ngiven number is palindrom");
}
else
{
printf("\ngiven number is not palindrom");
}
getch();
}
output:
Enter n value 2112
Reverese of a number given number is :2112
given number is palindrome
Page | 24
Introduction to Programming
}
while(n!=0);
printf("\nsum of positive integers : %d",pos);
printf("\nsum of nagative integers : %d",nev);
getch();
}
output:
Enter n value : 10
Enter n value : -10
Enter n value : 20
Enter n value : -20
Enter n value : 30
Enter n value : -30
Enter n value : -40
Enter n value : 0
sum of positive integers : 60
sum of nagative integers : -100
p)Write a program to print the entered number in reverse order. Use do while loop, also perform sum and multiplication with their digits
#include<stdio.h>
#include<conio.h>
void main()
{
int rem,sum,mul,n;
clrscr();
printf("\nEnter n value :");
scanf("%d",&n);
sum=0;
mul=1;
do
{
rem=n%10;
sum=sum+rem;
mul=mul*rem;
n=n/10;
}while(n!=0);
printf("\nsum of digits : %d",sum);
printf("\nmultiplication of digits : %d",mul);
getch();
}
output:
Enter n value :3425
sum of digits : 14
multiplication of digits : 120
321=3+2+1=6
321=3*2*1=6
Page | 25
Introduction to Programming
Statements;
}
1. The initialize the variable sets a loop to an initial(starting) value. This statement is executed only once.
2.
The test condition is a relational expression that determines the number of iterations desired or t0 determines when to exit from the loop . The ‘for’ loop continues to execute as long as
conditional test is satisfied, when the conditions becomes false the control of the program exits from the body of to ‘for’ loop and executes next statement after the body of the loop.
3. The increment/decrement/expression parameter decides how to make changes in the loop (increase or decries operations are to be used).
4. The body of the loop may contain either a single statement or multiple statements. In case there is only one statement after the ‘for’ loop braces may not be necessary.
Various formats of ‘for’ loop
for(a=0;a<=10;a++) Display value from 0 to 10 ‘a’ is increased from 0 to 10. Curly braces are not necessary. Default
Printf(“%d”.a); scope of the ‘for’ loop is one statement after for loop.
Page | 26
Introduction to Programming
printf("\n\n\n");
for(i=0;i<=127;i++)
{
if(t==6)
{
printf("\n\n");
t=0;
}
printf("%d--%c\t",i,i);
t++;
}
getch();
}
Output:
0--> 1-->☺ 2-->☻ 3-->♥ 4-->♦ 5-->♣
6-->♠ 7--> 8-- 9--> 10-->
11-->♂
14-->♫ 15-->☼ 16-->► 17-->◄
18--> 19--> 20-->¶ 21-->§ 22--> 23-->
Page | 27
Introduction to Programming
Page | 28
Introduction to Programming
printf("%d",j);
}
printf("\n");
}
getch();
}
output:
Enter n value : 6
1
12
123
1234
12345
123456
Page | 29
Introduction to Programming
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k;
clrscr();
for(i=5;i>=1;i--)
{
for(j=i;j>=1;j--)
{
printf("%d",j);
}
printf("\n");
for(k=5;k>=i;k--)
{
printf(" ");
}
}
getch();
}
OUTPUT
54321
4321
321
21
1
Page | 30
Introduction to Programming
AB
ABC
ABCD
ABCDE
Page | 31
Introduction to Programming
Page | 32