14 Marks
14 Marks
1. For Loop:
o The for loop is a repetition control structure that executes a
block of code a specific number of times.
o Syntax:
o
o while (test_expression) {
o // body of the loop
o }
o Example:
o #include <stdio.h>
o int main() {
o int i = 0;
o while (i < 5) {
o printf("Iteration %d\n", i);
o ++i;
o }
o return 0;
o }
3.Do-While Loop
o The do-while loop executes a block of code at least once, and
then repeatedly as long as a specified condition is true.
o Syntax:
o
o do {
o // body of the loop
o } while (test_expression);
o Example:
o #include <stdio.h>
o int main() {
o int i = 0;
o do {
o printf("Iteration %d\n", i);
o ++i;
o } while (i < 5);
o return 0;
o }
2)various conditional statements used inc with example.
1. if Statement:
o The if statement is the most straightforward decision-
o if (condition) {
o // Statements to execute if condition is true
o }
o
o Example:
o #include <stdio.h>
o int main() {
o int i = 10;
o if (i > 15) {
o printf("10 is greater than 15\n");
o }
o printf("I am Not in if\n");
o return 0;
o }
2. if-else Statement:
o The if-else statement allows you to execute different
o if (condition) {
o // Executes this block if condition is true
o } else {
o // Executes this block if condition is false
o }
o
o Example:
o #include <stdio.h>
o int main() {
o int age = 20;
o if (age >= 18) {
o printf("You are eligible to vote!\n");
o } else {
o printf("You are not eligible to vote.\n");
o }
o return 0;
o }
o 2.Nested if Statement:
o nested if statements to create more complex decision
structures.
o Example:
o #include <stdio.h>
o int main() {
o int num = 12;
o if (num > 10) {
o if (num % 2 == 0) {
o printf("Number is even and greater than 10.\n");
o } else {
o printf("Number is odd and greater than 10.\n");
o }
o }
o return 0;
o }
3. switch Statement:
o The switch statement allows you to choose among
o switch(){
o case 1……
o ……
o }
o Example:
o #include <stdio.h>
o int main() {
numeric values.
o Examples:
o #include<stdio.h>
o void main()
o {
o int a,b;
o printf(“Enter a and b:”);
o scanf(“%d%d”,&a,&b);
o printf(“%d+%d=%d”,a,b,a+b);
o printf(“%d-%d=%d”,a,b,a-b);
o printf(“%d*%d=%d”,a,b,a*b);
o printf(“%d/%d=%d”,a,b,a/b);
o printf(“%d%d=%d”,a,b,a%b);
o }
2. Relational Operators:
o Used for comparison between two operands.
o Examples:
o #include<stdio.h>
o void main()
o {
o int a,b;
o scanf(“%d%d”,&a,&b);
o printf(“%d>%d”,a,b,a>b);
o printf(“%d<%d”,a,b,a<b);
o printf(“%d>=%d”,a,b,a>=b);
o printf(“%d<=%d”,a,b,a<=b);
o }
3. Logical Operators:
o Used to combine conditions and evaluate logical
o Logicalo:a||b
4. Bitwise Operators:
o Perform operations at the bit level.The output is
▪ Bitwise OR: a | b
▪ Bitwise XOR: a ^ b
▪ Bitwise NOT: ~a
5. Assignment Operators:
o Assign values to variables.
o Examples:
▪ Assignment: a = b
▪ Compound assignment: a += b
6. Miscellaneous Operators:
o Ternary operator (a ? b : c), comma operator (,), and
4.matrix multiplication.
#include<stdio.h>
Void main()
{
int a[10],b[10],multi[10];
int r1,r2,c1,c2,i,j,k,sum=0;
printf(“enter a row and cols of matrix 1:”);
scanf(“%d%d”,&r1,&c1);
printf(“enter element:);
for(i=0;i<=r1;i++){
for(j=0;j<=c1;j++){
scanf(“%d”,a[i][j]);
}
}
printf(“enter rows and cols of matrix 2:”);
scanf(“%d%d”,&r2,&c2);
if(r1!=c2)
{
printf(“multiplication is not possile”);
}
else{
for(i=0;i<=r2;i++){
for(j=0;j<=c2;j++){
scanf(“%d”,b[i][j]);
}
}
}
for(i=0;i<r1;i++){
for(j=0;j<c2;j++){
for(k=0;k<r2;k++){
sum=sum+a[i][k]*b[k][j];
c[i][j]=sum;
}
}
}
for(i=0;i<r1;i++){
for(j=0;j<c2;j++){
printf(“multiply matrix:%d”,c[i][j]);
#include <stdio.h>
#include <ctype.h> // For isalpha() and isdigit() functions
int main() {
char input[100];
int vowels = 0, consonants = 0, digits = 0, spaces = 0;
printf("Enter a string: ");
fgets(input, sizeof(input), stdin);
if (isalpha(ch)) {
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
++vowels;
}
else {
++consonants;
}
}
else if (isdigit(ch)) {
++digits;
}
else if (ch == ' ') {
++spaces;
}
}
return 0;
}
1. strcat() Function:
o The strcat() function is used for string concatenation. It
o Example:
o #include <stdio.h>
o #include <string.h>
o
o int main() {
o char dest[50] = "This is an";
o char src[50] = " example";
o printf("dest Before: %s\n", dest);
o strcat(dest, src);
o printf("dest After: %s", dest);
o return 0;
o }
2. strlen() Function:
o The strlen() function calculates the length of a given
o Example:
o #include <stdio.h>
o #include <string.h>
o
o int main() {
o char str[] = "GeeksforGeeks";
o size_t length = strlen(str);
o printf("String: %s\n", str);
o printf("Length: %zu\n", length);
o return 0;
o }
3. strcmp() Function:
o The strcmp() function compares two strings
lexicographically.
o Syntax:
o int main() {
o char str1[] = "Geeks";
o char str2[] = "For";
o char str3[] = "Geeks";
o int result1 = strcmp(str1, str2);
o int result2 = strcmp(str2, str3);
o int result3 = strcmp(str1, str1);
o printf("Comparison of str1 and str2: %d\n", result1);
o printf("Comparison of str2 and str3: %d\n", result2);
o printf("Comparison of str1 and str1: %d\n", result3);
o return 0;
}
1. Pass by Value:
o
o Example:
o #include <stdio.h>
o
o int main() {
o int x = 10, y = 20;
o printf("Before swap: x = %d, y = %d\n", x, y);
o swapByValue(x, y);
o printf("After swap (pass by value): x = %d, y =
%d\n", x, y);
o return 0;
o }
o Example:
o #include <stdio.h>
o
o int main() {
o int x = 10, y = 20;
o printf("Before swap: x = %d, y = %d\n", x, y);
o swapByReference(&x, &y);
o printf("After swap (pass by reference): x = %d, y =
%d\n", x, y);
o return 0;
}
#include<stdio.h>
void main(){
int option,a,b,c;
printf(“enter option :”)
scanf(“%d”,&option);
switch(option);
{
case1;
printf(enter a and b:”);
scanf(“%d%d”.&a,&b);
c=a+b;
printf(“add;%d”,c);
break;
case2:
printf(enter a and b:”);
scanf(“%d%d”.&a,&b);
c=a-b;
printf(“add;%d”,c);
break;
case3:
printf(enter a and b:”);
scanf(“%d%d”.&a,&b);
c=a*b;
printf(“add;%d”,c);
break;
case4:
printf(enter a and b:”);
scanf(“%d%d”.&a,&b);
c=a/b;
printf(“add;%d”,c);
break;
default;
printf(“enter the valid option:”);
printf(result :%d”,c);
}
o struct Person {
o char name[50];
o int age;
o float salary;
o };
o Example usage:
o employee.age = 30;
o strcpy(employee.name, "John");
o employee.salary = 50000.0;
2. Array of Structures:
o An array of structures is an array where each element is
a structure.
o It allows you to store multiple instances of the same
structure type.
o Example:
o struct Point {
o int x;
o int y;
o };
o points[0].y = 20;
3. Nested Structures:
o A nested structure (also called a structure within a
structure) is a structure that contains another structure
as one of its members.
o It allows you to create more complex data structures.
o Example:
o struct Employee {
o int empId;
o char empName[50];
o float empSalary;
o };
o
o struct Department {
o char deptName[50];
o struct Employee emp; // Nested structure
o };
o