programming like c, c++, java,python learn in free

Saturday, June 13, 2020

C programming Question and Answer Day-15

71. Program to check Niven number (Harshad number).

#include<stdio.h>
#include<conio.h>
void main()
{
    int n, d, a, sum = 0;
    clrscr();
    printf("Enter the number : ");
    scanf("%d", &n);

    a = n;

    while (a > 0)
    {
        d = a % 10;
        sum = sum + d;
        a = a / 10;
    }

    if (n % sum == 0)
        printf("\nThe number is Niven Number.");
    else
        printf("\nThe number is not a Niven Number.");
    getch();
}

72. Program to check whether the number is palindrome or not.

#include<stdio.h>
#include<conio.h>
void main()
{
    int n, rev = 0, temp;
    clrscr();
    printf("Enter a number : ");
    scanf("%d", &n);

    temp = n;

    while (temp != 0)
    {
        rev = rev * 10;
        rev = rev + temp % 10;
        temp = temp / 10;
    }

    if (n == rev)
        printf("\n%d is palindrome number.", n);
    else
        printf("\n%d is not palindrome number.", n);
    getch();
}

73. Program to check perfect number.

#include<stdio.h>
#include<conio.h>

void main()
{
 int n, i = 1, sum = 0;
 clrscr();
 printf("Enter a number : ");
 scanf("%d", &n);

/The first perfect number is 6, because 1, 2, and 3 are its proper positive divisors, and 1 + 2 + 3 = 6./

 while (i < n)
 {
  if (n % i == 0)
  {
   sum = sum + i;
  }
  i++;
 }

 if (sum == n)
 {
  printf("\n%d is a perfect number.", i);
 }
 else
 {
  printf("\n%d is not a perfect number.", i);
 }
 getch();
}

74. Program to find the square root of a number.

#include<math.h>
#include<stdio.h>
#include<conio.h>
void main()
{
    double num, result;
    clrscr();
    printf("Enter number : ");
    scanf("%lf", &num);
    result = sqrt(num);
    printf("Square root of %lf is %lf.", num, result);
    getch();
}

75. Program to print sum of 'n' prime numbers.

#include<stdio.h>
#include<conio.h>
void main()
{
    int n, i = 3, count, c, sum = 2;
    clrscr();
    printf("Enter total number of prime numbers for addition : ");
    scanf("%d", &n);

    if (n >= 1)
    {
        printf("\nFirst %d prime numbers are :", n);
        printf("\n2 ");
    }
    for (count = 2; count <= n;)
    {
        for (c = 2; c <= i - 1; c++)
        {
            if (i % c == 0)
                break;
        }
        if (c == i)
        {
            sum = sum + i;
            printf("%d ", i);
            count++;
        }
        i++;
    }
    printf("\nSum : %d", sum);
    getch();
}
Share:

0 comments:

Post a Comment

Unordered List

recentposts1
Powered by Blogger.

Text Widget

Featured Post

C programming Question and Answer Day-15

71. Program to check Niven number (Harshad number). #include<stdio.h> #include<conio.h> void main() {     int n, d, a, su...

Search This Blog

recent comments

recentcomments

Labels

[slideshow][technology]

vertical posts

[verticalposts][technology]

business

[business][grids]

ad space

ads 600

vehicles

[cars][stack]
[verticalposts][food]

our facebook page

about us

logo

Jupiter is a magazine responsive Blogger template. It has everything you need to make your blog stand out. This template is fully customizable and very flexible and we believe you will love it as much as we do.

Slide show

[people][slideshow]

health

[health][btop]

recent posts

recentposts1

Subscribe Us

random posts

randomposts2

Blog Archive

Recent Posts

top ads

Unordered List

recent

Pages

Theme Support

[socialcounter] [facebook][#][215K] [twitter][#][115K] [youtube][#][215,635] [rss][#][23M] [linkedin][#][21.5K] [instagram][#][600,300]