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);
}
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.
[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);
}
Input | Output | |
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);
}
Input | Output | |
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);
}
Input | Output | |
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");
}
}
Input | Output | |
7 | ******* ****** ***** **** *** ** * | |
4 | **** *** ** * | |
5 | ***** **** *** ** * | |
3 | *** ** * |
Comments
Post a Comment