0% found this document useful (0 votes)
2 views

Module- 2

The document covers branching and iteration control structures in C programming, including if-then-else statements, loops (for, while, do-while), and switch statements. It explains the need for decision-making in programming and introduces arrays, their types, and operations. Additionally, it provides sample programs and exercises related to these concepts.

Uploaded by

ojsangwai17
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Module- 2

The document covers branching and iteration control structures in C programming, including if-then-else statements, loops (for, while, do-while), and switch statements. It explains the need for decision-making in programming and introduces arrays, their types, and operations. Additionally, it provides sample programs and exercises related to these concepts.

Uploaded by

ojsangwai17
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

MODULE- II: BRANCHING AND

ITERATIONS

1. Control flow structures: if-then-else, nested if-else, conditional expression, switch


2. while loop, do-while loop, for loop, break, continue, goto.
3. Arrays: Need of array, Types of arrays: One dimensional array, Two-dimensional
array
4. Examples and Operations, Primitive Operations on Strings.
Decision Making(Conditional Statements):
Need of Decision:
● There come situations in real life when we
need to make some decisions and based on
these decisions, we decide what should we
do next.

Need of conditional statements:


● Similar situations arise in programming also
where we need to make some decisions and
based on these decisions we will execute the
next block of code.
Types of Control Statement in C:
Control Structures

Control Structures are just a way to specify flow of control in programs.

1. if-then-else:

Syntax:
if(expression)
{
statement block1;
}
else
{
statement block2;
}
Sample Program of if then else:

#include <stdio.h>
void main( ) {
int x, y;
x = 15; y = 18;
if (x > y ) {
printf("x is greater than y"); }
else {
printf("y is greater than x");
}
}
Output:
y is greater than x
2.Nested if....else Statement
Syntax:
if( expression ) {
if( expression1 ) {
statement block1;
}
else {
statement block2;
}
}
else {
statement block3;
}
Sample Program of Nested if else: else{
if(b > c){
printf("b is the greatest");
#include <stdio.h> }
void main( ) { else{
int a, b, c; printf("c is the greatest");
printf("Enter 3 numbers..."); }
scanf("%d%d%d",&a, &b, &c); }
if(a > b) { }
if(a > c) {
Output:
printf("a is the
Enter 3 numbers:
greatest"); }
25
else {
36
printf("c is the greatest");
20
} 36 is greatest number
}
Switch Statement : The switch statement allows us to execute one code
block among many alternatives.

Syntax:

switch (expression)
{
case constant1: // statements
break;
case constant2: // statements
break;
.
.
.
default: // default statements
}
Sample program of switch case switch(choice)
{
#include<stdio.h> case 1:
printf("Enter 2 numbers");
void main( ) { scanf("%d%d", &a, &b);
int a, b, c, choice; c = a + b; Output
while(choice != 3) { printf("%d", c); Enter your choice
/* Printing the available options */ break; 1. Addition
printf("\n 1. Press 1 for addition"); case 2: 2. Subtraction
printf("\n 2. Press 2 for printf("Enter 2 numbers");
scanf("%d%d", &a, &b); 1
subtraction");
c = a - b;
printf("\n Enter your choice"); printf("%d", c); Enter 2 numbers
/* Taking users input */ break; 5
scanf("%d", &choice); default: 10
printf("you have passed a wrong key"); 5 + 10 = 15
printf("\n press any key to continue");
}
}
}
For loop:
Syntax of for loop:

for (initialization; condition test; increment or decrement)

//Statements to be executed repeatedly

while loop do...while loop:


syntax: syntax:
while (condition test) {
{
//Statements to be executed repeatedly
// statements inside the body of the loop
// Increment (++) or Decrement (--) Operation
}
}
while (testExpression);
Sample Program of for loop to Print Sample Program of while loop to Print Sample Program of do while loop
numbers from 1 to 5. numbers from 1 to 5. to Print numbers from 1 to 5.

#include<stdio.h> #include <stdio.h> #include <stdio.h>

void main() int main() { int main() {

{ int i = 1; int i = 1;

int i; while (i <= 5) { do{

for(i=0;i<5;i++) { printf("%d\n", i);


printf("%d\n", i);
++i;
printf("%d\n", i); ++i;
}while (i <= 5);
} }
return 0;
} return 0;
}
}
Output: Output:
1 Output: 1
2 1 2
3 2 3
4 3 4
5 4 5
5
goto statement:

When a goto statement is encountered in a C program, the control jumps directly to the label mentioned in the
goto statement.

Syntax : goto label_name;

..

..

label_name: C-statements;

continue statement:

Syntax: continue;

break statement:It is used to come out of the loop instantly.

Syntax: break;
Sample program of goto statement Sample program of break
Sample program of continue
#include <stdio.h> statement statement
#include <stdio.h>
int main() {
int main(){
int sum=0; #include <stdio.h> int num =0;
for(int i = 0; i<=10; i++){ int main() { while(num<=100) {
sum = sum+i; printf("value of variable num is: %d\n",
for (int j=0; j<=8; j++){ num);
if(i==5){
if (j==4) { if (num==2) {
goto addition;
Continue; break;
}
}
} }
num++;
addition: printf("%d ", j);
}
printf("%d", sum); }
printf("Out of while-loop");
return 0; return 0; return 0;
}
} }
Output:
Output: Output:
15 value of variable num is: 0
01 2 3 5 6 7 8
value of variable num is: 1
value of variable num is: 2
Question:
1.Check if a Number is Even or Odd
2.Check if a Person is Eligible to Vote
3.Check if a Person is Eligible to Drive
4.Find the Largest of Two Numbers
5.Check if a Number is Positive, Negative, or Zero
6.Check if a Year is a Leap Year
7.Check if a Character is a Vowel or Consonant
8.Check if a Student Passed or Failed
9.Check if a Number is Divisible by 5 and 11
10.Check if a Number is Positive, Negative, or Zero
Question:
11.Find the Largest of Three Numbers
12.Check the Type of Triangle
13.Find the Grade Based on Marks
14.Determine Leap Year or Not
15.Classify a Character as Vowel, Consonant, or Not an Alphabet
16.Check if a Number is Even or Odd
17.Find the Maximum of Two Numbers
18.Check if a Number is Positive, Negative, or Zero
19.Find the Smallest of Three Numbers
20.Assign a Grade Based on Marks
Question:
21.Print Numbers from 1 to 10
22.Calculate the Sum of Numbers from 1 to N
23.Factorial of a Number
24.Print Multiplication Table
25.Reverse a Number
26.Count Digits in a Number
27.Print Fibonacci Series
28.Print a Pattern of Stars
29.Fibonacci Series
30.Reverse a String
Question:

31.Sum of Even Numbers

32.Print Diamond Pattern

33.Count Vowels in a String

34.Print Pascal's Triangle

35.Print Non-Multiples of 5

36.Skip Negative Input and Print Positive

37.Print Numbers Except Multiples of 7

38.Skip Even Numbers and Print Squares

39.Print Even Numbers in a Range

40.Print Sum of Odd Numbers

41.Print Multiples of 4 but Skip Multiples of 6


What is Arrays
Array is a collection of element of same data types.
Types of Array
1D Array:

data_type array_name [size];

or

data_type array_name [size1] [size2]...[sizeN];


data_type array_name [size] = {value1, value2, ... valueN};
printf("%d\n", numbers[i]);
Example:
#include <stdio.h>
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
int i;
printf("Array elements are:\n");
for (i = 0; i < 5; i++) {
printf("%d\n", numbers[i]);
}
return 0;
}
2D Array:
Syntax:

data_type array_name [i][j];


OR
data type array_name[size_i] [size_j];

Example:
int matrix[3][4];
int matrix[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
}
Example:
#include <stdio.h>
int main() {

int matrix[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
}

printf("Matrix elements are:\n");

for (int i = 0; i < 3; i++) {


for (int j = 0; j < 4; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n"); // New line after each row
}

return 0;
}
Need of array:

1) An array holds elements that have the same data type.

2) Array elements are stored in subsequent memory locations.

3) Two-dimensional array elements are stored row by row in subsequent memory locations.

4) Array name represents the address of the starting element.

5) Array size should be mentioned in the declaration. Array size must be a constant

expression and not a variable.

Example:

1.Sorting an array

2. Storing elements in a 2D matrix and printing it

3.Addition of two 2D matrix


Operations on arrays:

1.Traversing: It is used to access each data item exactly once so that it can be processed.
2. Searching: It is used to find out the location of the data item if it exists in the given
collection of data items.
3. Insertion: It is used to add a new data item in the given collection of data items.
4. Deletion: It is used to delete an existing data item from the given collection of data
items.
5. Sorting: It is used to arrange the data items in some order i.e. in ascending or
descending order
6. Merging: It is used to combine the data items of two sorted files into single file in the
sorted form
Sample program of array
for Sorting an array printf("Printing Sorted
#include<stdio.h> Element List ...\n");
void main () { for(i = 0; i<10; i++) {
int i, j,temp;
printf("%d\n",a[i]); }
int a[10] = { 10, 9, 7, 101, 23, 44,
12, 78, 34, 123}; }
for(i = 0; i<10; i++) { Output:
for(j = i+1; j<10; j++) {
Printing Sorted Element List:
if(a[j] > a[i]) {
7,9,10,12,23,34,44,78,10,1231
temp = a[i];
a[i] = a[j];
a[j] = temp; } } }
Primitive operations on Strings
C String Length: strlen() function C Copy String: strcpy()

#include<stdio.h> #include<stdio.h>

#include <string.h> #include <string.h>

int main(){ int main(){

char ch[20]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', char ch[20]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};
'n', 't', '\0'}; char ch2[20];
printf("Length of string is: strcpy(ch2,ch);
%d",strlen(ch));
printf("Value of second string is: %s",ch2);
return 0;
return 0; }
}
Output
Output
Value of second string is: javatpoint
Length of string is: 10
C String Compare: strcmp() function C Copy String: strcat()

#include<stdio.h> #include <stdio.h>


#include<string.h> #include <string.h>
int main()
int main() {
{ char first_str[]=”PICT”;
char dest[100] = "PICT ";
Char second_str[]=”PICT”;

int res=strcmp(first_str,second_str); char src[100] = "PUNE";

if(res==0) strcat(dest, src);


printf(“Strings are Equal”);
printf("Concatenated String: %s\n", dest);
else
return 0;
printf(“Strings are unequal”);
}
printf(“Value returned by strcmp() is:%d”,res);

return 0;

}
Question:
1.C Program Enter the Student Marks and Find the Percentage and Grade
2. Program to create a simple calculator
3.write a program to check whether given year is leap or not
4.write a program to check whether the string is palindrome or not
5.C program to check whether a character is vowel or consonant
6.C program to print day of week name using switch case
7.Write a program to find grading scheme using
8.Find triangle is equilateral, isosceles or right angled Code in C Language
9.Print the season name of the year based on the month number
10.Design a program to print a message 10 times.
11.write a program to add numbers until user enters zero.
12.c program to find sum of digits of a number
13.Write a program in C to display the multiplication table vertically from 1 to n.
14.Fibonacci Series in C without recursion
15. What are Arrays in C. Define an array for storing 10 integers.
16 What are Arrays in C. Define an array for storing 10 real numbers.
17 What are Arrays in C. Define an array for storing a string “Hello World!”.
18 Explain the use of C data types for storing a string of characters.
19.What are conditional Expression in C? Explain with example.
20.Explain the following syntax with an example.
Syntax: exp1 ? exp2 : exp3
21.What are control flow structures? Give one Example.
22.What is called iteration? Give Example.
23.Explain with example, flow of operation.
24.Justify the sentence with example, “Control flow structures are called conditions, loops and
switch ”
25.Explain various constructs of conditions.
26.A student is displayed as passed if the passed status is marked as “P” else displayed not passed.
Using C write necessary coding statements.
27.With an example, Explain switch statement in C.
28. With an example, explain the for loop in C.
29. With an example, explain the while loop in C.
30. Differentiate between while and do...while loop in C.
31. With an example, explain the do ... while loop in C.
32. Give one example of break, continue and goto statements in C.
33. What are arrays? Define an array to store 10 integers using C.
34. What are arrays? Define an array to store 10 real numbers using C.
35. What are arrays? Define an array to store 10 characters using C.
36. Write a C code to display first data in an array.
37. Illustrate using C, who two dimensional array are declared.
38. Illustrate using C, who three dimensional array are declared.
39. Name three applications of using arrays.
40. A Character “A” is to be stored in a in an Integer data type three times, Write a code in C.
41. Write a C code to display a missing number from given 10 ordered integers.
42. Write a code using C to display a 2D integer matrix.
43. Write a code using C to display a 2D integer matrix, accept the input.

44. Write a code using C to perform and display a 2D integer matrix addition.
45. Write a code using C to perform and display a 2D integer matrix subtraction, accept the input.
46. What are strings? Write C code to display a stored string.
47.Using C describe different ways to declare strings. What is the difference between them.
48.How to find a length of a given string. Illustrate using C code.
49.Write a C code to reverse the Accepted string.
50.Write a C code to change an accepted strings first character.
51.Write a C code to check the palindrome in accepted string.

You might also like