0% found this document useful (0 votes)
2 views4 pages

C Arrays

Uploaded by

anannya7797
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)
2 views4 pages

C Arrays

Uploaded by

anannya7797
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
You are on page 1/ 4

C Arrays:

1. Write a program to input and display elements of an array.


#include <stdio.h>

int main() {
int a[4]={1,2,3,4};
int s=0, p=1;
int n;
printf("Enter the number here:");
scanf("%d",&n);
for ( int i=0;i<4;i++){
s=s+a[i];
p=p*a[i];
printf("%d\n",i[a]);
}
printf("Summ: %d\nProduct:%d\n",s,p);
printf("-------------------\n");
for(int j=3;j>=0;j--){
printf("%d\n",a[j]);
}
int q=0;
printf("--------------------\n");
for (int i=0;i<4;i++){

if (a[i]==n){
q=1;
printf("%d found at %d position",n,i+1);
break;
}

}
if (q==0){
printf("%d Not in this array",n);
}
for ( int i=0;i<4;i++){
if (i%2==0) printf("\n%d",a[i]);

return 0;
}
Ques2) In a Given array find the Total Number of Pairs exists which is equal to x;

#include <stdio.h>

int main(){
int a[10]={1,2,3,4,5,6,7,8};
int pairs=0;
int x, flag=0;
printf("Enter element:");
scanf("%d",&x);
for (int i=0;i<8;i++){
for (int j=i+1;j<=7;j++){

if (a[i]+a[j]==x)
{
flag=1;
pairs++;
}

}
}
if (flag==0){
printf("No Pair");

}
else{
printf("Pairs= %d",pairs);
}

Ques 3) Triplets from the Array equal to the Number


#include <stdio.h>

int main(){
int a[10]={1,2,3,4,5,6,7,8};
int pairs=0;
int x,flag=0;
printf("Enter element:");
scanf("%d",&x);
for (int i=0;i<8;i++){
for (int j=i+1;j<=7;j++){
for ( int k=j+1;k<=7;k++){

if (a[i]+a[j]+a[k]==x)
{
flag=1;
printf("%d, %d, %d\n",a[i],a[j],a[k]);
pairs++;
}
}
}
}
if (flag==0){
printf("No Pair");

}
else{
printf("Pairs= %d",pairs);
}

Ques 4) Copy the content of one array to another in Reverse Order

#include <stdio.h>

int main(){
int a[8]={1,2,3,4,5,6,7,8};
int p[8];
int k=0;
for (int i=7;i>=0;i--){
printf("%d\n",k);
p[k]=a[i];
k++;
}

//Printing
for (int i=0;i<8;i++){
printf("%d,",p[i]);
}
}

//Without Extra Variable


for ( int i=0;i<=n;i++){
a[i]=b[n-i];
}

You might also like