Skip to main content

Perfect number in C, Pyramid pattern, Number is power of two or not and Sum of series in c programming

Write a C program to check whether a given number (N) is a perfect number or not?
[Perfect Number - A perfect number is a positive integer number which is equals to the sum of its proper positive divisors. For example 6 is a perfect number because its proper divisors are 1, 2, 3 and it’s sum is equals to 6.]

#include <stdio.h>
int main()
{
    int N;
    scanf("%d",&N);
int i, sum=0;
    for(i=1; i<N;i++)
    {
        if(N%i==0)
            sum+=i;
    }

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



InputOutput

8000
8000 is not a perfect number.

8128
8128 is a perfect number.

6
6 is a perfect number.

87
87 is not a perfect number.


Write a C program to find sum of following series where the value of N is taken as input 1+ 1/2 + 1/3 + 1/4 + 1/5 + .. 1/N

#include<stdio.h>
int main()
{
int N;
float sum = 0.0;
scanf("%d",&N);
int i;
for(i=1;i<=N;i++)
sum = sum + ((float)1/(float)i);
printf("Sum of the series is: %.2f\n",sum);
}



InputOutput

100
Sum of the series is: 5.19

20
Sum of the series is: 3.60

6
Sum of the series is: 2.45

50
Sum of the series is: 4.50


Write a C program to check whether the given number(N) can be expressed as Power of Two (2) or not.
For example 8 can be expressed as 2^3. 


#include <stdio.h>
int main()
{
    int N;
    scanf("%d",&N);
int temp, flag;
    temp=N;
    flag=0;
   
    while(temp!=1)
    {
        if(temp%2!=0){
            flag=1;
            break;
        }
        temp=temp/2;
    }
  
    if(flag==0)
        printf("%d is a number that can be expressed as power of 2.",N);
    else
        printf("%d cannot be expressed as power of 2.",N);
}




InputOutput

6572
6572 cannot be expressed as power of 2.

1024
1024 is a number that can be expressed as power of 2.

8
8 is a number that can be expressed as power of 2.

46
46 cannot be expressed as power of 2.



Write a C program to print the following Pyramid pattern upto Nth row. Where N (number of rows to be printed) is taken as input.*****
****
***
**
*


#include<stdio.h>
int main()
{
int N;
scanf("%d", &N); /*The value of N is taken as input from the test case */

int i,j;
for(i=N; i>0; i--)
  {
  for(j=0;j<i;j++)
    {
    printf("*");
    }
  printf("\n");
  } 
}




InputOutput

7
*******
******
*****
****
***
**
*

4
****
***
**
*

5
*****
****
***
**
*

3
***
**
*

Comments

Popular posts from this blog

c programming MCQ questions and answers

c programming MCQ  questions and answers 1. Which among the following is the most complete definition of Programming?            a) Programming is a process to solve any logical problems.            b) Programming is a process to solve any numerical problem.            c) Programming is a process to solve any real life problems.            d) Programming is the process of taking an algorithm and encoding it into a notation (using a                    programming language), so that it can be executed by a computer. Solution: (d) Programming is the process of taking an algorithm and encoding it into a notation, a programming language, so that it can be executed by a computer 2. Which of the following controls the way in which the computer system works and give means by which users can interact wit...

C program for practice

1.Write a C Program to calculates the area (floating point number with two decimal places) of a Circle given it’s radius (integer value).  The value of Pi is 3.14. #include <stdio.h> #define PI 3.14 void main() {     int radius;     float area;     /* Enter the radius of a circle */     /* The radius of the circle is taken from the test cases */      scanf("%d", &radius); area = PI * radius * radius; printf("Area of a circle = %5.2f\n", area); } Input Output 42 Area of a circle = 5538.96 9 Area of a circle = 254.34 7 Area of a circle = 153.86 50 Area of a circle = 7850.00 2.Write a C program to check if a given Number is zero or Positive or Negative Using if...else statement. #include <stdio.h> int main() {     double number;     scanf("%lf", &number);  if (number <= 0.0)     {   ...

array mcq questions in c

1. The elements of an array can be accessed by its            a) name            b) index            c) dimension            d) none of the above Solution: (b) An array is accessed by its index. 2. The elements of array are stored in contiguous memory due to             a) This way computer can keep track only the address of the first element and the addresses                     of  other elements can be calculated.             b) The architecture of computer does not allow arrays to store other than serially             c) Both             d) None Solution: (a) This way computer can keep track only the address of the first element and the addresses of other elements can be calc...