Pyramid C Prgrms
Pyramid C Prgrms
return 0;
}
Q. Write a program to generate a following numbers triangle:
(Where user entered number through keyboard, for example if num=5)
1
12
123
1234
12345
Ans.
/* c program for number triangle*/
#include<stdio.h>
#include
//
<conio.h>
int main()
{
int num,r,c;
printf("Enter loop repeat number(rows): ");
&
scanf("%d",
num);
for(r=1; r<=num; r++)
{
for(c=1; c<=r; c++)
printf("%d",c);
printf("\n");
}
//getch();
return 0;
}
Q. Write a program to generate a following
numbers triangle:
(Where user entered number through keyboard,
for example if num=5)
12345
1234
123
Ans.
12
1
1
2
3
4
5
&
scanf("%d",
num);
for(r=1; num>=r; r++)
{
for(sp=num-r; sp>=1; sp--)
printf(" ");
for(c=1; c<=r; c++)
printf("%d",c);
printf("\n");
}
getch();
return 0;
}
Q. Write a c program for following number structure :
1
22
333
4444
55555
4444
333
22
1
Ans.
/*c program for above triangle structure using while*/
#include<stdio.h>
#include<conio.h>
int main()
{
int num,n,r=1,c;
printf("Enter triangle number : ");
scanf("%d",&num);
while(num >= r)
{
c=1;
while(c <= r)
{
printf("%d",r);
c++;
}
printf("\n");
r++;
}
n=num-1;
while(n >= 1)
{
c=1;
while(c <= n)
{
printf("%d",n);
c++;
}
printf("\n");
n--;
}
getch();
return 0;
}
OR
/*c program for above number triangle using for loop*/
#include<stdio.h>
#include<conio.h>
int main()
{
int num,n,r,c;
printf("Enter triangle number : ");
scanf("%d",&num);
for(r=1; r<=num; r++)
{
for(c=1; c<=r; c++)
printf("%d",r);
printf("\n");
}
for(n=num-1; n>=1; n--)
{
for(c=1; c<=n; c++)
printf("%d",n);
printf("\n");
}
return 0;
}
Output :Enter triangle number :5
1
22
333
4444
55555
4444
333
22
1
#include<stdio.h>
int main()
{
int num,r,c,n,sp,p;
printf("Enter any number : ");
scanf("%d", &num);
for(r=1,n=num; r<=num; r++,n--)
{
for(sp=r; sp>1; sp--)
printf(" ");
for(c=r,p=n; c<=num; c++,p--)
printf("%d",p);
for(c=num-r,p=2; c>=1; c--,p++)
printf("%d",p);
printf("\n");
}
getch();
return 0;
}
Q. Write a C program to design the number triangle pyramid as:
1
121
1231
12341
123451
Ans.
/*c program for number triangle design*/
#include<stdio.h>
int main()
{
int num,r,c,z;
printf("Enter No. of rows : ");
scanf("%d", &num);
for(r=1; r<=num; r++)
{
for(c=1; c<=r; c++)
printf("%d",c);
for(z=1; z<r; z++)
{
printf("%d",z);
break;
}
printf("\n");
}
getch();
return 0;
}
#include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
void title();
class tym
{
private:
int hh,mm,ss,ms;
public:
void show()
{
if(ms>9)
{
ms=0;
ss++;
}
else if(ss>59)
{
ss=0;
mm++;
}
else if(mm>59)
{
mm=0;
hh++;
}
cout<<hh<<":"<<mm<<":"<<ss<<":"<<ms;
}
void inc()
{
ms++;
}
void cancel()
{
hh=mm=ss=ms=0;
}
tym()
{
hh=mm=ss=ms=0;
}
};
void title()
{
cout<<"\n
cout<<"\n
cout<<"\n
cout<<"\n
cout<<"\n
}
press
press
press
press
press
s
p
t
r
c
for
for
for
for
for
start ";
pause ";
stop ";
resume ";
cancel ";
void main()
{
tym t;
char opt;
clrscr();
title();
cout<<"\n\n stopwatch :- ";
opt=getch();
if((opt=='s')||(opt=='S'))
e: {
while(!kbhit())
{
clrscr();
title();
cout<<"\n\n stopwatch :- ";
t.show();
delay(100);
t.inc();
}
opt=getch();
if((opt=='p')||(opt=='P'))
{
while(!kbhit())
{
clrscr();
title();
cout<<"\n\n stopwatch :- ";
t.show();
delay(100);
}
opt=getch();
if((opt=='s')||(opt=='S'))
goto e;
else if((opt=='t')||(opt=='T'))
{
clrscr();
title();
cout<<"\n\n stopwatch :- ";
t.show();
delay(5000);
exit(1);
}
else if((opt=='c')||(opt=='C'))
{
while(!kbhit())
{
clrscr();
title();
cout<<"\n\n stopwatch :- ";
t.cancel();
t.show();
delay(100);
}
opt=getch();
if((opt=='s')||(opt=='S')||(opt=='r')||
(opt=='R'))
goto e;
else
exit(1);
}
else if((opt=='r')||(opt=='R'))
goto e;
else
exit(1);
}
else if((opt=='t')||(opt=='T'))
{
clrscr();
title();
cout<<"\n\n stopwatch :- ";
t.show();
delay(5000);
exit(1);
}
else if((opt=='c')||(opt=='C'))
{
while(!kbhit())
{
clrscr();
title();
cout<<"\n\n stopwatch :- ";
t.cancel();
t.show();
delay(100);
}
opt=getch();
if((opt=='s')||(opt=='S')||(opt=='r')||(opt=='R'))
goto e;
else
exit(1);
}
else
exit(1);
}
else
exit(1);
getch();
}