C Code 34 _ To Find the sum of the elements of the array whose sum of digit is same as of the given number. [InfoGain Coding Round Question]

C Code 34 _ To Find the sum of the elements of the array whose sum of digit is same as of the given number. [InfoGain Coding Round Question]


Sample Input:

5                                 //number of elements
123  321  46  33  8     //array elements
6                                 //comparing number

Sample Output:

477                          // 123 + 321 + 33

Explanation:

The elements 123 , 321 and 33 has the sum of digit equal to as the given comparing number.
So add these elements only and print the output. 
If none of the sum of digits of the elements matches the given number then the sum is 0.

Program:


#include<stdio.h>
int main()
{
int a[100];
int n,t,i,v,x,sum=0,s;
printf("Enter the no of elements in the array : ");
scanf("%d",&n);
printf("\n\nEnter %d elements : ",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n\nEnter a number : ");
scanf("%d",&t);
for(i=0;i<n;i++)
{
v=a[i];
s=0;
while(v>0)
{
x=v%10;
s=s+x;
v=v/10;
}
if(s==t)
{
sum=sum+a[i];
}
}
printf("\n\nThe sum of elements in the array that has the sum of its digits equal as the given number is : %d",sum);
return 0;
}


Output:














Thank You ...





Previous Post
Next Post
Related Posts

0 comments:

Popular Posts