Digital जीवन

Free Online Education for India...

Full width home advertisement

Computer Basic

C Programming

Engineering Graphics

Post Page Advertisement [Top]

Dr. A.P.J. Abdul Kalam Technical University, Lucknow

KCS151 / KCS251 Programming for Problem Solving - Using C Language


Lab Exercises


6. Write a program to find the value of y for a particular value of n. The a, x, b, n is input by user
if n = 1     y = ax % b
if n = 2     y = ax2 + b2
if n = 3     y = a - bx
if n = 4     y = a + x/b


/*
 File: Prgrm06.c
 Author: Aditya Saini
 Date: Jan 15, 2021
 Description: Program to find the value of y for a particular value of n. The a, x, b, n is input by user
 if n = 1     y = ax % b
 if n = 2     y = ax2 + b2
 if n = 3     y = a - bx
 if n = 4     y = a + x/b
*/

#include <stdio.h>

int main (void)
{
     int a, x, b, n;
     float y;

     //Input a, x, b, n
     printf ("Input a: ");
     scanf ("%d", &a);
     printf ("Input x: ");
     scanf ("%d", &x);
     printf ("Input b: ");
     scanf ("%d", &b);
     printf ("Input n: ");
     scanf ("%d", &n);

     //Calculate y on the basis of n
     switch (n)
     {
          case 1:
               printf ("y = ax % b = ");
               y = (a * x) % b;
               break;
          case 2:
               printf ("y = ax^2 + b^2: ");
               y = a * x * x + b * b;
               break;
          case 3:
               printf ("y = a - bx = ");
               y = a - b * x;
               break;
          case 4:
               printf ("y = a + x/b = ");
               y = (float) a + (float) x / (float) b;
               break;
          default:
               printf ("Error! Wrong input of n.");
               return 0;
     }

     //Print result
     printf ("%.2f", y);

     return 0;
};

Output

Input a: 5
Input x: 7
Input b: 3
Input n: 4
y = a + x/b = 7.33



No comments:

Post a Comment

Please do not post spam links.

Bottom Ad [Post Page]

| Designed by Colorlib