pages

Monday, April 27, 2020

C Programming slip 29


C Programming BCA Practical Slips Solution
Slip 29
Q1. Write a C program to calculate factorial of a number using user defined function.
[15 Marks]
#include <stdio.h>
int main()
{    int n, i;
    unsigned long long factorial = 1;
    printf("Enter an integer: ");
    scanf("%d",&n);
    // show error if the user enters a negative integer
           for(i=1; i<=n; ++i)
        {
            factorial *= i;              // factorial = factorial*i;
        }
        printf("Factorial of %d = %llu", n, factorial);
        return 0;
}
Q2. Write a program, which accepts a number n and displays each digit separated by tabs.
Example: 6702 Output = 6 7 0 2 [25 Marks]
#include<stdio.h>
int main()
{
      int digit, num,rem;
      printf("Enter positive integer number: ");
      scanf("%d", &num);    
     while(num>0)
         {
          rem=rem*10+num%10;
           num=num/10;
          }
      printf("\nYou have entered: ");
      while (rem > 0)
      {            digit = rem % 10;   
            printf("\t %d",digit);
            rem=rem/10;
      }
      return 0;
}

No comments:

Post a Comment