0% found this document useful (0 votes)
10 views20 pages

14 Marks

The document provides an overview of various programming concepts in C, including looping statements (for, while, do-while), conditional statements (if, if-else, switch), operators (arithmetic, relational, logical), matrix multiplication, counting vowels and consonants, string functions, pass by value vs. pass by reference, and structures. Each concept is explained with syntax and examples to illustrate their usage. Additionally, it includes a simple program for a scientific calculator and definitions of structures, arrays of structures, and nested structures.
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)
10 views20 pages

14 Marks

The document provides an overview of various programming concepts in C, including looping statements (for, while, do-while), conditional statements (if, if-else, switch), operators (arithmetic, relational, logical), matrix multiplication, counting vowels and consonants, string functions, pass by value vs. pass by reference, and structures. Each concept is explained with syntax and examples to illustrate their usage. Additionally, it includes a simple program for a scientific calculator and definitions of structures, arrays of structures, and nested structures.
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/ 20

14 marks

1)various looping statements in c with example.

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 for (initialize_expression; test_expression; update_expression)


{
o // body of the loop
o }
o Example:
o #include <stdio.h>
o int main() {
o for (int i = 0; i < 10; ++i) {
o printf("Iteration %d\n", i);
o }
o return 0;
o }
2. While Loop:
o The while loop repeatedly executes a block of code as long as a
specified condition is true.
o Syntax:

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-

making statement. It executes a block of code if a


certain condition is true.
o Syntax:

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

blocks of code based on whether a condition is true or


false.
o Syntax:

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

several alternatives based on the value of an expression.


o Syntax :

o switch(){
o case 1……
o ……

o }

o Example:

o #include <stdio.h>

o int main() {

o char grade = 'B';


o switch (grade) {
o case 'A':
o printf("Excellent!\n");
o break;
o case 'B':
o printf("Good!\n");
o break;
o default:
o printf("Needs improvement.\n");
o }
o return 0;
}

3).different types of operator in c.


1. Arithmetic Operators:
o These operators perform mathematical operations on

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 printf(“Enter a and 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

expressions. The output is whether true or not .


o
o Examples:
o Logical and:a&&b

o Logicalo:a||b

o Logical not :!a

4. Bitwise Operators:
o Perform operations at the bit level.The output is

whether true or not .


o Examples:

▪ Bitwise AND: a & b

▪ 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

sizeof operator (sizeof(a)).


o

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]);

5)count the no of vowels ,consonants and white spaces

#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);

for (int i = 0; input[i] != '\0'; ++i) {


char ch = tolower(input[i]); // Convert to lowercase for
case-insensitive comparison

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;
}
}

printf("Vowels: %d\n", vowels);


printf("Consonants: %d\n", consonants);
printf("Digits: %d\n", digits);
printf("Spaces: %d\n", spaces);

return 0;
}

6) types of string function.

1. strcat() Function:
o The strcat() function is used for string concatenation. It

appends a copy of the source string to the end of the


destination string.
o Syntax:

o char* strcat(char* dest, const char* src);


o

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

string (excluding the null character \0).


o Syntax:

o int strlen(const char* str);


o

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 strcmp(const char* str1, const char* str2);


o Example:
o #include <stdio.h>
o #include <string.h>
o

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;
}

5)Explain the concept of pass by value and pass by


reference by using swapping of two values.

1. Pass by Value:

o In pass by value, a copy of the actual value is passed to


a function. Any modifications made within the function
do not affect the original value outside the function.
o When you pass a variable by value, the function works
with its local copy, leaving the original unchanged.
o

o
o Example:
o #include <stdio.h>
o

o void swapByValue(int a, int b) {


o int temp = a;
o a = b;
o b = temp;
o }
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 As you can see, the values of x and y remain unchanged

after calling swapByValue().


2. Pass by Reference:
o In pass by reference, a reference (memory address) to

the original value is passed to the function. Any


modifications made within the function directly affect
the original value.
o In C, we achieve pass by reference using pointers.

o Example:
o #include <stdio.h>
o

o void swapByReference(int* a, int* b) {


o int temp = *a;
o *a = *b;
o *b = temp;
o }
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;
}

8)Create a C program to design the scientific


calculator using built-in functions.

#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);
}

6)Explain Structure, array of Structure and nested


structure
Structure:
o A structure in C is a composite data type that allows
you to group related variables of different data types
into a single unit.
o It provides a way to define a custom data structure with

named members (fields).


o Syntax:

o struct Person {

o char name[50];
o int age;
o float salary;
o };

o Example usage:

o struct Person employee;

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 struct Point points[5]; // Array of 5 Point structures


o points[0].x = 10;

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

o struct Department hrDept;


o hrDept.emp.empId = 101;
o strcpy(hrDept.emp.empName, "Alice");
o hrDept.emp.empSalary = 60000.0;
strcpy(hrDept.deptName, "HR");

You might also like