LOGIC PYRAMID:
SOLUTION :
#include<stdio.h>
int main()
{
int x=6,y=22,n,i,j,k=0;
scanf("%d",&n);
if(n>0 && n<=14)
{
for(i=1;i<=n;i++)
{
for(j=i;j<n;j++)
{
printf(" ");
}
for(k=1;k<=i;k++)
{
printf("%05d ",x);
x=x+y;
y=y+16;
}
printf("\n");
}
}
return 0;
}
CONSECUTIVE PRIME SUM:
#include<stdio.h>
int main()
{
int x=6,y=22,n,i,j,k=0;
scanf("%d",&n);
if(n>0 && n<=14)
{
for(i=1;i<=n;i++)
{
for(j=i;j<n;j++)
{
printf(" ");
}
for(k=1;k<=i;k++)
{
printf("%05d ",x);
x=x+y;
y=y+16;
}
printf("\n");
}
}
return 0;
}
CONSECUTIVE PRIME SUM:
Some prime numbers can be expressed as Sum of other consecutive prime numbers.
For example
5 = 2 + 3
17 = 2 + 3 + 5 + 7
41 = 2 + 3 + 5 + 7 + 11 + 13
Your task is to find out how many prime numbers which satisfy this property are present in the range 3 to N subject to a constraint that summation should always start with number 2.
Write code to find out number of prime numbers that satisfy the above mentioned property in a given range.
For example
5 = 2 + 3
17 = 2 + 3 + 5 + 7
41 = 2 + 3 + 5 + 7 + 11 + 13
Your task is to find out how many prime numbers which satisfy this property are present in the range 3 to N subject to a constraint that summation should always start with number 2.
Write code to find out number of prime numbers that satisfy the above mentioned property in a given range.
Input Format:
First line contains a number N
First line contains a number N
Output Format:
Print the total number of all such prime numbers which are less than or equal to N.
Print the total number of all such prime numbers which are less than or equal to N.
Constraints:
1. 2
Sample Input and Output
SNo. | Input | Output | Comment |
---|---|---|---|
1 | 20 | 2 | (Below 20, there are 2 such numbers: 5 and 17). 5=2+3 17=2+3+5+7 |
2 | 15 | 1 |
SOLUTION:
#include<stdio.h>
int conprime(int);
int main()
{
int n1=2 , n2, i, flag,o=0;
scanf("%d",&n2);
while (n1 <= n2)
{
flag=0;
for(i=2; i<=n1/2; ++i)
{
if(n1%i == 0)
{
flag=1;
break;
}
}
if (flag == 0)
{
if(n1>2)
{
if(conprime(n1) == 0)
{
o++;
}
}
}
++n1;
}
printf("%d",o);
return 0;
}
int conprime(int n)
{
int a[1000],s=0,flag,i,n1=2,j=0,m=0,b;
while (n1 < n)
{
flag=0;
for(i=2; i<=n1/2; ++i)
{
if(n1%i == 0)
{
flag=1;
break;
}
}
if (flag == 0)
{
a[j] = n1;
j++;
}
++n1;
}
for(b=0;b<j;b++)
{
s+=a[b];
if(s==n)
{
m=1;
break;
}
else
{
m=0;
}
}
if(m==1)
{
return 0;
}
else return 1;
}
error
ReplyDeletemy program is fully complied and run & I have checked in various online C compilers as well...
ReplyDeleteso please tell me the error :(
ReplyDeletein python :
ReplyDeletex,y=6,22
size = 5
for i in range(0, size):
for j in range(0,(size-i)*3):
print(end=" ")
for j in range(0, i+1):
print(format(x, '05'), end=" ")
x = x + y;
y = y + 16;
print(end='\n')
Use this code for Logic Pyramid:
ReplyDelete#include
using namespace std;
int main()
{
int n, t;
cin >> t;
while (t > 0) {
cin >> n;
int first = 6;
int second = 22;
if (n > 0 && n <= 14) {
for (int i = 1; i <= n; i++) {
for (int j = 0; j < (n - i) * 3; j++) {
cout << " ";
}
for (int k = 1; k <= i; k++) {
printf("%05d", first);
first += second;
second += 16;
cout << " ";
}
cout << endl;
}
}
t--;
}
return 0;
}