Prolog Plase with comment and screenshot from program Write
*Prolog**
Plase with comment and screenshot from program
Write a Prolog program that takes a list as its first argument and returns the odd numbers and the sum of the odds as the second and third arguments respectively. For example: ?- sum_odd ([3, 4, 5, 7, 14], Odd_numbers, Sum). Odd_numbers = [3, 5, 7] Sum = 15.Solution
Okay so lets Start, now as the language is not specified, lets write the code in C language.
So here we go with the code
#include <stdio.h>
int main(void)
{
int a[10],b[10];
int c=0,i,n;
printf(\"Enter Number of Elements:\");
scanf(\"%d\",&n);
printf(\"Enter Array of Numbers:\");
for(i=0;i<n;i++)
{
scanf(\"%d\",&a[i]);
}
printf(\"Odd Numbers are:\");
for (i=0;i<n;i++)
{
if (a[i]%2==1)
printf(\"%d\\t\",a[i]);
}
for (i=0;i<n;i++)
{
if(a[i]%2==1)
{
a[i]=b[i];
}
else
b[i]=0;
}
for (i=0;i<n;i++)
{
c=c+b[i];
}
printf(\"\ Sum of odd Numbers is:%d\",c);
return 0;
}
So this program displays the odd numbers in the array and displays the sum of the odd numbers.
Thank You for using Chegg...
