Module- 2
Module- 2
ITERATIONS
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:
{ int i = 1; int i = 1;
When a goto statement is encountered in a C program, the control jumps directly to the label mentioned in the
goto statement.
..
..
label_name: C-statements;
continue statement:
Syntax: continue;
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:
35.Print Non-Multiples of 5
or
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}
}
return 0;
}
Need of array:
3) Two-dimensional array elements are stored row by row in subsequent memory locations.
5) Array size should be mentioned in the declaration. Array size must be a constant
Example:
1.Sorting an array
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>
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()
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.