Pps Unit 2
Pps Unit 2
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.
salary
SALARY
Digits
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-
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.
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.
2.4478889999999999
Types of operations performed
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=
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
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
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
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);
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
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
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(*)
Output:
Dot(.) operator And Arrow operator
#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.
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;
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
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.
•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;
#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