Monday, April 27, 2020

C Programming slip 14

Slip14
Q1. Write a C program to check whether a input number is Armstrong number or not.
[15 Marks]
#include <stdio.h>
int main()
{
    int number, originalNumber, remainder, result = 0;

    printf("Enter a three digit integer: ");
    scanf("%d", &number);

    originalNumber = number;

    while (originalNumber != 0)
    {
        remainder = originalNumber%10;
        result += remainder*remainder*remainder;
        originalNumber /= 10;
    }

    if(result == number)
        printf("%d is an Armstrong number.",number);
    else
        printf("%d is not an Armstrong number.",number);

    return 0;
}

Q2. Write a program to accept a number and count number of even, odd and zero digits
 within that number. [25 Marks]
#include <stdio.h>
int main()
{    
      int nodd,neven,num,digit,zero=0 ;
      printf("Enter four digit number: ");
      scanf("%d",&num);
      while (num> 0)
      {
            digit = num % 10; /* separate LS digit from number */
            num /= 10;
            if(digit != 0 && digit % 2 == 0)
            {
                  neven++;
            }
            else if(digit==0)
            {
                  zero++;
            }
            else
            {
                  nodd++;
            }
      }
      printf("\nOdd digit : %d \nEven digit : %d\nZeros : %d", nodd, neven,zero);
      return 0;              
}

No comments:

Post a Comment