program for pointer in c

[ad_1]

                                 

 

Code:

#embody <stdio.h>

int predominant() {

    int a[100];

    int i,n,sum=0;

    int *p;

    printf(“Enter a Quantity:”);

    scanf(%d,&n);

    printf(“Enter %d parts:”,n);

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

    {

        scanf(%d,&a[i]);

    }

    p=a;

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

    {

        sum=sum+*p;

        p++;

        

    }

    printf(“Sum of parts=%d,sum);

    return 0;

}

Pattern Enter and Output:

Enter a Quantity:4

Enter 4 parts:2

1

5

6

Sum of parts=14

On this article we’re going to see tips on how to write program to sum of all parts saved in an array utilizing pointer

What’s Pointer ?

In C Programming Language Pointer is a variable which is used to retailer the deal with of one other variable.The variable will be of any kind like int,float,char and so forth.It’s retailer to retailer and handle the deal with of dynamically allotted blocks of reminiscence.

Clarification of above program 

Within the first line of code,we now have included stdio.h header file which can assist us to make use of a few of the built-in perform like printf() and scanf() that will probably be used to print the assertion and take console enter respectively.

Within the subsequent line of code,we now have outlined predominant perform which can begin the execution of our C program,we are going to outline the core of our program in predominant perform solely and never use user-defined perform for a similar.

Within the subsequent line of code,we now have declared one array with measurement 100 and sort int,we are going to use this array to take all of the component from consumer of which we now have to fined  sum utilizing pointers.

Within the subsequent line we now have declared three variable i,n and sum=0 the place i will probably be used as iteration variable i.e in for loop , n to take complete no.of component of an array and sum to retailer the results of addition of all parts

Within the subsequent line, we now have declared one pointer of title p,we are going to use this to retailer the beginning deal with of first component of a array and remaining component of array will probably be fetched iteratively.

After that we now have printed one assertion ‘Enter a Quantity’ assertion which can assist consumer to get to identified that we now have to present some enter then solely this system will proceed since we now have wrote one scanf() assertion which can really pause the execution till enter is offered.

Within the subsequent line we now have wrote yet one more print assertion will assist consumer to get to identified that how a lot component we now have present to make an array,we’re reaching it utilizing following assertion printf(“Enter %d parts:”,n); right here the worth of n will probably be concatenated with Enter component assertion for instance if consumer entered the worth of n=3 then this print assertion will probably be evaluated as beneath Enter 3 component,In above assertion we now have %d format specifier as a result of n is of int(Integer) kind.

Within the subsequent line of ,we now have outlined one for loop which can assist us to take parts of array one after the other and retailer it in an a array by indexing technique.In case you did not get it let’s perceive by iteration by iteration.

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

The pattern of n = 3

1st Iteration

i = 0(Assigned i=0 for the reason that array indexing begins from 0)

0<3 //Since(i<n) Due to this fact the management will go contained in the for loop and execute all of the assertion inside it.

Within the first line of for loop we’re utilizing scanf( “%d”,&a[i]) the which means of this assertion is taking the consumer enter of  kind int and retailer it in a[0] for the reason that worth of i on this iteration is 0 subsequently it should retailer the enter worth in a[0].

the worth of i will probably be incremented

2nd iteration 

i =1 (Because the worth of i is incremented by 1 because it was beforehand 0)

1<3 //Since(i<n) Situation happy subsequently the management will go contained in the for loop and execute the statements

It’ll once more take consumer enter as a second component of an array and can retailer it in a[1] as the worth of i is 1 

 

In case you any have doubt,suggestion concerning this put up or web site then be at liberty to share with us.

 

When you’ve got realized one thing new from this put up then share this put up with your loved ones and
buddies.

 

Pleased Studying :)?   

 

Earlier
Subsequent

[ad_2]

Leave a Reply

Your email address will not be published. Required fields are marked *