Prime Number, Reverse Number, Sum of digit using loop in c

Program to reverse a number.

In this example, we will understand to reverse number and know how to write, compile and debug it in C language and also learn how to implement it in c.

        
In order to do this you will need
  • Basic knowledge of c programming language.
  • As well as know how to operate codeblocks.   
So let's begin with our program
1.Open a codeblocks software.
2.Open new empty file.
3.Copy and paste the code from below.
 
Input Code-
// Program to reverse a number.

#include <stdio.h>
int main()
{
    int n, rev = 0, remainder;
    printf("Enter an integer: ");
    scanf("%d", &n);
    while (n != 0)
    {
        remainder = n % 10;
        rev = rev * 10 + remainder;
        n /= 10;
    }
    printf("Reversed number = %d", rev);
    return 0;
}

For output input any integer number you want.

output-
Enter an integer: 123
Reversed number = 321
Process returned 0 (0x0)

Program to check given number is prime or not.

In this example, we will check if given number is prime or not.

Input code-
// Program to check given number is prime or not.

#include <stdio.h>

int main()
{
  int n, i, c = 0;
  printf("Enter any number n:");
  scanf("%d", &n);

    for (i = 1; i <= n; i++)
    {
      if (n % i == 0)
      {
         c++;
      }
     }

     if (c == 2)
     {
     printf("%d is a Prime number:",n);
    }
    else
    {
       printf("%d is not a Prime number",n);
    }
  return 0;
}


For output enter any integer number 

output-
Enter any number n:7
7 is a Prime number:
Process returned 0 (0x0)

Program to find sum of digits of an integer.

In this example, we will do addition of digits of an integer entered by user

Input code-
// Program to find sum of digits of an integer.

#include <stdio.h>
int main()
{
   int n, t, sum = 0, remainder;

   printf("Enter an integer\n");
   scanf("%d", &n);

   t = n;

   while (t != 0)
   {
      remainder = t % 10;
      sum       = sum + remainder;
      t         = t / 10;
   }

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

   return 0;
}

For output enter any number you want.

output-
Enter an integer
132
Sum of digits of 132 = 6

Process returned 0 (0x0)

Post a Comment

If you have any doubt or suggestions.please let me know.

Previous Post Next Post

Ads by google

Ads by google