Category: Arrays and Structures

Addition of array elements in C 5

Addition of Array elements

Write a program in C that accepts number of array elements from the user, accepts array element values and calculates the sum/addition of all the array elements. The program should display the value of addition. #include<stdio.h> int main() { int arr[100], i, no,sum=0; clrscr(); printf(“\nEnter the no. of elements: “); scanf(“%d”,&no); for(i=0;i<no;i++) { printf(“\nEnter element %d”,i+1); scanf(“%d”, &arr[i]); } for(i=0;i<no;i++) { sum=sum+arr[i]; } printf(“\nsum of array element: %d”,sum); return 0; } Output: Enter the no. of elements: 5 Enter element 1 : 1 Enter element 2 : 2 Enter element 3 : 3 Enter element 4 : 4 Enter element...

2-Dimensional (2D) Array in C Programming Language 14

2-Dimensional (2D) Array in C Programming Language

An array is a collective name given to a group of similar variables. An array can be 1-Dimensional, 2-Dimensional, 3-Dimensional and so on. In this topic, we will discuss 2-Dimensional (2D) arrays in C Programming Language. Let us understand what two dimensional arrays are. Consider the following matrix. 11 12 13 A= 14 15 16 17 18 19 The above-mentioned matrix is 3 by 3. The matrix elements can be accessed using the formula A[m,n], where m represents row number and n represents column number. Thus, the first element i.e. 11 is represented as A[0,0]. Similarly, A[0,1] = 12 A[0,2]...

C Arrays – 1D (1 Dimensional) 29

C Arrays – 1D (1 Dimensional)

An array in C is a collective name given to a group of similar variables. C arrays can be 1-Dimensional, 2-Dimensional, 3-Dimensional and so on. In this topic, we will discuss 1-Dimensional (1D) arrays in C. So lets start with 1D array in C. Let us understand C arrays with the help of an example. void main(){ int arr[3],i; printf(“Enter 3 values\n”); for(i=0;i<3;i++) { scanf(“%d”,&arr[i]) } printf(“The entered values are:\n”); for(i=0;i<10;i++) { printf(“%d\t”,arr[i]) } } Explanation: int arr[3] statement declares an array capable of holding 3 integer values. The first value can be accessed using arr[0]. Similarly second value can...

FREE C Cheatsheet - Speed Up Your C Programming.

FREE C Cheatsheet - Speed Up Your C Programming.

Download a 7-page free cheat sheet for easy and quick access to C Concepts, Snippets, and Syntax.

Thank you! Check you inbox and access your cheat-sheet