You are on page 1of 5

Assignment # 5-4

1. Print following sequence for n row

1 10 100 1000

2 20 200 2000

9 90 900 9000

#include <iostream>

using namespace std;

int main() {

int n;

cin>>n;

for(int i=1; i<=n; i++){

for(int j=i; j<=n*1000; j=j*10){

cout<<j<<"\t";

cout<<endl;

return 0;

2. Write a program to enter a number and show how many times a digit appeared in number
e.g., n = 23531
1 =1 times
2 = 1 times
3= 2 times
5 = 1 times
#include <iostream>
using namespace std;
int main() {
int n,r,d,count;
cin>>n;
int mpos=1;
for(int i=n; i>0; i=i/10){
r=i%10;
int spos=1;
count=0;
for(int j=n; j>0; j=j/10){
d=j%10;
if(r==d){
if(spos<mpos)
break;
count++;
}
spos++;
}
if(count>0){
cout<<r<<" occurs "<<count<<" times "<<endl;
}
mpos++;
}
return 0;
}
3. Write a program to enter an expression and display its result whne = is pressed/

#include <iostream>

#include <conio.h>

using namespace std;

int main() {

char ch, op, flag='f';

int n1=0, n2=0;

for(;;){

ch=getche();

if(ch>=48 && ch<=57){

n2=n2*10+ch-48;

else{

if(flag=='f'){

n1=n2;

op=ch;
n2=0;

flag='t';

continue;

switch(op){

case '+':

n1=n1+n2;

break;

case '-':

n1=n1-n2;

break;

case '*':

n1=n1*n2;

break;

case '/':

n1=n1/n2;

break;

default:

cout<<"Invalid operator";

n2=0;

op=ch;

if(ch=='='){

break;

cout<<n1;
return 0;

4. Wrtie a program to print pascal’s trangle using formula

n! / k! (n – k )!

Where n is row number and k is column number, bith starting at 0

5.

#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
for(int i=1; i<=n; i++){
char ch=65;
for(int j=1; j<=n*2-1; j++){
if(j>=n-i+2 && j<=n+i-2){
cout<<" ";
}
else{
cout<<ch;
}
if(j<n)
ch++;
else
ch--;
}
cout<<endl;
}
return 0;
}

You might also like