0% found this document useful (0 votes)
3 views1 page

C Programming: Loops, Functions, Arrays

The document provides an overview of key concepts in C programming, including loops (while, do-while, for), functions (user-defined and recursion), and arrays and strings. It includes code examples for each concept to illustrate their usage. The document serves as a concise reference for fundamental C programming techniques.

Uploaded by

poojithalagu5684
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)
3 views1 page

C Programming: Loops, Functions, Arrays

The document provides an overview of key concepts in C programming, including loops (while, do-while, for), functions (user-defined and recursion), and arrays and strings. It includes code examples for each concept to illustrate their usage. The document serves as a concise reference for fundamental C programming techniques.

Uploaded by

poojithalagu5684
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

C Programming: Expanded Notes

1. Loops in C Programming Loops are used to execute a block of code repeatedly until a specified
condition is met. C supports three main types of loops: while, do-while, and for loops. While Loop
#include <stdio.h> int main() { int i = 1; while (i <= 5) { printf("%d\n", i); i++; } return 0; } Do-While
Loop The do-while loop ensures the body executes at least once. #include <stdio.h> int main() { int i
= 1; do { printf("%d\n", i); i++; } while (i <= 5); return 0; } For Loop #include <stdio.h> int main() { for
(int i = 1; i <= 5; i++) { printf("%d\n", i); } return 0; } Break and Continue break is used to exit a loop
immediately, while continue skips the current iteration.

2. Functions in C Functions are reusable blocks of code that perform specific tasks. Declaration and
Definition returnType functionName(parameters) { // body } User-Defined Function Example
#include <stdio.h> int add(int a, int b) { return a + b; } int main() { int result = add(5, 10); printf("Sum
= %d", result); return 0; } Recursion Example (Factorial) #include <stdio.h> int factorial(int n) { if (n
== 0) return 1; return n * factorial(n - 1); } int main() { printf("Factorial of 5 = %d", factorial(5)); return
0; }

3. Arrays and Strings Arrays store multiple values of the same type in a single variable. Strings are
arrays of characters. One-Dimensional Array Example #include <stdio.h> int main() { int marks[5] =
{90, 85, 78, 92, 88}; for (int i = 0; i < 5; i++) { printf("%d ", marks[i]); } return 0; } Two-Dimensional
Array Example (Matrix Addition) #include <stdio.h> int main() { int a[2][2] = {{1, 2}, {3, 4}}; int b[2][2]
= {{5, 6}, {7, 8}}; int sum[2][2]; for (int i=0; i<2; i++) { for (int j=0; j<2; j++) { sum[i][j] = a[i][j] + b[i][j];
printf("%d ", sum[i][j]); } printf("\n"); } return 0; } Strings Example #include <stdio.h> #include
<string.h> int main() { char str1[20] = "Hello"; char str2[20] = "World"; strcat(str1, str2); printf("%s",
str1); return 0; }

You might also like