0% found this document useful (0 votes)
3 views11 pages

For While and Array

The document provides various C programming examples, including pre and post-increment operations, loops (for and while), arrays, and arithmetic operations on arrays. It explains the concepts with code snippets and outputs, demonstrating how to manipulate data and control program flow. Additionally, it covers the declaration, initialization, and usage of one-dimensional arrays.

Uploaded by

danushssy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views11 pages

For While and Array

The document provides various C programming examples, including pre and post-increment operations, loops (for and while), arrays, and arithmetic operations on arrays. It explains the concepts with code snippets and outputs, demonstrating how to manipulate data and control program flow. Additionally, it covers the declaration, initialization, and usage of one-dimensional arrays.

Uploaded by

danushssy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Pre increment

#include <stdio.h>
int main() {
int i = 5;
int result;

result = ++i; // first increment i (i becomes 6), then assign


printf("i = %d, result = %d\n", i, result);

return 0;
}

Post increment

#include <stdio.h>
int main() {
int i = 5;
int result;

result = i++; // first assign i (5) to result, then increment i (to 6)


printf("i = %d, result = %d\n", i, result);

return 0;
}

Comparing both

#include <stdio.h>
int main() {
int x = 10, y;

y = ++x; // pre-increment
printf("Pre-increment: x = %d, y = %d\n", x, y);

x = 10; // reset x
y = x++; // post-increment
printf("Post-increment: x = %d, y = %d\n", x, y);

return 0;
}

Problem Statement

Write a C program to evaluate the following expression and print the results:

y = x++ + ++x;

#include <stdio.h>
int main() {
int x = 5, y;

y = x++ + ++x;
printf("Final value of x = %d\n", x);
printf("Final value of y = %d\n", y);

return 0;
}

Example 1: Day Finder

#include <stdio.h>
int main() {
int day, choice;

printf("Days: 1=Monday, 2=Tuesday, ..., 7=Sunday\n");


printf("Enter today’s day (1-7): ");
scanf("%d", &day);

printf("Press 1 to go to next day, 2 to go to previous day: ");


scanf("%d", &choice);

if (choice == 1) {
day++;
if (day > 7) day = 1; // wrap around to Monday
}
else if (choice == 2) {
day--;
if (day < 1) day = 7; // wrap around to Sunday
}

// print day name using if...else


if (day == 1) printf("Day = Monday\n");
else if (day == 2) printf("Day = Tuesday\n");
else if (day == 3) printf("Day = Wednesday\n");
else if (day == 4) printf("Day = Thursday\n");
else if (day == 5) printf("Day = Friday\n");
else if (day == 6) printf("Day = Saturday\n");
else if (day == 7) printf("Day = Sunday\n");

return 0;
}

Simple Counter

#include <stdio.h>
int main() {
int value = 0, choice;

do {
printf("\nCurrent value = %d\n", value);
printf("1. Increment (++value)\n");
printf("2. Decrement (--value)\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
if (choice == 1) {
value++;
}
else if (choice == 2) {
value--;
}
else if (choice == 3) {
printf("Exiting...\n");
}
else {
printf("Invalid choice!\n");
}
} while (choice != 3);

return 0;
}

1. For Loop – Sum of First N Natural Numbers

#include <stdio.h>

int main() {

int n = 5, sum = 0, i;

printf("For Loop Example: Sum of first %d numbers\n", n);

for (i = 1; i <= n; i++) {

sum += i;

printf("Sum = %d\n", sum);

return 0;

FOR LOOP WHILE LOOP


#include <stdio.h> #include <stdio.h>
int main() {
int i; int main() {
printf("For Loop Example: Print numbers int i = 1;
1 to 5\n"); printf("While Loop Example: Print
for (i = 1; i <= 5; i++) { numbers 1 to 5\n");
printf("%d ", i); while (i <= 5) {
} printf("%d ", i);
return 0; i++;
} }
return 0;
}
Feature For Loop While Loop
Initialization Inside for(i=1;...) Before the loop (int i = 1;)
Condition
In the for statement In the while statement
check
Increment In the for statement (i++) Must be written inside the loop body
When number of iterations is When number of iterations is not known, depends
Typical Use
known on a condition
Slightly longer, separates initialization, condition,
Readability Compact, all in one line
and increment
Executes same as while if written Executes same as for if increment is included
Behavior
correctly properly

Right-Angled Triangle (5×5)

#include <stdio.h>

int main() {

int i, j;

for (i = 1; i <= 5; i++) { // Rows

for (j = 1; j <= i; j++) { // Columns in each row

printf("* ");

printf("\n");

return 0;

Introduction to Arrays (C Programming)

🔹 What is an Array?

● An array is a collection of elements of the same data type, stored in contiguous


memory locations.

● It allows storing multiple values under a single name and accessing them using
indices.
👉 Example: Instead of writing 5 separate variables like

int a1, a2, a3, a4, a5;

We can use:

int arr[5];

Now arr[0], arr[1], arr[2], arr[3], arr[4] represent 5 integer values.

🔹 Declaration of One-Dimensional Array

Syntax:

datatype array_name[size];

● datatype → Type of elements (e.g., int, float, char).

● array_name → Name of the array.

● size → Number of elements.

Example:

int marks[5]; // array of 5 integers

float prices[10]; // array of 10 floats

char letters[20]; // array of 20 characters

🔹 Initialization of One-Dimensional Array

We can initialize arrays at the time of declaration:

1. Full Initialization

int arr[5] = {10, 20, 30, 40, 50};

2. Partial Initialization
(Uninitialized elements become 0)

int arr[5] = {10, 20}; // arr = {10, 20, 0, 0, 0}

3. Automatic Size Detection

int arr[] = {5, 10, 15, 20}; // size = 4

🔹 Accessing Elements
● Index starts from 0.

● First element → arr[0]

● Last element → arr[size-1]

Perfect! Let’s make it very clear with a table showing declaration, initialization, and
memory representation for one-dimensional arrays.

One-Dimensional Array Examples

S. Declaration & Memory Representation


Notes
No Initialization (Index → Value)

arr[0] = ?

arr[1] = ?
Declared but not initialized.
1 int arr[5]; arr[2] = ?
Values are garbage.
arr[3] = ?

arr[4] = ?

arr[0] = 10

arr[1] = 20
int arr[5] = {10, 20,
2 arr[2] = 30 Fully initialized.
30, 40, 50};
arr[3] = 40

arr[4] = 50

arr[0] = 10

arr[1] = 20
Partially initialized. Remaining
3 int arr[5] = {10, 20}; arr[2] = 0
elements set to 0.
arr[3] = 0

arr[4] = 0

4 int arr[] = {5, 15, 25, arr[0] = 5 Size automatically determined =


35}; 4.
arr[1] = 15
S. Declaration & Memory Representation
Notes
No Initialization (Index → Value)

arr[2] = 25

arr[3] = 35

letters[0] = 'A'

char letters[4] = {'A', letters[1] = 'B' Partial initialization. Last


5
'B', 'C'}; letters[2] = 'C' element is null character (\0).

letters[3] = '\0'

prices[0] = 99.5
float prices[3] = Array of floats. Memory stores
6 prices[1] = 49.9
{99.5, 49.9, 25.0}; decimal values.
prices[2] = 25.0

1. Array indices always start from 0.

2. Uninitialized elements of global/static arrays are 0, local arrays are garbage.

3. Size can be automatic if you provide initialization values.

4. Arrays store homogeneous data (all same type).

Example Program

#include <stdio.h>

int main() {

int marks[5] = {80, 90, 75, 88, 95}; // initialization

int i;

printf("Student Marks:\n");

for (i = 0; i < 5; i++) {

printf("marks[%d] = %d\n", i, marks[i]);

return 0;
}

🔹 Output

Student Marks:

marks[0] = 80

marks[1] = 90

marks[2] = 75

marks[3] = 88

marks[4] = 95

One-Dimensional Array = Linear collection of elements stored in a sequence.

Sum of First N Natural Numbers using Array

#include <stdio.h>

int main() {

int n = 5; // Number of natural numbers

int numbers[5]; // Array to store first N numbers

int sum = 0;

int i;

// Store numbers in the array

for (i = 0; i < n; i++) {

numbers[i] = i + 1; // first N natural numbers

// Calculate sum

for (i = 0; i < n; i++) {

sum += numbers[i]; // add each element to sum

// Print the numbers and sum


printf("First %d natural numbers: ", n);

for (i = 0; i < n; i++) {

printf("%d ", numbers[i]);

printf("\nSum = %d\n", sum);

return 0;

#include <stdio.h>

int main() {

int n, i;

float arr1[50], arr2[50], sum[50], diff[50], prod[50], div[50];

printf("Enter number of elements (max 50): ");

scanf("%d", &n);

// Input first array

printf("Enter elements of first array:\n");

for (i = 0; i < n; i++) {

scanf("%f", &arr1[i]);

// Input second array

printf("Enter elements of second array:\n");

for (i = 0; i < n; i++) {

scanf("%f", &arr2[i]);

}
// Perform arithmetic operations

for (i = 0; i < n; i++) {

sum[i] = arr1[i] + arr2[i];

diff[i] = arr1[i] - arr2[i];

prod[i] = arr1[i] * arr2[i];

if (arr2[i] != 0) {

div[i] = arr1[i] / arr2[i];

} else {

div[i] = 0; // avoid division by zero

// Display results

printf("\nResults:\n");

printf("Index\tA\tB\tA+B\tA-B\tA*B\tA/B\n");

for (i = 0; i < n; i++) {

printf("%d\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n",

i, arr1[i], arr2[i], sum[i], diff[i], prod[i], div[i]);

return 0;

While Loop – Reverse Digits of a Number

#include <stdio.h>

int main() {

int num = 1234, rev = 0;

printf("While Loop Example: Reverse of %d\n", num);


while (num != 0) {

rev = rev * 10 + num % 10;

num /= 10;

printf("Reversed Number = %d\n", rev);

return 0;

You might also like