C PROGRAM ON NUMBER

C Program to Find Prime Numbers in a Given Range

#include <stdio.h>
#include <stdlib.h>
 
void main()
{
    int num1, num2, i, j, flag, temp, count = 0;
 
    printf("Enter the value of num1 and num2 \n");
    scanf("%d %d", &num1, &num2);
    if (num2 < 2)
    {
        printf("There are no primes upto %d\n", num2);
        exit(0);
    }
    printf("Prime numbers are \n");
    temp = num1;
    if ( num1 % 2 == 0)
    {
        num1++;
    }
    for (i = num1; i <= num2; i = i + 2)
    {
        flag = 0;
        for (j = 2; j <= i / 2; j++)
        {
            if ((i % j) == 0)
            {
                flag = 1;
                break;
            }
        }
        if (flag == 0)
        {
            printf("%d\n", i);
            count++;
        }
    }
    printf("Number of primes between %d & %d = %d\n", temp, num2, count);
}

 

Runtime Test Cases

Case:1

Enter the value of num1 and num2

70 85

Prime numbers are

71

73

79

83

Number of primes between 70 and 85 = 4

Case:2

Enter the value of num1 and num2

0 1

There are no primes upto 1

C Program to Check whether a Number is Prime or Not using Recursion

/*
C Program to find whether a Number is Prime or Not using Recursion
*/
#include <stdio.h>
 
int primeno(int, int);
 
int main()
{
int num, check;
printf("Enter a number: ");
scanf("%d", &num);
check = primeno(num, num / 2);
if (check == 1)
{
printf("%d is a prime number\n", num);
}
else
{
printf("%d is not a prime number\n", num);
}
return 0;
}
 
int primeno(int num, int i)
{
if (i == 1)
{
return 1;
}
else
{
if (num % i == 0)
{
return 0;
}
else
{
return primeno(num, i - 1);
}       
}
}

 

Runtime Test Cases

 

$ cc pgm24.c

$ a.out

Enter a number: 456

456 is not a prime number

 

$ a.out

Enter a number: 89

89 is a prime number

C Program to Display Armstrong Number between Two Intervals

*
 * C Program to Print Armstrong Number from 1 to 1000
 */
#include <stdio.h>
 
void main()
{
    int temp, digit1, digit2, digit3;
 
    printf("All Armstrong numbers between 1 and 1000 are:\n");
    for(int n=1;n<=1000;n++)  //suppose loop is at n = 371
    {
        if(n<=9)
        {
            printf("%d ",n);
        }
        else
        {
            digit1 = n % 10;   //digit1 = 371 % 10 = 1.
            digit2 = (n % 100 - digit1) / 10;  //digit2 = (371%100 – 1) / 10 = 7     
            digit3 = (n % 1000 - digit2) /100; //digit3 = (371%1000 –7)/100 = 3
            temp = (digit1 * digit1 * digit1) + (digit2 * digit2 * digit2) + (digit3 * digit3 * digit3); 
             //(1*1*1) + (7*7*7) + (3*3*3) = 371
         if (temp == n)
            {
                printf("%d ", temp);
            }
        }
    }
}

 

Program Output

All Armstrong numbers between 1 and 1000 are:

 

1 2 3 4 5 6 7 8 9 153 370 371 407

Sum of First N Natural Numbers in C

/*
 * C program to find the sum of 'N' natural numbers using for loop
 */
#include <stdio.h>
 
int main()
{
    int n;
    printf("Enter number:");
    scanf("%d",&n);
    int sum=0;
    for(int i=1;i<=n;i++)
    sum += i;
    printf("sum of first %d numbers is: %d",n,sum);
    return 0;
}

 

Run Time Testcases

To find the sum of the first n natural numbers, we enter the value “4” as input.

Enter number: 4

sum of first 4 numbers is: 10

/*
 * C program to find the sum of 'N' natural numbers using formula
 */
#include <stdio.h>
 
int main()
{
    int n;
    printf("Enter number:");
    scanf("%d",&n);
    int sum= ((n)*(n+1))/2;
    printf("sum of first %d numbers is: %d",n,sum);
    return 0;
}

 

Run Time Testcases

To find the sum of the first n natural numbers, we enter the value “7” as input.

Enter number: 7

sum of first 7 numbers is: 28

C Program to Reverse a Number

/* C program to reverse a number using function*/
 
#include <stdio.h>
 
int reverse(int n)
{
    int rev = 0;
    while (n > 0)
    {
        rev = rev * 10 + n % 10;
        n /= 10;
    }
    return rev;
}
 
int main(void)
{
    int n;
    printf("Enter a number: ");
    scanf("%d", &n);
    printf("Reversed number is %d", reverse(n));
    return 0;
}

 

Runtime Test Cases

Testcase 1: In this case, we enter the number “453424123” as input.

Enter a number: 453424123

Reversed number is 321424354

Testcase 2: In this case, we enter the number “0324135” as input.

Enter the number:

Enter a number: 0324135

Reversed number is 531423%

C Program to Multiply Two Binary Numbers

/*
 * C Program to Find Multiplication of two Binary Numbers
 */
#include <stdio.h>
 
int binaryproduct(int, int);
 
int main()
{
 
    long binary1, binary2, multiply = 0;
    int digit, factor = 1;
 
    printf("Enter the first binary number: ");
    scanf("%ld", &binary1);
    printf("Enter the second binary number: ");
    scanf("%ld", &binary2);
    while (binary2 != 0)
    {
        digit =  binary2 % 10;
        if (digit == 1)
        {
            binary1 = binary1 * factor;
            multiply = binaryproduct(binary1, multiply);
        }
        else
            binary1 = binary1 * factor;
        binary2 = binary2 / 10;
        factor = 10;
    }
    printf("Product of two binary numbers: %ld", multiply);
    return 0;
}
 
int binaryproduct(int binary1, int binary2)
{
    int i = 0, remainder = 0, sum[20];
    int binaryprod = 0;
 
    while (binary1 != 0 || binary2 != 0)
    {
        sum[i++] =(binary1 % 10 + binary2 % 10 + remainder) % 2;
        remainder =(binary1 % 10 + binary2 % 10 + remainder) / 2;
        binary1 = binary1 / 10;
        binary2 = binary2 / 10;
    }
    if (remainder != 0)
        sum[i++] = remainder;
    --i;
    while (i >= 0)
        binaryprod = binaryprod * 10 + sum[i--];
    return binaryprod;
}

 

Runtime Test Cases

Output:

 

Enter the first binary number: 10010

Enter the second binary number: 10101

Product of two binary numbers: 101111010