Engineer Simple https://www.engineersimple.com/2022/08/c-program-learning-part-8.html

# DECISION MAKING AND LOOPING

Given a number, write a program using loop to reverse the digits of the number. For example, the number 12345 Should be written as 54321

#include<stdio.h>

int main()

{

int n, rev = 0, remainder;

printf("Enter an integer: ");

scanf("%d", &n);

while (n != 0)

{

remainder = n % 10;

rev = rev * 10 + remainder;

n /= 10;

}

printf("Reversed number = %d", rev);

return 0;

}

Write a program to find maximum number N input numbers?

#include<stdio.h>

int main()

{

int i,num,n,large=0, loc=0;

printf("How many numbers: ");

scanf("%d",&n);

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

{

printf("\nEnter number %d: ",i);

scanf("%d",&num);

if(num>large)

{

large=num;

loc=i;

}

}

printf("\n\nThe Largest Number is %d %d ",large,loc);

return 0;

}

Arrays:

An Array is a fixed-size sequenced collection of elements of the same data type. It is simply a grouping of like-type data. In its simplest form, an array can be used to represent a list of numbers, or a list of names. For example, we can use an array name salary to represent a set of salaries of a group of employees in an organization. For example:

salary 

represents the salary of 10th employee. While the complete set of values is referred to as an array, individual values are called elements.

One Dimensional Arrays:

A list of items can be given one variable name only one subscript and a variable is called a single-subscripted variable or a one dimensional array. For example, if we want to represent a set of five numbers, say (35, 40, 20, 57, 19), by an array variable number, then we may declare the variable number as follows

int number;

and the computer reserves the five storage locations as follows below: Declaration of one Dimensional Arrays:

Like any other variable, arrays must be declared before they are used so that the complier can allocate space for them in memory. The general form of array declaration is

type variable-name[size];

The type specifies the type of element that will be contained in the array, such as int, float, char and the size indicates the maximum number of elements that can be stored inside inside the array. For example,

float height;

Why we need to allow one extra element space when declaring character arrays?

The size in a character string represents the maximum number of characters that that the string can hold. For instance,

char name;

declares the name as a character array variables that can hold a maximum of 10 characters. Suppose we read the following string constant into the string variable name.

“WELL DONE”

Each character of the string is treated as an element of the array name and is stored in the memory as follows: When the compiler sees a character string, it terminates it with an additional null character. Thus, the element name holds the null character ‘\0’.

Initialization of one dimensional arrays:

After an array is declared, its elements must be initialized. Other wise they contain ” garbage ”. An array can be initialized at either of the following stages:

1. At compile time

2. At runtime

Compile time initialization:

The general form of initialization of arrays is:

type array-name[size] = {list of values};

The values in the list are separated by commas. For instance, the statement

int number = { 0, 0, 0};

will  variable number as an array of size 3 and will assign zero to each element.

Run time initialization:

An array can be explicitly initialized at run time. For example consider the following segment of the C program.

for( i = 0; i < 100; i = i + 1)

{

if( i < 50)

sum[i] = 0.0;

else

sum[i]=1.0;

}

The first 50 elements of the array sum are initialized to zero while the remaining 50 elements are initialized to 1.0 at run time.

We can also use a read function such as scanf to initialize an array.

For example, the statements

int x;

scanf(“%d%d%d”,&x, &x, &x);

Two Dimensional Arrays:

Consider The table:

C allows us to define such tables of items by two dimensional arrays. The table can be defined as
v
The two dimensional arrays are declared as follows:

type array_name[row_size][column_size];

Two dimensional arrays are stored in memory as shown below:

Initialization of Two dimensional Arrays:
int table = { 0, 0, 0, 1, 1, 1 };
Above statement can be equivalently written as
int table = {{0, 0, 0}, {1, 1, 1}};
In matrix form
int table = {
{0, 0, 0}, {1, 1, 1}
};

What is the out put of the following program?

#include<stdio.h>

main()

printf(“\” WELL DONE ! “\”);
Answer: The output is “WELL DONE”

### Task for you-

What is the out put of the following program?
int main()
char string[]="HELLO WORLD";
int m;
for(m=0;string[m]!='\0';m++)
if((m%2)==0)
printf("%c ",string[m]);
return 0;
}

### Share with others

Publish comments by following these Rules. 