0% found this document useful (0 votes)
43 views13 pages

Pyramid C Prgrms

The document contains C/C++ code for a stopwatch program. The program uses a class called 'tym' to keep track of time in hours, minutes, seconds and milliseconds. It displays the time and increments the milliseconds each time. The user can start, pause, resume and cancel the stopwatch using keyboard inputs. On start, it continuously displays and increments the time until another key is pressed. It allows pausing and resuming the timer. Cancel resets the time to zero.

Uploaded by

VeerraghavaReddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views13 pages

Pyramid C Prgrms

The document contains C/C++ code for a stopwatch program. The program uses a class called 'tym' to keep track of time in hours, minutes, seconds and milliseconds. It displays the time and increments the milliseconds each time. The user can start, pause, resume and cancel the stopwatch using keyboard inputs. On start, it continuously displays and increments the time until another key is pressed. It allows pausing and resuming the timer. Cancel resets the time to zero.

Uploaded by

VeerraghavaReddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Q.

Write a c program to print the following star structure:


*********
*******
*****
***
*
***
*****
*******
*********
Ans.
/*c program for print the star structure*/
#include<stdio.h>
#include<conio.h>
int main()
{
int num,n,r,c,sp;
printf("Enter number of rows: ");
scanf("%d", &num);
printf("\n");
n=num;
for(r=1; r<=num; r++)
{
for(sp=1; sp<=r; sp++)
printf(" ");
for(c=1; c<=n; c++)
printf("*");
for(c=num-r; c>=1; c--)
printf("*");
n--;
printf("\n");
}
for(r=2; r<=num; r++)
{
for(sp=num-r+1; sp>=1; sp--)
printf(" ");
for(c=1; c<=r; c++)
printf("*");
for(c=r-1; c>=1; c--)
printf("*");
printf("\n");
}

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

/*c program for number triangle pyramid*/


#include<stdio.h>
#include<conio.h>
int main()
{
int num,c;
printf("Enter loop repeat number(rows): ");
scanf("%d",&num);
for(; num>=1; num--)
{
for(c=1; c<=num; c++)
printf("%d",c);
printf("\n");
}
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
Ans.
/*c program for number triangle pyramid*/
#include<stdio.h>
#include<conio.h>
int main()
{
int num,r,c,sp;
printf("Enter loop repeat number(rows): ");

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

Q. Write a program to display the string INDIA in the following fashion:


I
IN
IND
INDI
INDIA
INDIA
INDI
IND
IN
I
Ans.
/*program to display the string as given fashion*/
#include<stdio.h>
#include<conio.h>
int main()
{
int x,y;
static char str[]="INDIA";
for(x=0; x<5; x++)
{
y=x+1;
printf("%-5.*s\n",y,str);
}
for(x=4; x>=0; x--)
{
y=x+1;
printf("%-5.*s\n",y,str);
}
getch();
return 0;
}

Q. Write a C program to print the following half-square number triangle C program


as :
543212345
4321234
32123
212
1
Ans.
/*c program for half-square number triangle*/

#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;
}

Q. Write a C program to print the following number pyramid or Floyd triangle:


1
23
456
7 8 9 10
Ans.
/*c program to print the number pyramid*/
#include<stdio.h>
int main()
{
int num,r,c,sp,i=1;
printf("Enter any number : ");
scanf("%d", &num);
for(r=1; r<=num; r++)
{
for(sp=1; sp<=num-r; sp++)
printf(" ");
for(c=1; c<=r; c++,i++)
printf("%d ",i);
printf("\n");
}
return 0;
}

WRITE A C++ PROGRAM FOR STOPWATCH

#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();
}

You might also like