Write a program to print following pattern.
1
2 3
4 5 6
4 5 6
2 3
1
SOURCE CODE
#include<stdio.h>
int main(){
int i,j,n,k=1;
scanf("%d",&n);
for(i=1;i<=n;i++){
for(j=1;j<=i;j++){
printf("%d",k++);
if(j<i) printf(" ");
}
if(i<=n) printf("\n");
}
for(i=n;i>=1;i--){
k=i*(i-1)/2;
for(j=1;j<=i;j++){
printf("%d",++k);
if(j<i) printf(" ");
}
if(i>1) printf("\n");
}
return 0;
}
Write a program to print following pattern
Sample Input
253
Sample Output
2
2 5
2 5 3
5
5 3
3
SOURCE CODE
#include<stdio.h>
int main(){
int n,nrev=0,temp,i,j,c=0,temp2;
scanf("%d",&n);
temp=n;
while(temp!=0){
nrev=nrev*10+temp%10;
temp/=10;
c++;
}
while(nrev!=0 || c!=0){
temp=nrev;
for(i=1;i<=c;i++){
temp2=temp;
for(j=1;j<=i;j++,temp2/=10){
printf("%d%c",temp2%10,32);
}
printf("\n");
}
printf("\n");
nrev/=10;
c--;
}
}
Write a program to print following pattern
Sample Input1
3
Sample Output1
* 2 3
* 5 *
7 * *
Sample Input2
4
Sample Output2
* 2 3 *
5 * 7 *
* * 11 *
13 * * *
SOURCE CODE
#include<stdio.h>
int main() {
int num, i, j, k, prime=1,c;
scanf("%d",&num);
for(i=1;i<=num;i++){
for(j=1;j<=num;j++){
c=0;
for(k=1;k<=prime/2;k++){
if(prime%k==0)
c++;
}
if(c==1)
printf("%d",prime);
else
printf("*");
if(j<num)
printf(" ");
prime++;
}
if(i<num)
printf("\n");
}
}
Write a program to print following pattern.
1*2*3*10*11*12
4*5*8*9
6*7
SOURCE CODE
#include<stdio.h>
int main() {
int i,j,k,lt,rt,n;
scanf("%d",&n);
lt=1;
k=n*n+1;
for(i=n;i>0;i--){
for(j=i;j<n;j++)
printf("%c%c",32,32);
for(j=1;j<=i;j++){
printf("%d*",lt++);
}
rt=k;
for(j=1;j<=i;j++){
printf("%d",rt++);
if(j<i) printf("*");
}
k=k-i+1;
printf("\n");
}
}
1
2 3
4 5 6
4 5 6
2 3
1
SOURCE CODE
#include<stdio.h>
int main(){
int i,j,n,k=1;
scanf("%d",&n);
for(i=1;i<=n;i++){
for(j=1;j<=i;j++){
printf("%d",k++);
if(j<i) printf(" ");
}
if(i<=n) printf("\n");
}
for(i=n;i>=1;i--){
k=i*(i-1)/2;
for(j=1;j<=i;j++){
printf("%d",++k);
if(j<i) printf(" ");
}
if(i>1) printf("\n");
}
return 0;
}
Write a program to print following pattern
Sample Input
253
Sample Output
2
2 5
2 5 3
5
5 3
3
SOURCE CODE
#include<stdio.h>
int main(){
int n,nrev=0,temp,i,j,c=0,temp2;
scanf("%d",&n);
temp=n;
while(temp!=0){
nrev=nrev*10+temp%10;
temp/=10;
c++;
}
while(nrev!=0 || c!=0){
temp=nrev;
for(i=1;i<=c;i++){
temp2=temp;
for(j=1;j<=i;j++,temp2/=10){
printf("%d%c",temp2%10,32);
}
printf("\n");
}
printf("\n");
nrev/=10;
c--;
}
}
Write a program to print following pattern
Sample Input1
3
Sample Output1
* 2 3
* 5 *
7 * *
Sample Input2
4
Sample Output2
* 2 3 *
5 * 7 *
* * 11 *
13 * * *
SOURCE CODE
#include<stdio.h>
int main() {
int num, i, j, k, prime=1,c;
scanf("%d",&num);
for(i=1;i<=num;i++){
for(j=1;j<=num;j++){
c=0;
for(k=1;k<=prime/2;k++){
if(prime%k==0)
c++;
}
if(c==1)
printf("%d",prime);
else
printf("*");
if(j<num)
printf(" ");
prime++;
}
if(i<num)
printf("\n");
}
}
Write a program to print following pattern.
1*2*3*10*11*12
4*5*8*9
6*7
SOURCE CODE
#include<stdio.h>
int main() {
int i,j,k,lt,rt,n;
scanf("%d",&n);
lt=1;
k=n*n+1;
for(i=n;i>0;i--){
for(j=i;j<n;j++)
printf("%c%c",32,32);
for(j=1;j<=i;j++){
printf("%d*",lt++);
}
rt=k;
for(j=1;j<=i;j++){
printf("%d",rt++);
if(j<i) printf("*");
}
k=k-i+1;
printf("\n");
}
}