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...

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...

Introduction to c language

Introduction to c C is a programming language that lets us give a computer very specific commands. C was invented in 1972. It's one of the oldest languages to be used even today! It is one of the most widely used programming languages in the world. It has influenced many popular languages, especially c++. But why do we still use such an old programming language? why not something newer and easier to understand,like python? Let's find out. History of C Language History of C language  is interesting to know. Here we are going to discuss a brief history of the c language. C programming language  was developed in 1972 by Dennis Ritchie at bell laboratories of AT&T (American Telephone & Telegraph), located in the U.S.A. Dennis Ritchie   is known as the   founder of the c language . It was developed to overcome the problems of previous languages such as B, BCPL, etc. Initially, C language was developed to be used in  UNIX operating sys...