Monday, April 27, 2020

C Programming Slip 12

Slip12
Q1. Write a C program to calculate sum of digits of a given input number.[15 Marks]
#include <stdio.h>

int main()
{
    int num, sum=0;

    /* Input a number from user */
    printf("Enter any number to find sum of its digit: ");
    scanf("%d", &num);

    /* Repeat till num becomes 0 */
    while(num!=0)
    {
        /* Find last digit of num and add to sum */
        sum += num % 10;

        /* Remove last digit from num */
        num = num / 10;
    }

    printf("Sum of digits = %d", sum);

    return 0;
}
Q2. Accept two numbers from user and write a menu driven program to perform
 the following operations [25 Marks]
1. swap the values of two variables
2. calculate arithmetic mean and harmonic mean of two numbers
#include<stdio.h>
void main()
{
    int n1,n2,t,choice;
   
         float arithmetic_mean,harmonic_mean;

    printf("\n Enter the radius of circle:- ");
    scanf("%d%d",&n1,&n2);

    printf("\n Please make a choice from the following :");
    printf("\n \n Options:\t \t \t \t"); 
    printf("Actions:");
    printf("\n \n 1. SWap :");
   
    printf("\n \n 2. MEAN ");
       
    scanf("%d",&choice);

    switch(choice)
    {

    case 1:
                   t=n1;
                   n1=n2;
                   n2=t;
        printf("\n swap=%d %d \n \n",n1,n2);
    break;

   
   

    case 2:
       
   
     arithmetic_mean = (n1+n2) /2;
    harmonic_mean = (n1*n2) / (n1+n2);
   printf("arithmetic mean = %f and harmonic mean = %f",arithmetic_mean,harmonic_mean);
    break;

    default:
    printf("\n You haven made invalid choice \n \n");

    }
} // End of the code 

No comments:

Post a Comment