[go: up one dir, main page]

0% found this document useful (0 votes)
125 views189 pages

Pps Unit 2

The document discusses various programming concepts in C language including variables, data types, constants, and character sets. It explains different data types like integers, floating point numbers, characters, and strings. It also describes various constant types like numeric, character, and string constants. Symbolic constants and their use with preprocessor directives is demonstrated. Variables in C are introduced along with their declaration and initialization. Keywords, identifiers, and tokens in C are defined. Examples of C programs are provided to illustrate various concepts.

Uploaded by

afakraipur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
125 views189 pages

Pps Unit 2

The document discusses various programming concepts in C language including variables, data types, constants, and character sets. It explains different data types like integers, floating point numbers, characters, and strings. It also describes various constant types like numeric, character, and string constants. Symbolic constants and their use with preprocessor directives is demonstrated. Variables in C are introduced along with their declaration and initialization. Keywords, identifiers, and tokens in C are defined. Examples of C programs are provided to illustrate various concepts.

Uploaded by

afakraipur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 189

Unit 2 Programming Concepts

Syllabus

•Variables,
•data types,
•memory locations,
•Syntax and Logical Errors in compilation,
•object and executable code,
•Arithmetic expressions and precedence,
•Conditional Branching and Loops: Writing and evaluation of
conditionals and consequent branching, Iteration and loops.
Character Set
 The Character that can be used to form words, numbers and
expression depends upon the computer on which the program
runs. Characters in C are classified into 4 groups:
I. Alphabets- A to Z , a to z
II. Digits-0 ,1,2…..9
III. Special characters: + -
IV. Escape Sequences/Backslash Constants/ White Space
Alphabets
C language supports all the alphabets from the English language.
Lower and upper case letters together support 52 alphabets.

Lower case letters - a to z


Upper case letters - A to Z

salary
SALARY
Digits

C language supports 10 digits which are used to


construct numerical values in C language.

Digits - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Special characters
Escape Sequences/Backslash
Constants/ White Space
•Characters are printed on the screen through the keyboard
but some characters such as newline, tab, backspace cannot
be printed like other normal characters.
• C supports the combination of backslash (\) and some
characters from the character set to print these characters.
Program 1
#include <stdio.h>
int main(void)
{
printf("My mobile number is 12345789\a\a");
printf("\n");
printf("Hello\n ");
printf("world"); Output:
printf("\n");
printf("Hello world\b\b\b\bF");
printf("\n");
printf("H\tworld");
printf("\n");
printf("Hello fri\rends");

return 0;
}
C Tokens

C tokens are the basic buildings blocks in C language which are constructed
together to write a C program.
Each and every smallest individual units in a C program are known as C
tokens.
Keywords
auto double int struct
•There are certain words
that are reserved for doing break else long switch
specific tasks.
• These words are known as case enum register typedef
keywords and they have
char extern return union
standard, predefined
meaning in C. const float short unsigne
•They are always written in d
lowercase. continue for signed void
•They are only 32
default go to size of volatile
keywords available in C
which are given - do if static while

Identifiers
 Identifiers" are the names you supply for variables, types,
functions, and labels in your program.
 Identifier names must differ in spelling and case from any
keywords.
 You cannot use keywords as identifiers; they are
reserved for special use.
Rules for naming identifiers are given below:-
i. First character must be an alphabet (or underscore).
ii. Must consist of only letters, digits or underscore.
iii. Only first 31 characters are significant.
iv. Cannot use a keyword.
v. Must not contain white space .
Constants
Constant is a value that cannot changed during the execution of a
program.
Numeric constant
Numeric constants consist of numeric digits, they may or may
not have decimal point.
These are rules for defining numeric constants-

I. Numeric constant should have at least one digit.


II. No comma or space is allowed within the numeric constant.
III.Numeric constants can either be positive or negative but
default sign is always positive.
There are two types of numeric constants-
I. Integer constants
II. Real( Floating Point) constants
Integer constant
•Integer constants are whole numbers which have no decimal
point.
•There are three types of integer constants based on different
number systems.
• The permissible characters that can be used in these constants
are-
•Decimal constants-0,1,2,3,4,5,6,7,8,9 (base 10)
•Octal constants-0,1,2,3,4,5,6,7 (base 8)
•Hexdecimalconstants-0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,a,b,c,d,e,f (base 16)
Decimal Octal integer Hexadecimal
integer constants are- integer constants
constants are- are-
•0 In octal integer In hexadecimal
•123 constants, first digit integer constants,
•3705 must be 0. first two characters
•23759 For example- should be Ox or OX.
0 For example-
05 ox
OX23
Real(floating point)Constants
Floating point constants are numeric constants that contain decimal point.
Some valid floating point constants are-
0.5
5.3
4000.0
0.0073
5597
•For expressing very large or very small real constants, exponential
(scientific) form is used. Here, the number is written in the mantissa
and exponent form, which are separated by ‘e‘ or 'E'.
•The mantissa can be an integer or a real number, while the exponent can
be only an integer (positive or negative).
• For example the number 1800000 can be written as 1.8e6, here 1.8 is
mantissa and 6 is the exponent.
Real(floating point)Constants

Some more examples are as-


Program 2
#include <stdio.h>
int main()
{
int value=256;
float f=0.7;
printf("Decimal value is: %d\n",value);
printf("Octal value is: %o\n",value);
printf("Hexadecimal value in small letters): %x\n",value);
printf("Hexadecimal value in capital letters): %X\n",value);
printf("%f\n",f);
return 0;
}

Output:
Octal System
•Octal became widely used in computing when systems such as
the UNIVAC employed 6-bit, 12-bit, 24-bit or 36-bit words.

• Octal was an ideal abbreviation of binary for these machines


because their word size is divisible by three (each octal digit
represents three binary digits).
Hexadecimal Number System
•The one main disadvantage of binary numbers is that
the binary string equivalent of a large decimal base-10
number can be quite long.

•The Hexadecimal, or Hex, numbering system is


commonly used in computer and digital systems to
reduce large strings of binary numbers into a sets of
four digits for us to easily understand.

•The word “Hexadecimal” means sixteen because this


type of digital numbering system uses 16 different
digits from 0-to-9, and A-to-F.
Single Character Constants
Single Character constant is a single character that is enclosed
within single quotes. Some valid character constants are: '9‘ 'D‘
'$' Some invalid character constants are
String Constants
A string constant has zero, one or more than one
character.
A string constant is enclosed within double quotes(“ ").
At the end of string,\0 is 'automatically placed by the
compiler.
Some examples of string constants are-
"Kumar“
"593“
"8“
"A“
Program 3
#include <stdio.h>
int main()
{
const char c='m';
const char s[]="minu";
printf("%c\n",c);
printf("%s\n",s);
return 0;
}

Output:
Program 4
#include <stdio.h>
void main()
{
const int height = 100; /*int constant*/
const float number = 3.14; /*Real constant*/
const char letter = 'A'; /*char constant*/
const char letter_sequence[10] = "ABC"; /*string constant*/
printf("value of height :%d \n", height );
printf("value of number : %f \n", number );
printf("value of letter : %c \n", letter );
printf("value of letter_sequence : %s \n", letter_sequence);
}
Output:
Symbolic consonants
•A symbolic constant is a name that substitutes a numeric, a
character constant or a string constant.
•For example assume that we are computing the area of a circle
using the formula, area = 3.14 * redius *radius
•In this formula , the numeric constant 3.14 can be replicated by
symbolic constant named pie.
•Thus, the formula takes a new form as
•area = pie * redius * redius;
•Here, we substituted pie for a numeric consonant. During
compile-time, each occurrence of pie is replaced by its value
defined. Symbolic constants are defined using preprocessor
statement #define.
•Example:
•#define pie 3.14
•#define Name “PRABHU”
Variables
•Variable is a name that can be used to store values.
Variables can take different values but one at a time.
•These values can be changed during execution of the
program.
• A data type is associated with each variable.
•The data type of the variable decides what values it can
take.
• The rules for naming variables are same as that for naming
identifiers.
Declaration of Variables
•It is must to declare a variable before it is used in the program.
Declaration of a variable specifies it & name and data type.
•The type and range of values that a variable can store depends upon its
data type.
The syntax of declaration of a variable is--
•data type variable name;
Here data type may be int, float, char, double etc.

Some examples of declaration of variables are-


•Int x;
•float salary;
•Char grade;
Here x is a variable of type int, salary is a variable of type float, and
grade is a variable of type char.
We can also declare more than one variable in a single declaration.
•For example-int x,y,z,total;

Here x, y, z, total are all variables of type int.


Initialization of variables
When a variable is declared it contains undefined value
commonly known as garbage value.
If we want we can assign some initial value to the variable
during the declaration it self, this is called initialization of the
variable.
For example-
int a=5;
float x =8.9,Y =10.5;
char ch='y';
Double num=0.15197e-7;
int l, m, n, total=0;
Multiple Assignment Statement
•The process of giving initial values to variable is known as
initialization. We can initialize more than one variable in a
single statement. This type of initialization would need more
number of assignment operators.
•Hence, the name multiple assignment statements.
•Example: -
•int x=y=z=10;
•float a=b=c=d=5.75;
Data Types
Data type indicates 4 things. They are:
i. Types of data can be stored in a variable or a constant
ii. Amount of memory to be allocated for a specific type of
data
iii. Possible range of values can be stored in a specific type of
a variable or a constant
iv. Type of operations can be performed on a specific type of
data
Primitive data types

There are four fundamental data types in C, which are

• int: It is used to store integer value,


•char: It is used to store any single character
•float : It is used for storing single precision floating point
number
•Double: It is used for storing double precision floating
point number.
Difference between ‘float’ and ‘double’
Though Float and Double both of them are used for assigning real (or decimal)
values in programming there is a major difference between these two data types.

2.4478889999999999
Types of operations performed

int, float,double: +, -,*, /


char: tolower(), toupper()
Data Types continued……………..
We can use type qualifiers(modifiers) with these basic
types to get some more types.
Modifiers are used to modify properties of basic data types.
There are two types of type qualifiers-
1.Size qualifiers
i. short,
ii. long
2.Sign qualifiers
i. signed,
ii. Unsigned
Sign qualifiers

i. Unsigned: When the qualifier unsigned is used the number


is always positive
ii. Signed: when signed is used number may be positive or
negative.
Note: If the sign qualifier is not mentioned, then by default
signed qualifier is assumed. The range of values for signed
data types is less than that of unsigned type. This is because
in signed type, the leftmost bit is used to represent the sign,
while in unsigned type this bit is also used to represent the
value.

unsigned int a=-45;


Size qualifiers
•The size qualifier in c language is used to alter the size of a
primitive data type. C supports two size qualifiers, short and
long.
•The size qualifier is generally used with an integer type in
addition, double type supports long qualifier.
Qualifiers
Example
Range of values
•Signed: -(2n-1 ) To + (2n-1 -1)
•Unsigned: 0 To (2n – 1)
Where n is the number of bits of the data type.

00000000 to 11111111
Range and amount of memory used
Range and amount of memory used
Program 5
//Program to show how characters are stored as signed
or unsigned characters
#include<stdio.h>
int main()
{
unsigned char char1=255;
signed char char2=-1;
printf("%d\n", char1);
printf("%d", char2);
}

Output:
Program 6
//Program to show overflow range
#include<stdio.h>
int main()
{
unsigned char char1=256; //out of range
signed char char2=-1;
printf("%d\n", char1);
printf("%d", char2);
}

Output:
Format Specifiers in C
 The format specifier is used during input and output. It is a
way to tell the compiler what type of data is in a variable
during taking input using scanf() or printing using printf().
Here is a list of format specifiers.

 Int a=7;
 Signed int b=-8;
 Unsigned int c=9;
 Short signed int e =
 Short unsigned int f=

 Long signed int


 Long unsigned int
 Printf(“the value of int =%d\n”, a);
scanf() function
To read the values for the variables in a program form the
keyword, C provides a function called scanf(). It is included in
stdio.h. The general format of scanf() is as follows:
scanf(“Control String”, Address_list);

Where
Control String :- The control string specifies the type of the
values which are to be supplied to the variables.
Address_list:- Address of memory location where the value of
the input variables should be stored.
Example:
scanf(“%d”,&x);
scanf(“%f”,&y);
scanf(“%c”,&z);
scanf(“%s”,&name);
scanf(“%d %d”,&a,&b);
scanf(“%d %f %c”, &a,&b,&name);
printf() function
C provides the printf() function to display the data on
the monitor. The printf() is included in stdio.h.
The general form of printf() statement is
printf(“control string”, varlist);
where,
control string – Specifies the type and format of the
values to be displayed.
varlist - A list of variable to be displayed.

Example:

printf(“Programmingis an art”);
printf(“%d”,number);
printf(“%f%f”, p,q);
printf(“Sum of threenumber=%d”,sum);
Program 7
#include <stdio.h>
int main()
{
char str[] = "geeksforgeeks";
printf("%20s\n", str);
printf("%-20s\n", str);
printf("%20.5s\n", str);
printf("%-20.5s\n", str);
return 0;
}
Output:
Operator and Expressions

 An operator is a symbol that tells the computer to perform


certain manipulations.
 An expression is a sequence of operands and operators that
reduces to a single value.
 C operators can be classified into a number of categories.
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Increment and decrement operators
6. Conditional operators
7. Bitwise operators
8. Special operators

51
52
1. Arithmetic operators
The arithmetic operators in C
Operations Operator Precedence/Priori Associativity
ty
Addition + 2 L to R
Subtraction - 2 L to R
Multiplication * 1 L to R
Division / 1 L to R
Modulus % 1 L to R

The priority indicates which term is to be evaluated and associativity indicates


the order in which the terms are evaluated.

Note:,
•Integer division truncates remainder
•The % operator cannot be applied to a float or
double.
53
1. Arithmetic expressions
An arithmetic expression is a combination of variables,
constants, and operators.
For example,
a*b-c → a*b-c
(m+n)(x+y) → (m+n)*(x+y)
ax2+bx+c → a*x*x+b*x+c
Program 8
#include<stdio.h>
void main()
{
int n1,n2;
int sum, diff, prod, quotient, remainder; n1=5, n2=3;
sum = n1+n2;
diff = n1-n2;
prod=n1*n2;
quotient= n1/n2;
remainder= n1%n2;
printf("Sum = %d\n" ,sum);
printf("Difference=%d\n",diff);
printf("Product =%d\n",prod); Output:
printf("Quotient=%d\n",quotient);
printf("Remainder =%d\n",remainder);
}
55
2. Relational Operators
• Relational operators are used to compare values of two
expressions depending on their relations.
• An expression that contains relational operators is called
relational expression.
•If the relation is true then the value of relational expression is 1
and if the relation is false then the value of expression is 0. The
relational operators are:
Operator Meaning
< less than
<= less than or equal to
> greater than
>= greater than or equal to
== equal to
!= not equal to
Program 9
#include<stdio.h>
int main()
{
int a,b;
printf("enter values of a and b");
scanf("%d%d",&a,&b);
if(a<b)
printf("%d is less than %d\n", a, b);
if(a<=b)
printf("%d is less than or equal to %d\n", a,b);
if(a==b)
printf("%d is equal to %d\n",a,b); Output:
if(a!=b)
printf("%d is not equal to %d\n",a,b);
if(a>b)
printf("%d is greater than %d\n",a,b);
if (a>=b)
printf("%d is greater than or equal to %d\n",a,b);
}
3. Logical operators
•An expression that combines two or more expressions is termed as a
logical expression.
•For combining these expressions we use logical operators.
•These operators return 0 for false and 1 for true.
•The operands may be constants, variables or expressions. C has three
logical operators.

57
Program 10
#include<stdio.h>
int main()
{
int a,b,c,d;
a=4,b=3;
c=(a && b);
b=a||b||c;
d=!(a && b);
printf("%d %d %d",c, b,d);
}
Output:
4. Assignment operators
C provides a compact way of writing assignment
statement in an expression.
This is basically associated with all arithmetic operators
and bitwise operator.
Operators Assignments Shorthand
assignment
+
a=a+b a+=b;
-
a=a-b a-=b;
*
a=a*b a*=b;
/
a=a/b a/=b;
%
a=a%b a%=b;
&
a=a&b a&=b;
|
a=a|b a/=b;
^
a=a^b a^=b;
<< a=a<<b a<<=b;
>> a=a>>b a>>=b; 59
Program 11
#include <stdio.h>
int main()
{
int a = 10;
printf("Value of a is %d\n", a);
a += 10; //a=a+10
printf("Value of a is %d\n", a);
Output:
a -= 10; //a=a-10
printf("Value of a is %d\n", a);
a *= 10; //a=a*10
printf("Value of a is %d\n", a);
a /= 10; a=a/10
printf("Value of a is %d\n", a);
return 0;
}
5. Increment and decrement operators

 C provides two unusual operators for incrementing and decrementing


variables.
 The increment operator ++ adds 1 to its operand, while the decrement
operator -- subtracts 1.
 The unusual aspect is that ++ and -- may be used either as prefix
operators (before the variable, as in ++n), or postfix operators (after the
variable: n++).
 In both cases, the effect is to increment n. But the expression ++n
increments n before its value is used, while n++ increments n after its
value has been used.

 Int a=7
 ++a; //prefix
 a++; //post
 --a; //prefix
 a-- //postfix

61
Program 12
#include<stdio.h>
main( )
{
int x=8;
printf("x=%d\n",x);
printf ("x=%d\n",++x);
printf ("x=%d\n",x);
printf ("x=%d\n",--x);
printf ("x=%d\n",x);
printf("x=%d\t" ,x) ;
printf ("x= %d\t",x++);
printf("x=%d\t",x);
printf("x=%d\t" ,x--);
printf ("x=%d\n" ,x) ;
}
Output:

62
6. Conditional operator(ternary)
•There is only one such operator in C. It takes three operands.
•The symbol ? Is used as a ternary operator in C.
•The general form of a ternary expression is as follows:
Test_expression ? statement1: statement2

From the above syntax, If the given test condition is true, then it
returns statement1, and if it is false, statement2 will return.
C = a<b? a:b;
Here C will be assigned the value of a if a is less than b. Otherwise
it will be assigned the value of b.
Program 13
#include<stdio.h>
int main()
{
int age;
printf(" Please Enter your age here: \n ");
scanf("%d",&age);
(age>=18) ? printf("You are eligible to Vote"):printf(" You are not eligible to Vote ");
return 0;
}

Output:
Program 14
#include<stdio.h>
int main()
{
int num1, num2, max;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
max = (num1 > num2) ? num1 : num2;
printf("Maximum of %d and %d is= %d", num1, num2,
max);
return 0;
}

Output:
Program 15
//Program to Check Odd or Even Using the Ternary Operator

#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
(num % 2 == 0) ? printf("%d is even.", num) : printf("%d is odd.", num);
return 0;
}
Output:
Program 16
//C program to check leap year using conditional operator
#include <stdio.h>
int main()
{
int year;
printf("Enter any year: ");
scanf("%d", &year);

(year%4==0)? printf(“leap year”): printf(“non leap year”);

(year%4==0 && year%100!=0) ? printf("LEAP YEAR"):(year%400


==0 ) ? printf("LEAP YEAR") : printf("COMMON YEAR");
return 0;
}
Output:
7. Bitwise Operators

•All data items are stored in the computer‘s memory as


a sequence of bits (o‘s and 1‘s) and some applications
need the manipulation of these bits.
•To perform, the bitwise operations, C provides six
operations.
• These operators work with int and char type data.
They cannot be used with floating point numbers.

68
70
Program 17
#include <stdio.h>
int main()
{
int a = 12, b = 25;
printf("Bitwise AND = %d\n", a&b);
printf("Bitwise OR = %d\n", a|b);
printf("Bitwise 1's complement = %d\n", ~35);
printf("Bitwise xor = %d\n", a^b);
printf("Bitwise right shift = %d\n", 212>>7);
printf("Bitwise left shift = %d\n", 212<<4);
return 0;
}
Output:
8. Special operators

C provides the following special characters


•Comma Operator
•Sizeof()
•Address operator
•Dereferencing Operator
•Dot Operator
•Arrow Operator

77
Comma Operator
•It is used to link two or more related expression
together.
•In such a case, the expressions are evaluated in a left to
right fashion and we can obtain the value of the
combined expression from the value of the right most
expression.

Example:-
Sum=(a=12,b=22,a+b)
Here are three expressions a =12, b=22, and a+b
The value of the rightmost expression (i.e. a+b = 12+22
=34) is set to sum.
Sizeof() Operator

•The sizeof() operator returns the size of the operand.


•It should be written in lowercase letters.
•It must precede its operand.
•The operand may be constant, a variable or a data type.
•It is normally used to determine the size of arrays and
structures.
Syntax:
sizeof(operand);
Example :
X= sizeof(int); Y=sizeof(sum);
Program 18
#include<stdio.h>
int main()
{
int a;
char b;
float c;
double d;
printf("Storage size for int data type:%d \n",sizeof(a));
printf("Storage size for char data type:%d \n",sizeof(b));
printf("Storage size for float data type:%d \n",sizeof(c));
printf("Storage size for double data type:%d\n",sizeof(d));
return 0;
}

Output:
Address operator(&)

int a; 1000-1003
This is used to get the address of the variable.
Example : &a will give address of a.

Program 19
#include<stdio.h>
void main( )
{
int age=30;
float sal=1500.50;
printf ("Value of age=%d,Address of age= %u\n",age,&age);
printf ("Value of sal=%f, Address of sal= %u\n",sal,&sal);
}
Output:
Dereferencing Operator(*)

This is used as pointer to a variable.


Example : * a where, * is pointer to the variable a.
Program 20
#include <stdio.h>
int main()
{
int *ptr, q;
q = 50;
/* address of q is assigned to ptr */
ptr = &q;
/* display q's value using ptr variable */
printf("%d", *ptr);
return 0;
}

Output:
Dot(.) operator And Arrow operator

•The Dot(.) operator is used to normally access


members of a structure or union.
•The Arrow(->) operator exists to access the
members of the structure or the unions using
pointers.
Precedence of operators
Associativity of operators
Operator Precedence and Associativity
Determine the hierarchy of operations
Type Casting/ Type Conversion
•The process of converting one predefined type into another is
called type conversion. The C language type casting is of two
types:

•Implicit type conversion


•Explicit type conversion
Implicit type conversion
•Also known as ‘automatic type conversion’. Done by the
compiler on its own, without any external trigger from the user.
•Generally takes place when in an expression more than one data
type is present.
•Type conversion takes place to avoid lose of data. All the data
types of the variables are upgraded to the data type of the variable
with largest data type.
• For example, if we have an integer data type value and a double
data type value in an expression then C will automatically convert
integer type value to double in order to evaluate the expression.
Hierarchy
Program 21
/ An example of implicit conversion
#include<stdio.h>
int main()
{
int x=10; // integer x
char y= 'a'; // character c
x = x + y; // y implicitly converted to int. ASCII value of 'a' is 97
float z = x + 1.0; // x is implicitly converted to float
printf("x = %d, z = %f", x, z);
return 0;
}
Output:
Explicit Type Conversion
•This process is also called type casting and it is user defined.
•Here the user can type cast the result to make it of a
particular data type.

• The syntax for explicit type conversion or type casting is


•(type)expression

•Where, type is any of the type we want to convert the


expression into.
Program 22
/ /An example of explicit conversion

#include <stdio.h>
int main(void)
{
//variables
float x = 24.5,y = 7.2;
int result = (int) x / (int) y; //converting float to int
//output
printf("Result = %d\n", result);
return 0;
}

Output:
Programs
1. Write a program which asks the user to input the marks of physics, chemistry
and math. Then calculate the percentage and print onto the screen.
2. Write a program to find out the equation of a straight line passing through
the points (x1,y1) and (x2,y2).
3. Write a C program by using conditional operator to find maximum out of 3
given numbers.
4. Write a c program to find the area of a triangle if its three sides are given.
[formula: area=
5. In a town, the percentage of men is 52. The percentage of total literacy is 48.
If the total percentage literate men is 35 of the total population. Write a
program to find the total number of illiterate men and woman if the
population of the town is 80000.
6. Write a program for taking input distance between two cities in kilometers
and convert it into meters and centimeter, feets and inches.
7. Write a program to find out the roots of a quadratic equation.
8. C program to swap two numbers using third variable. a=6 b=7
Control statement
•A computer program is a set of instructions for a computer.

•These instructions, also known as statements, are executed


sequentially, i.e. one after the other in a program.

•This order of executing the statement works well for simple


problems where neither any decision making process is
involved nor any repetition of statements is involved.

•The C Language provides facilities for controlling the order


of execution of the statements, which are referred to as flow
control statements or simply as control statement.
Flow Control Statements
The various flow control statements are:
1. Decision Making Statements:
•Simple if statement
•If….else statement
•Nested if…..else statement
•else if ladder
•switch statement
2. Looping Statements:
•for statement
•while statement
•do-while statement
3. Jumping Statements:
•break statement
•continue statement
•goto statement
Simple If Construct
The general format of a simple if statement is

There can be a single statement or block of statements after the if part.


Flowchart

Here if the condition is true( non zero) then statement 1 is executed,


and if it is false(zero), then the next statement which is immediately
after the if control statement is executed.
Program 23
Program to understand the if statement.
#include<stdio.h>
void main()
{
int a=9;
if (a==10)
{
printf(“TRUE”);
}
printf(“Have A Nice Program!!!”);
}

Output:
Program 24
Program to understand the if statement.
#include<stdio.h>
void main()
{
int a=100;
if (a==10)
{
printf(“TRUE”);
}
printf(“Have A Nice Program!!!”);
}

Output:
Program 25
Program to find out the largest of two numbers.
#include <stdio.h>
int main()
{
int n1, n2;
printf("Enter two different numbers: ");
scanf("%d %d", &n1, &n2);
if( n1>n2)
{
printf("%d is the largest number.", n1);
}
if( n2>n1)
{
printf("%d is the largest number.", n2);
}
return 0;
}
Output:
Program 26
Program to find out the largest of three numbers.
#include <stdio.h>
int main()
{
int n1, n2, n3;
//printf("Enter three different numbers: ");
//scanf("%d %d %d", &n1, &n2, &n3);
if( n1>n2 && n1>n3 )
{
printf("%d is the largest number.", n1);
}
if( n2>n1 && n2>n3 )
{
printf("%d is the largest number.", n2);
}
if( n3>n1 && n3>n2 )
{
printf("%d is the largest number.", n3);
}
return 0;
}
Output:
If-else Construct
Syntax: Example: a=5,b=2
if (expr) if (a<b)
statement1; {
else printf(“b is bigger than a”);
statement2; }
else
{
Printf(“a is bigger than b”);
}
Flowchart
Program 27
Program to find out the largest of two numbers.
#include <stdio.h>
int main()
{
int n1, n2;
printf("Enter two different numbers: ");
scanf("%d %d", &n1, &n2);
if( n1>n2)
{
printf("%d is the largest number.", n1);
}
else
{
printf("%d is the largest number.", n2);
}
return 0;
}
Output:
Program 28
Program to print whether the number is even or odd
#include <stdio.h>
int main()
{
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if(num % 2 == 0)
{
printf("%d is even.", num);
}
else
{
printf("%d is odd.", num);
}
return 0;
}
Output:
Else-if Construct
This is a type of nesting in which there is an if ….else statement
in every else part except the last else part. This type of nesting is
frequently used in programs and is also known as else if ladder.
Flowchart
Problem
Program to find out the grade of a student when the marks of 4 subjects
are given. The methods of assigning grades are given as
Program 29
#include<stdio.h>
main()
{
float m1,m2,m3,m4,total, per;
char grade;
printf("enter marks of 4 subjects:");
scanf("%f%f%f%f",&m1,&m2,&m3,&m4);
total=m1+m2+m3+m4;
per=total/4;
if(per>=85)
grade='A';
else if(per>=70)
Output:
grade='B';
else if(per>=55)
grade='C';
else if(per>=40)
grade='D';
else
grade='E';
printf(“TOTAL = %f Percentage is %f\nGrade is
%c\n",total ,per,grade);
}
Nested if Construct
Flowchart
Program 30
//Program to find largest number from three given numbers
#include<stdio.h>
int main()
{
int a,b,c,large;
printf("enter three numbers:");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
if(a>c)
Output:
large=a;
else
large=c;
}
else
{
if(b>c)
large=b;
else
large=c;
}
printf("largest number is %d\n",large);
}
Switch Construct
This is a multi-directional conditional control statement.
Sometimes there is a need in program to maintain choice
among number of alternatives. For making this choice, we use
the switch statement. This can be written as:
Switch Construct
•Here switch, case and default are keywords.
• The “expression” following the switch keyword can be any C expression
that yields an integer value.
• It can be
•value of any integer or character variable, or
•a function call returning an integer, or
•an arithmetic, logical , relational, bitwise expression yielding an
integer
•any integer or character constant also.
•The constants following the case keywords should be of integer or
character type.
•Each case can be followed by any number of statements. It is also possible
that a case has no statement under it. If a case is followed by multiple
statements, then it is not necessary to enclose them within pair of curly
braces, but it is not an error if we do so.
•The statements under case can be any valid C statements like if else, while,
for or even another switch statement. Writing a switch statement inside
another is called nesting of switches.
Valid and invalid ways of writing switch
Flowchart
Program 31
switch(choice)
#include<stdio.h>
void main( )
{
{ case 1: c = a + b;
int a, b, c, choice; printf("\n%d", c);
printf("\n 1. Press 1 for addition\n 2. break;
Press 2 for subtraction\n 3. Press 3 for case 2: c = a - b;
subtraction \n 4. Press 4 for printf("\n%d", c);
subtraction"); break;
printf("\nEnter 2 numbers"); case 3: c = a * b;
scanf("%d%d", &a, &b);
printf("\n%d", c);
printf("\n Enter your choice");
scanf("%d", &choice);
break;
case 4: c = a / b;
printf("\n%d", c);
Output: break;
default:
printf("\nyou have passed a
wrong key");
printf("\n press any key to
continue");
}}
Program 32
Program to print day of week name
#include <stdio.h>
int main()
{
int week;
printf("Enter week number(1-7): ");
scanf("%d", &week);
switch(week)
{
case 1: printf("Monday"); break;
case 2: printf("Tuesday"); break;
case 3: printf("Wednesday"); break;
case 4: printf("Thursday"); break;
case 5: printf("Friday"); break;
case 6: printf("Saturday"); break;
case 7: printf("Sunday"); break;
default: printf("Invalid input! Please enter week number between 1-7.");
}
return 0;
} Output:
Program 33
Program to find number of days in a case 2:
month. days=28; break;
#include <stdio.h> default:
int main(){ days=0; break;
int month; }
int days; if(days)
printf("Enter month: "); printf("Number of days in %d month is:
scanf("%d",&month); %d\n",month,days);
switch(month) else
{ printf("You have entered an invalid
case 4: month!!!\n");
case 6: return 0;
case 9: }
case 11:days=30; break;
case 1: Output:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:days=31; break;
#include <stdio.h>
Program 34
int main()
{
Output:
char ch;
printf("Enter a character: ");
scanf("%c",&ch);
switch(ch)
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf("%c is a VOWEL.\n",ch); break;
default: printf("%c is a CONSONANT.\n",ch);
} }
Looping Statements
• Loops are used when we want to execute a part of the program or a
block of statements several times.
• For example, suppose we want to print “C is the best” 10 times. One
way to get the desired output is-we write 10 printf statements, which
is not preferable. Other way out is-use loop.
• Using loop we can write one loop statement and only one printf
statement, and this approach is definitely better than the first one.
• With the help of loop we can execute a part of the program
repeatedly till some condition is true.
• There are three loop statements in C-

• While statement
• Do-while statement
• For statement
While Constructs
While Constructs
•First the condition is evaluated; if it is true then the
statements in body of loop are executed.
•After the execution, again the condition is checked
and if it is found to be true then again the statement in
the body of loop are executed.
•This means that these statements are executed
continuously till the condition is true and when it
becomes false, the loop terminates and the control
comes out of the loop.
•Each execution of the loop body is known as
iteration.
Program 35
//Program to print the numbers from 1 to 10 using while loop.
#include <stdio.h>
int main()
{
int i=1;
while(i<=10)
{
printf("%d\t",i);
i=i+1;
}
}
Output:
Program 36
//Program to print the numbers in reverse order with a difference of 2 using while loop..
#include <stdio.h>
int main()
{
int k=10;
while(k>=2)
{
printf("%d\t",k);
k=k-2;
}
}
Output:
Program 37
//Program to print the sum of digits of any number using while loop.
#include<stdio.h>
void main() n=123
{ Ist iteration
123%10=3
int n,sum=0,rem;
sum=0+3=3
printf("enter the number"); n=123/10=12
scanf("%d", &n);
while(n>0) 2nd iteration
{ rem=12%10=2
rem=n%10; sum=3+2=5
sum=sum+rem; n=12/10=1
n=n/10;
} 3rd iteration
rem=1%10=1
printf("sum of digits=%d\n",sum);
sum=5+1=6
} n=1/10=0
Output:
Program 38
//Program to print the product of digits of any number using while loop.
#include<stdio.h>
void main()
{
int n,prod=1,rem;
printf("enter the number");
scanf("%d", &n);
while(n>0)
{
rem=n%10;
prod=prod*rem;
n=n/10;
}
printf("Product of digits=%d\n",prod);
}
Output:
Program 39
//Program to find the factorial of any number using while loop.
#include<stdio.h>
void main()
{
int n,num;
long fact=1;
Factorial 4
printf("enter the number");
scanf("%d", &n); 4*3*2*1
num=n;
while(n>=1) Fact=4*3*2*1
{
fact=fact*n;
n--;
}
printf("Factorial of %d =%ld\n",num,fact);
}
Output:
Do-while Construct
The ‘do-while’ statement is also used for looping. The body of
this loop may contain a single statement or a block of
statements. The syntax for writing this loop is:
Flowchart
Difference between while and do-while
• Here firstly the statements inside loop body are executed and
then the condition is evaluated. (do-while)
• If the condition is true, then again the loop body is executed and
this process continues until the condition becomes false.
• Note that unlike while loop, here a semicolon is placed after the
condition.
• In a ‘while loop, first the condition is evaluated and then the
statements are executed whereas in, do while loop, first the
statements are executed and then the condition is evaluated.
• So if initially the condition is false the while loop will not
execute at all, whereas the do while loop will always execute at
least once.
Program 40
//Program to print the numbers from 1 to 10 using do-while loop.

#include<stdio.h>
void main()
{
int i=1;
do
{
printf("%d\t",i);
i=i+1;
}
while(i<=10);
}

Output:
Program 41
//Program to print table for the given number using do while loop
#include<stdio.h>
int main(){
int i=1,number;
printf("Enter a number: ");
scanf("%d",&number);
do{
printf("%d \n",(number*i));
i=i+1;
}while(i<=10);
return 0;
}
Output:
Program 42
//Program to print numbers between 1 and 100 which are multiple of 3 using
the do while loop:
#include<stdio.h>
int main()
{
int i = 1;
do
{
if(i % 3 == 0)
{
printf("%d ", i);
}
i=i+1;

}while(i < 100);


}
Output:

}while(i < 100);


}
For Construct
The ‘for’ statement is very useful while programming in C. It has
three expressions and semicolons are used for separating these
expressions. The ‘for’ statement can be written as-

OR
For Construct
The loop body can be a single statement or block of statements.
expression1--> it is an initialization expression. It is executed
only once when the loop starts and is used to initialize the loop
variables. This expression is generally an assignment
expression.
expression2-->it is a test expression or condition and is tested
before each iteration of the loop. This condition generally uses
relational and logical operators.
Expression3-->it is an update expression and is executed each
time after the body of the loop is executed.
Flowchart
Working

•Firstly the initialization expression is executed and the loop


variables are initialized, and then the condition is checked, if
the condition is true then the body of the loop is executed.
•After executing the loop body, control transfers to expression3
and it modifies the loop variables and then again the condition
is checked, and if it is true, the body of loop is executed.
•This process continues till the condition is true and when the
condition becomes false the loop is terminated and control is
transferred to the statement following the loop.
Example for For Construct
#include<stdio.h> #include<stdio.h>
int main() int main()
{ {
int i; int i;
for (i = 0; i < 10; i++) for (i =20; i >10; i--)
{ {
printf ("Hello\n"); printf ("Hello\n");
printf ("World\n"); printf ("World\n");
} i--;
return 0; }
} return 0;
}
Program 43
//Write a program in C to display the first 10 natural numbers.
#include <stdio.h>
void main()
{
int i;
printf("The first 10 natural numbers are:\n");
for (i=1;i<=10;i++)
{
printf("%d ",i);
}
printf("\n");
}
Output:
Program 44
//Write a C program to find the sum of first 10 natural numbers.
#include <stdio.h>
void main()
{
int j,
sum = 0;
printf("The first 10 natural number is :\n");
for (j = 1; j <= 10; j++)
{
sum = sum + j;
printf("%d ",j);
}
printf("\nThe Sum is : %d\n", sum);
}

Output:
Program 45
//Program to generate fibonacci series 1 1 2 3 5 8 13 34
…………………………using for loop.
#include <stdio.h>
int main()
{
int i, n, num1 = 0, num2 = 1, num3;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series:\n");
printf("%d\n",num2);
for (i = 1; i <= n; i++)
{
num3 = num1 + num2;
printf("%d\n", num3);
num1 = num2; num2 = num3;
}
}
Output:
Program 46
Program to check whether the entered number is prime or not..
#include <stdio.h>
void main()
{
int n, i, c = 0;
printf("Enter any number n:");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
if (n % i == 0)
{
c=c+1;
}
}
if (c == 2)
{
printf("%d is a Prime number",n);
}
Else
{ printf("%d is not a Prime number", n);
}} Output:
Program 47
//Program to print all the prime number between a given range.
#include <stdio.h>
int main()
{
int low, high, i, flag;
printf("Enter two numbers(intervals): ");
scanf("%d %d", &low, &high);
printf("Prime numbers between %d and %d are: ", low, high);
while (low < high)
{
flag = 0;
for(i = 2; i < low; i++)
{
if(low % i == 0)
{
flag = flag+1;
}
}
if (flag == 0)
printf("%d ", low);
low=low+1;
}
return 0;
}
Program 48
Program to Calculate the Power of a Number.
#include<stdio.h>
int main()
{
int base, exp, result=1,i;
printf("enter the base and exponent");
scanf("%d%d",&base,&exp);
if(base<1 || exp<0)
result=0;
else
for(i=1;i<=exp;i++)
{
result =result * base;
}
printf("%d",result);
}

Output:
Nesting of loops
•When a loop is written inside the body of another loop, then it
is known as nesting of loops.
•Any type of loop can be nested inside any other type of loop.
• For example a for loop may be nested inside another for loop
or inside a while or do while loop.
•Similarly while and do while loops can be nested.
Program 49
Program to understand nesting in for loop.
#include<stdio.h>
void main()
{
int i,j;
for(i=1;i<=3;i++)
{
printf("i=%d\n",i);
for(j=1;j<=4;j++)
{
printf("j = %d\t",j);
}
printf("\n");
}
}
Output:
Infinite loops
•The loops that go on executing infinitely and never terminate are called infinite
loops.
•Sometimes we write these loops by mistake while sometimes we deliberately
make use of these loops in our programs.
•Let us take some examples and see what type of mistakes lead to infinite loops.

•For (i=0; i<=5;i--)


•Printf(“%d”,i);

•This loop will execute till the value of I is less than or equal to 5 i.e. the loop will
terminate only when I becomes greater than 5.
•The initial value of i is 0 and after each iteration its value is decreasing, hence it
will never become greater than 5.
•So the loop condition will never become false and the loop will go on executing
infinitely.
•For the loop to work correctly we should write i++ instead of i--.
Jumping Statements

I. break statement
II. continue statement
III. goto statement
Break statement
•Break statement is used inside loops and switch statements.
•Sometimes it becomes necessary to come out of the loop even
before the loop condition becomes false.
•In such a situation, break statement is used to terminate the loop.
•This statement causes an immediate exit from that loop in which
this statement appears.
•It can be written as
Break;

•When break statement is encountered, loop is terminated and the


control is transferred to the statement immediately after the loop.
• The break statement is generally written along with a condition. If
break is written inside a nested loop structure then it causes exit
from the innermost loop.
Flowchart
Program 50
Program to understand the break statement.
#include<stdio.h>
void main()
{
int n;
for(n=1;n<=5;n++)
{
if(n==3)
{
printf("i understand the use of break\n");
break;
}
printf("number=%d\n",n);
}
printf("out of for loop\n");
}
Output:
Program 51
Program to understand the break statement.
#include<stdio.h>
int main()
{int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=4;j++)
{
break;
printf("i understand the use of break\n");
}
printf("number=%d\n",i);
}
printf("out of for loop\n");
return 0;
}
Output:
Program 52
Program to Check Prime Number
#include <stdio.h>
int main()
{
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d",&n);
for(i=2; i<=n/2; ++i)
{
if(n%i==0)
{
flag=1;
break;
}
} //END OF FOR LOOP
if (flag==0)
printf("%d is a prime number.",n);
else
printf("%d is not a prime number.",n);
return 0;
}
Output:
Continue statement
•The continue statement is used when we want to go to the next iteration of
the loop after skipping some statements of the loop.
• This continue statement can be written simply as-
continue;
•It is generally used with a condition. When continue statement is
encountered all the remaining statement (statements after continue) in the
current iteration are not executed and the loop continues with the next
iteration.
•The difference between break and continue is that when break is
encountered the loop terminates and the control is transferred to next
statement following the loop, but when a continue statement is
encountered the loop is not terminated and the control is transferred to
the beginning of the loop.
•In while and do-while loops, after continue statement the control is
transferred to the test condition and then the loop continues, whereas in for
loop after continue statement the control is transferred to update expression
and then the condition is tested.
Flowchart
Program 53
Program to understand the use of continue statement.
#include<stdio.h>
main()
{
int n;
for(n=1;n<=5;n++)
{
if(n==3)
{
printf(“I understand the use of continue\n”);
continue;
} END OF IF
printf(“number=%d\n”,n);
} //END OF FOR LOOP
printf(“out of for loop\n”);
} Output:
Program 54
Program to understand the use of continue statement.
#include<stdio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=2;j++)
{
printf("I understand the use of continue\n");
continue;}
printf("i=%d\n",i);}printf("out of for loop\n");
return 0;
}
Output:
goto statement
•This is an unconditional control statement that transfers the
flow of control to another part of the program.
•The goto statement can be used as-
goto label;
….…..….…
label: statement;
….……….…..
•Here label is any valid C identifier and it is followed by a
colon.

•Whenever the statement goto label, is encountered, the


control is transferred to the statement that is immediately
after the label.
Example for goto Statement
#include<stdio.h>
int main()
{
int i=0;
START: i++;
printf(“%d\n”,i);
if(a<=10)
{
goto START;
}
else
{
goto END;
}
END:
return 0;
}
Program 55
Program to print whether the number is even or odd.
#include<stdio.h>
void main()
{
int n;
printf("enter the number");
scanf("%d",&n);
if(n%2==0)
goto even;
else
goto odd;
even :printf("number is even");
goto end;
odd :printf("number is odd");
goto end;
end:printf("\n");
} Output:
Write Programs
1. Write a program to calculate the monthly telephone bills as per
the following rule:
Minimum Rs. 200 for up to 100 calls. Plus Rs. 0.60 per call for
next 50 calls. Plus Rs. 0.50 per call for next 50 calls. Plus Rs.
0.40 per call for any call beyond 200 calls.

2. Write a menu driven program which has the following options.


• Factorial of a no.
• Prime or not
• Odd or even
• Exit
Once a menu item is selected the appropriate option should be
taken and once this action is finished, the menu should reappear.
Unless the user selects the exit option, the program should
continue to work.
Pattern Printing
//To print *
#include<stdio.h>
void main()
{
int ;
printf("*");
}

//to print *****


#include<stdio.h>
void main()
{
int i,j;
for(i=1;i<=5;i++)
{
printf("*");
}
}
Pattern Printing Conti….

#include<stdio.h>
void main()
{
int i,j;
for(i=1;i<=3;i++)
{
for(j=1;j<=5;j++)
{
printf("*");
}
printf("\n");
}
}
Steps to create star pattern 1
Star Pattern 1
#include<stdio.h>
void main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
{
if(j<=i)
printf("*");
else
printf(" ");
}
printf("\n");
}
}
Steps to create star pattern 2
Star Pattern 2
#include<stdio.h>
void main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
{
if(j>=6-i)
printf("*");
else
printf(" ");
}
printf("\n");
}
}
Steps to create star pattern 3
Star Pattern 3
#include<stdio.h>
void main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
{
if(j>=i)
printf("*");
else
printf(" ");
}
printf("\n");
}
}
Steps to create star pattern 4
Star Pattern 4
#include<stdio.h>
void main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
{
if(j>=6-i )
printf("*");
else
printf(" ");
}
printf("\n");
}
}
Steps to create star pattern 5
Star Pattern 5
#include<stdio.h>
void main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=9;j++)
{
if(j>=6-i && j<=4+i)
printf("*");
else
printf(" ");
}
printf("\n");
}
}
Steps to create star pattern 6
#include<stdio.h>
Star Pattern 6
void main()
{
int i,j,k;
for(i=1;i<=5;i++)
{ k=1;
for(j=1;j<=9;j++)
{
if(j>=6-i && j<=4+i && k)
{
printf("*");
k=0;
}

else
{
printf(" ");
k=1;
}

}
Steps to create star pattern 7
Star Pattern 7
#include<stdio.h>
void main()
{
int i,j,k;
for(i=1;i<=5;i++)
{
for(j=1;j<=9;j++)
{
if(j<=6-i || j>=4+i)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
}
Star Pattern 8
#include<stdio.h>
void main()
{
int i,j,k;
for(i=1;i<=4;i++)
{
k=1;
for(j=1;j<=7;j++)
{
if(j>=5-i && j<=3+i)
{
printf("%d",k);
}
else
printf(" ");
}
printf("\n");
Star Pattern 9
#include<stdio.h>
void main()
{
int i,j,k;
for(i=1;i<=4;i++)
{
k=1;
for(j=1;j<=7;j++)
{
if(j>=5-i && j<=3+i)
{
printf("%d",k);
k++;
}

else
Star Pattern 10
#include<stdio.h>
void main()
{
int i,j,k;
for(i=1;i<=4;i++)
{
k=1;
for(j=1;j<=7;j++)
{
if(j>=5-i && j<=3+i)
{
printf("%d",k);
j<4?k++:k--;
}

else
printf(" ");
}
printf("\n");
}
}
Star Pattern 11
#include<stdio.h>
void main()
{
int i,j;
char k;
for(i=1;i<=5;i++)
{
k='A';
for(j=1;j<=5;j++)
{
if(j<=i)
{
printf("%c",k);
k++;
}
else
printf(" ");
}
printf("\n");
}
Star Pattern 12
#include<stdio.h>
void main()
{
int i,j;
char k;
for(i=1;i<=5;i++)
{
k='A';
for(j=1;j<=5;j++)
{
if(j>=i)
{
printf("%c",k);
k++;
}
else
printf(" ");
Star Pattern 13
#include<stdio.h>
void main()
{
int i,j;
char k;
for(i=1;i<=5;i++)
{
k='A';
for(j=1;j<=5;j++)
{
if(j<=i)
{
printf("%c",k-1+i);

}
else
{ printf(" ");
Star Pattern 14
#include<stdio.h>
void main()
{
int i,j;
char k;
for(i=5;i>=1;i--)
{
k='A';
for(j=5;j>=1;j--)
{
if(j<=i)
{
printf("%c",k-1+j);

}
else
{ printf(" ");
Star Pattern 15
#include<stdio.h>
void main()
{
int i,j;
char k;
for(i=1;i<=4;i++)
{
k='A';
for(j=1;j<=7;j++)
{
if(j<=5-i || j>=3+i)
{
printf("%c",k);
j<4?k++:k--;

}
else

You might also like