Computer Programming C: UNIT I: Introduction to C

Case Studies: Solving simple scientific and statistical problems

1. Write a program to check, whether the given Prime or not. 2. Write a program to find the Greatest Common Division (GCD) (Highest Common Factor) of two given numbers. 3. Program to print Table of Binomial Coefficients.

Case Studies

(Solving simple scientific and statistical problems)

 

1. Write a program to check, whether the given Prime or not.

A prime number is an integer which has no proper divisions. For example, 13 is a prime number, because 13 is not divisible by any number other than 1 and 13.

If 'n' has a factor greater than √n, it also has a factor less than √n. So, it is enough to check whether 'n' is divisible by any number between 2 and √n. If divisible, 'n' is not prime, otherwise it is prime.

/* Program to check the given number is prime or not */

#include <stdio.h>

main()

{

     /* Local definitions */

     int n,i,r;

     printf("Enter the positive integer value.....");

     scanf("%d", &n);

     i=2;

     Step1:

          if(i<=sqrt(n))

          {

               r=n%i;

               if(r==0)

               {

                    printf("%d is not a prime",n);

                    goto end;

               } /* if */

          } /* if */

          else

          {

               i++;

               goto stepl;

          } /* if else */

     printf("%d is prime",n);

end:

     printf(" ");

} * main */

OUTPUT

Enter the positive integer value.....3

3 is prime

EXPLANATION: Read n then checks whether the i<=sort(n), if the condition is true if block is executed and prints the entered value is not a prime. Otherwise else part is executed, then the variable i is increased by 1 then goto step1, after completion of else part finally the entered value is printed as prime.

 

2. Write a program to find the Greatest Common Division (GCD) (Highest Common Factor) of two given numbers.

Let the numbers be I and J, and I be the bigger number and J be the smaller number. Suppose, we want to find the GCD(HCF) of two numbers 36 and 54. The bigger number 54 must assigned to I and 36 to J.

 I=54, J=36

Divide I by J, the remainder when 54 is divided by 36 is 18, So, I=18, J=36

Divide I by J and find the remainder, when 36 is divided by 18, the remainder is 0, so, I=0, J=18.

Interchanging I and J we get I=18, J=0. Here J=0, hence I is the GCD, so, GCD=18. The Greatest Common Division of 54 and 36 is 18.

/* Program to find ged of two numbers */

#include <stdio.h>

main()

{ /* Local definitions */

    int i,j,k;

    printf("Enter Two numbers.....");

    scanf("%d %d", &i, &j);

    if(j>i)

    {

        k=i;

        i=j;

        j=k;

    } /* if */

step1:

    if(j==0)

        goto step2;

    else

    {

        k=i%j;

        i=j;

        j=k;

    } /* if else */

step2:

    printf("The GCD is..... %d",j);

} /* main */

OUTPUT

Enter Two numbers.....54 36

The GCD is.. 18

EXPLANATION: Read the values i and j, the if statement checks the condition j>i, if condition is true, then the value of i and j is interchanged using third variable k. Otherwise take modular division between i, j. If k becomes zero then prints the value of GCD through the variable j as a output.

 

3. Program to print Table of Binomial Coefficients.

The Binomial coefficients are used to study the binomial distributions.

It is given by B(m, x) =  , m >= x

A table of binomial coefficients is required to determine the binomial coefficient for any set of 'm' and 'x'. The binomial coefficient can be recursively calculated as follows:

B(m, o)=1, B(m, x)=B(m,x‒1) [(m‒x+1) / x ] , ,x=1, 2, 3, ..., m, B(0,0)=1

That is, the binomial coefficient is one, when either 'x' is zero or 'm' is zero.

/*Program to print table of binomial coefficients */

#include <stdio.h>

main()

{

   int m,x, bin;

   printf("mx");

   for(m=0;m<=5;m++)

      printf("%4d",m);

      printf("\n‒‒‒‒‒‒‒‒‒‒‒‒\n");

   m=0;

   do

   { printf("%2d", m);

      x=0;

      bin=1;

      while(x<=m)

      {

         if(m==0||x==0)

               printf("%4d", bin);

         else

         {

            bin=bin*(m‒x+1)/x;

            printf("%4d", bin);

      } /* if else */

   x=x+1;

   }/* while */

   printf("\n");

   m=m+1;

} /* do */

   while(m<=5);

      printf("\n‒‒‒‒‒‒‒‒‒‒‒‒\n");

} /* main

OUTPUT

mx 0 1 2 3 4 5

‒ ‒ ‒ ‒ ‒ ‒ ‒ ‒ ‒

0  1

1  1

2  1  1

3  1  2  1

4  1  3  3     1

5  1  4  6     4   1

1  1  5  10  10   5   1

‒ ‒ ‒ ‒ ‒ ‒ ‒ ‒ ‒


Computer Programming C: UNIT I: Introduction to C : Tag: Computer Science : - Case Studies: Solving simple scientific and statistical problems


Computer Programming C: UNIT I: Introduction to C



Under Subject


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation



Related Subjects


English Essentials I

EN25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


தமிழர் மரபு - Heritage of Tamils

UC25H01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Calculus

MA25C01 Maths 1 M1 - 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I

PH25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Chemistry I

CY25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Makerspace

ME25C04 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming Python

CS25C02 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Fundamentals of Electrical and Electronics Engineering

EE25C03 1st Semester EEE Depart | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Mechanical Engineering

ME25C03 1st Semester Mechanical Dept | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Civil Engineering

CE25C01 1st Semester Civil, Agri Departments | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing

CS25C03 1st Semester - AIDS, CSE, CSE(CY), IT Department | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I Laboratory

PH25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Applied Chemistry I Laboratory

CY25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming C Laboratory

CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming Python Laboratory

CS25C02 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Engineering Drawing

ME25C01 EEE, Mech, Agri, EEE Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electronics and Electrical Engineering

EE25C04 1st Semester ECE Dept | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing - Laboratory

CS25C03 1st Semester AIDS, CSE, CSE(CY), IT Depts | practical Laboratory Manual | 2025 Regulation | 1st Semester 2025 Regulation