Anda di halaman 1dari 2

SUM OF DIGITS IN A GIVEN NUMBER:

#include <stdio.h>
void main()
{
int num, temp, digit, sum = 0,c=0,stat;
int i=0,a[50],count=0;
printf("Enter the number \n");
scanf("%d", &num);
temp = num;
stat=sizeof(num);
if(num%2==0)
{
while (num > 0)
{
digit = num % 10;
count++;
if(count%2==0)
{
c++;
a[i]=digit;
i++;
}
num /= 10;
}
}
else
{

while (num > 0)


{
digit = num % 10;
count++;
if(count%2!=0)
{
c++;
a[i]=digit;
i++;
}
num /= 10;
}
}
for(i=0;i<c;i++)
{
sum=sum+a[i];
}
printf("Given number = %d\n", temp);
printf("Sum of alternate digits=%d", sum);
}
Output:
Enter the number

12345

Given number = 12345

Sum of alternate digits=9

Enter the number

12345678

Given number = 12345678

Sum of alternate digits=16

Anda mungkin juga menyukai