Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Write a program which will input a 5-digit number. If the sum of digits is even, find whether the
input number is a prime or not. If the sum of digits is odd find, whether the number is palindrome or
not?

What I have tried:

C++
#include<stdio.h>
int main()
{
    int i,j,Sum=0,Num;
    printf("Enter your number:");   
    for (int j=1; j<=5; j++)
    {
        scanf("%d",&i);
        Sum=Sum+i;
    }
    printf("%d",Sum);
    for (i=2; i<=Num/2 ;i++);
    if (Num%i==0)
    {
        Sum++;
    }
    if(Sum==0 && Num!=1)
    {
        printf("%d is a Prime number",Num);
    }
    else
    {
        printf("%d is not a Prime number", Num);    
    }
    return 0;
}
Posted
Updated 18-Oct-22 7:24am
v2

1 solution

There are a few problems. Your first loop to accept five numbers is not what you want there. It is going to accept five numbers, one at a time, and Sum will have their total. This means if you entered "1 2 3 4 5" then Sum would be set to 15. To accept a five-digit number you need to omit the loop and the addition to Sum and change the format specifier to scanf to "%5d". The resulting code would be this :
C++
scanf( "%5d", &Sum );
Another problem is this line :
for (i=2; i<=Num/2 ;i++);
This loop does not do anything useful. The first issue is the variable Num is not set to anything. The second thing is the for statement has a terminating semicolon after it. I am really not sure what that is supposed to do. If the loop is used to determine if the value is a prime number then that is not the right way to do it. There are many algorithms available to do this so I will let you find one you like.

The previous time you posted this I attempted to figure out if a number is palindromic and it was actually pretty easy. I peeled of each digit and put them into an array and then evaluated the contents of the array. Actually, I used a vector for that but since you have a five digit number an array of size 5 will work OK. Just has one can determine odd or even using the modulus operator, one can also separate digits using the same modulus operator. For this, you want to use 10 has the divisor and remember to divide by ten to get the next digit.

I hope this is helpful.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900