save natures

save natures

Monday 13 July 2015

interview programs

Write a program to print the following output for the given input. You can assume the string is of

odd length

Eg 1: Input: 12345

       Output:

1       5

  2   4

    3

  2  4

1      5

Eg 2: Input: geeksforgeeks

         Output:

g                         s

  e                     k

    e                 e

      k             e

        s         g

          f      r

             o

          f     r

        s         g

      k             e

    e                 e

  e                      k

g                          s


code:

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{

int i,j,l;

char a[100];

scanf("%s",&a);

l=strlen(a);

for(i=0;i<l;i++)

{

for(j=0;j<l;j++)

{

if(i==j)

{

printf("%c",a[i]);

}

else if(i+j==l-1 && i!=j)

{

printf("%c",a[j]);

}

else

{

printf(" ");

}

}

printf("\n");

}

getch();

}

Using Recursion reverse the string such as

Eg 1: Input: one two three

      Output: three two one

Eg 2: Input: I love india

      Output: india love I

Code:

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{

char a[1000];

int i,j,count=0,d;

clrscr();
d=strlen(a);
gets(a);

for(i=strlen(a)-1;i>=0;i--)

{

if(a[i]==' '||a=='\0')
{
for(j=i;j<d;j++)
{
printf("%c",a[i]);
}
d=i;
}
 
}

getch();

}






 Write a program to give the following output for the given input

Eg 1: Input: a1b10

       Output: abbbbbbbbbb

Eg: 2: Input: b3c6d15

          Output: bbbccccccddddddddddddddd

The number varies from 1 to 99
.

code:

#include<stdio.h>

#include<string.h>

int main()

{

char a[100],t;

int i,j,x,f;

printf("Enter the string:");

scanf("%s",&a);

for(i=0;i<strlen(a);i=i+2)

{

t=a[i];

if((a[i+2]-48)>=0 && (a[i+2]-48)<=9)

{

int x;

x=((a[i+1]-48)*10)+(a[i+2]-48);

for(f=0;f<x;f++)

{

printf("%c",t);

}

i++;

}

else

{

for(j=0;j<a[i+1]-48;j++)

{

printf("%c",t);

}

}

}

return 0;

}




 Write a program to sort the elements in odd positions in descending order and elements in

ascending order


Eg 1: Input: 13,2 4,15,12,10,5

        Output: 13,2,12,10,5,15,4

Eg 2: Input: 1,2,3,4,5,6,7,8,9

        Output: 9,2,7,4,5,6,3,8,1

code:

#include<conio.h>

#include<string.h>

void main()

{

int a[100];

int i,j,s,temp;

clrscr();

printf("Enter the size of array:");

scanf("%d",&s);

for(i=0;i<s;i++)

{

scanf("%d",&a[i]);

}

for(i=0;i<s;i+=2)

{

for(j=i+2;j<s;j+=2)

{

if(a[i]<a[j])

{

temp=a[i];

a[i]=a[j];

a[j]=temp;

}

}

}

for(i=1;i<s;i+=2)

{

for(j=i+2;j<s;j+=2)

{

if(a[i]>a[j])

{

temp=a[i];

a[i]=a[j];

a[j]=temp;

}

}

}

printf("array");

for(i=0;i<s;i++)

{

printf(" \t%d",a[i]);

}

getch();

}








ROMAN TO DECIMAL:-


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int digitValue(char);

int main(){

    char rn[1000];
    int i=0;
    long int number =0;
  
   
    scanf("%s",rn);
  
    while(rn[i]){
       
         if(digitValue(rn[i]) >= digitValue(rn[i+1]))
             {
             number = number + digitValue(rn[i]);
         }
         else{
             number = number + (digitValue(rn[i+1]) - digitValue(rn[i]));
             i++;
         }
         i++;
    }
       
    printf("%ld",number);
    return 0;
}
int digitValue(char c){
   int value=0;
     switch(c){
         case 'I': value = 1; break;
         case 'V': value = 5; break;
         case 'X': value = 10; break;
         case 'L': value = 50; break;
         case 'C': value = 100; break;
         case '\0': value = 0; break;
         default: value = -1;
    }

    return value;
}





TAXI APPLICATION:-






#include<stdio.h>
#include<conio.h>
#include<math.h>
#define pf printf
#define c() clrscr()


int check2(char,int);
int check_picpnt(char,int);
int booking();
void details();
int static id;
char dist[6]={'A','B','C','D','E','F'};           /* pick up points */
struct taxi{
     int index;                              /*for each customer*/
     char pic_pnt[10];
     char drp_pnt[10],cur_pos;                   /* struct variables */
     int str_time[10],cust_id[10],end_time[10];
     int wage[10],t_wage;
}t[5];


void main()                                      /* main function */
{
  c();
  for(int i=0;i<5;i++)
  {                                              /* initial position allot */
  t[i].cur_pos='A';
  }
  menu:
  pf("\t\t\t\tWELCOME TO TAXI BOOKING");
  pf("\n\n\n\n\n\t\t MENU");                    /* front page */
  pf("\n\n\n\t\t\t1.TAXI BOOKING");
  pf("\n\t\t\t2.DETAILS\n");
  pf("\t\t\t3.EXIT\n");
  int opt;
  pf("\n\n\t\t\tEnter your option:");
  scanf("%d",&opt);
  switch(opt)
  {
   case 1:
   booking();                               /* calling respective functions */
   c();
   goto menu;
   case 2:
   details();
   getch();
   c();
   goto menu;
   case 3:
   break;
   default:
   pf("Wrong option!!!");
   getch();
   c();
   goto menu;
  }
  getch();
}



int booking()
{                                             /*booking function,second page*/
c();
char p_pnt,d_pnt;
int tme,wag,taxi=0;
pf("\t\t\tbooking menu");
pf("\n\ncustomer_id %d",++id);
pf("\n\npickup_pnt:");
getchar();
scanf("%c",&p_pnt);                          /* getting booking details from user using temp variables */
pf("\ndrop_pnt:");
getchar();
scanf("%c",&d_pnt);
pf("\ntime:");
getchar();
scanf("%d",&tme);
taxi=check_picpnt(p_pnt,tme);            /* checking taxi availability by calling function */
if(taxi!=777)
{
int x=t[taxi].index;
pf("\n\ntaxi %d is alloted",taxi+1);      /* Assigning details to struct variables if function return respective taxis */
pf("\n\nthank for booking");                   /* display alloted taxi number */
int cost=((abs(p_pnt-d_pnt)*15)-5)*10+100; /* calculating cost based on distance*/
t[taxi].pic_pnt[x]=p_pnt;
t[taxi].drp_pnt[x]=d_pnt;
t[taxi].cust_id[x]=id;
t[taxi].wage[x]=cost;
t[taxi].cur_pos=d_pnt;
t[taxi].t_wage=t[taxi].t_wage+cost;
t[taxi].end_time[x]=abs(p_pnt-d_pnt)+tme; /*calculating taxi free time */
t[taxi].str_time[x]=tme;
t[taxi].index++;                    /*increasing for each customer*/
}
else
{pf("\n\nOOPs failed");}            /* taxi is not available*/
getch();
return 0;
}


int check_picpnt(char pic_pnt,int p_tme) /*function to check the pick point*/
{
int flag=0,min=10000,min_ind,get;      /* set the value for min*/
for(int i=0;i<5;i++)
{
 if(pic_pnt==t[i].cur_pos)  /*check the pickpoint of the customer with the current position of the taxi*/
 {
   if(t[i].end_time[(t[i].index-1)]<=p_tme)     /* check the previously allocated time of the customer with the current customer*/
     {
 if(t[i].t_wage<min) /* calculate the minimium wage who earned that day */
  {
   min=t[i].t_wage;
   min_ind=i;/* assigning the minimum index*/
   flag=1;
  }
     }
 }
}
  if(flag==1)
  {return (min_ind);}  /*returning the respective taxi index to the booking function */

  if(flag==0) /*if the taxi is not in that current position*/
  {
    for(int j=0;j<6;j++) /*getting the index of the pick point  */
      {
       if(pic_pnt==dist[j])
       {get=j;
       }
      }
     int k,m,m1,indx,indx1;
  for(j=get-1,k=get+1;j>=0||k<=6;j--,k++) /* checking both the sides of the pickpoint if the taxi is available or not*/
    {
      if(j>=0)
      {
 indx=check2(dist[j],p_tme);/* passing the neighbour value and pick up time */
   if(indx!=-1)
   m=t[indx].t_wage; /* storing the minimum wage in that current point */
   else
   m=10000;
      }
     if(k<=6)
      {
       indx1=check2(dist[k],p_tme);  /* passing the neighbour value and pick up time */
  if(indx1!=-1)
  m1=t[indx1].t_wage;  /* storing the minimum wage in that current point */
  else
  m1=10000;
     }
     if(m1!=10000||m!=10000) /* if both wages of taxi is same then return the lowest index of taxi */
      {
   if(m<m1)
        {return (indx);}
   else if(m>m1)
  {return (indx1);}
   else
  {
  if(indx<indx1)
        {return (indx);}
  else
  {return (indx1);}
  }
     }
   }
 }
return(777);/*returning the error code if no taxi is availble*/
}

int check2(char pic_pnt,int p_tme)  /*function to check the pick point*/
{
int flag=0,min=10000,min_ind,get; /* set the value for min*/
for(int i=0;i<5;i++)
{
 if(pic_pnt==t[i].cur_pos)   /*check the pickpoint of the customer with the current position of the taxi*/
 {
    if(t[i].end_time[(t[i].index-1)]<=p_tme)   /* check the previously allocated time of the customer with the current customer*/
    {
     if(t[i].t_wage<min) /* calculate the minimium wage who earned that day */
      {
 min=t[i].t_wage;
 min_ind=i;       /*assigning minimum index*/
 flag=1;
      }
   }
 }
}
if(flag==1)
{return (min_ind);}
if(flag==0)            /*returning minimum index to booking function*/
{return (-1);}
return 0;
}


void details()
  {
   int i,j;
   pf("\t\t\t\tBOOKING DETAILS");
   for(i=0;i<3;i++)
   {
     pf("\n\nTaxi %d details",i+1);   /*print the details based on taxi*/
      pf("\nTotal wage:%d",t[i].t_wage);
     pf("\nc_id  p_pnt  d_pnt  s_tm  e_tm   wage\n");
     for(j=0;j<3;j++)/*print the details based on cust id*/
     {
       pf("%d      %c       %c    %d     %d      %d ",t[i].cust_id[j],t[i].pic_pnt[j],
 t[i].drp_pnt[j],t[i].str_time[j],t[i].end_time[j],t[i].wage[j]);
       pf("\n");
     }
   }
}
 
 
 
 
 
PROGRAM:
 
INPUT:
tamil
 
OUTPUT:
taamiil
 
EXPLAINATION:
given string print the vowels 2 times; 

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,j,l;
char str[100]; 
scanf("%s",str); 
l=strlen(str); 
for(i=0;i<l;i++)
{
if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u')
{ 
for(j=0;j<2;j++)
{
printf("%c",str[i]); 
} 
}
else
{

printf("%c",str[i]}; 
}
}
 
 
 
 
 
PATTERN:
Print the word with odd letters as
Pattern :


 #include <stdio.h>

#include <string.h>

#include <math.h>

#include <stdlib.h>

int main() {

    char a[100];

    int i,j,b,k,n;

    scanf("%s",a);

    n=strlen(a);

    for(i=0,j=n-1;i!=j;i++,j--)

        {

        printf("%c",a[i]);

        for(k=1;k<n/2;k++)

            {

            printf(" ");

        }

        printf("%c",a[j]);

        n=n-2;

        printf("\n");

      

    }

    n+=2;

   printf("%c\n",a[i]);

     for(i--,j++;i>=0;i--,j++)

        {

        printf("%c",a[i]);

        for(k=1;k<n/2;k++)

            {

            printf(" ");

        }

        printf("%c",a[j]);

        n=n+2;

        printf("\n");

    }

    return 0;
  }
 
 
 
 
 
 
 
 
Problem Statement
Given an array , find the Mth largest and Kth smallest number. Sorting should not be used.
Input Format
The first line contains an integer N which is the size of the array.
N>0
The second line has N integers of an array.
The third and fourth line have an integers M and K respectively.
0< M,K<=N
Output Format
Print the Mth largest element and Kth smallest element.
Sample Input
5
23 1 56 7 200
2
3
Sample Output
56
23
 
 
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

    int i,j,n,val[10],k,temp=0,m,c,d;
    scanf("%d",&n);
    
    for(m=0;m<n;m++)
    {
    scanf("%d",&val[m]); 
    }
    for(i=0;i<n;i++)
        {
        for(j=i+1;j<n;j++)
            {
            if(val[i]>val[j])
                {
                temp=val[i];
                val[i]=val[j];
                val[j]=temp;
                
            }
        
        
        }
        
        }
   
    scanf("%d",&d);
     scanf("%d",&c);
    
     printf("\n%d",val[n-d]);
         printf("\n%d",val[c-1]);
   
    
    
    return 0;
} 
 
 
 
 
 
 
 
 
Problem Statement
Given a square matrix(n is odd) , print the elements in spiral order starting from the center till array[0][0]th element.
Input Format
The first line contains an integer N which represents row and column size.
The next N lines contains N elements which represents the array values.
Output Format
Print the Spiral order
Sample Input
3
1 2 3
4 5 6
7 8 9
Sample Output
5 4 7 8 9 6 3 2 1
 
 
 
 
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

  int z,a[100][100],i,k=0,l=0,n,m,j,b[100],g=0,h,p,natu;
    scanf("%d",&n);
    m=n;
    natu=n;
    for(i=0;i<n;i++)
        {
        for(j=0;j<n;j++)
            {
            scanf("%d",&a[i][j]);
        }
    }
    z=(n/2)+1;
for( p=0;p<z;p++){
        for (i=l;i<n;i++)
        {
            b[g]=a[k][i];
            g++;
        }
        k++;
        for (i = k; i < m; i++)
        {
            b[g]=a[i][n-1];
            g++;
           
           
        }
        n--;
 
       if ( k < m)
        {
            for (i = n-1; i >= l; i--)
            {
          b[g]=a[m-1][i];
                g++;
            }
         
            m--;
        }
      
        if (l < n)
        {
            for (i = m-1;i>=k;i--)
           {
               
             b[g]=a[i][l];
           g++;
            }
            l++;   
        }
    }
     for(h=natu*natu-1;h>=0;h--)
          {
          printf("%d ",b[h]);
      }
    return 0;

}
 
 
 
Output Format
Print the Spiral order
Sample Input
3
3
1 2 3
4 5 6
7 8 9
Sample Output
123698745


#include<stdio.h>
main()
{
int a[20][20],i,j,n,m,p,q,k=0;
printf("\n Enter Order Of matrix");
scanf("%d%d",&m,&n);
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);

p=m;
q=n;
i=j=1;

while(k<p*q)
{
for(;j<=n&&k<p*q;j++)
{
printf("%d ",a[i][j]);
k++;
}
j--;
i++;
for(;i<=m && k<p*q;i++)
{
printf("%d ",a[i][j]);
k++;
}
i--;
j--;
for(;j>=i-m+1 && k<p*q;j--)
{
printf("%d ",a[i][j]);
k++;
}
j++;
i--;
for(;i>1 && k<p*q;i--)
{
printf("%d ",a[i][j]);
k++;
}
if(k<p*q)
{
j++;
i++;
n--;
m--;
}
}
}

(or)
#include<stdio.h>
int main()
{
     int a[10][10],s;
     int m,i,j=0,num,p;
    // printf("Enter the square matrix size ");
     scanf("%d",&s);
    // printf("\nyour matrix is of %d*%d\n",s,s);
     //printf("\nEnter the value for the matrix\n\n");

     for(i=0; i<s; i++)
     {
          for( j=0; j<s; j++)
          scanf("%d",&a[i][j]);
     }

     printf("\n\n");
     p = s;

     j=0;
     for(i=p-1; i > 0;i--,j++)
     {
          for(num=j; num < i; num++)
          printf("%d ", a[j][num]);
          for(num=j; num < i; num++)
          printf("%d ", a[num][i]);
          for(num=i; num > j; num--)
          printf("%d ", a[i][num]);
          for(num=i; num > j; num--)
          printf("%d ", a[num][j]);
         //i=0;
     }

     m = (p-1)/2;
     if (p % 2 == 1) printf("%d", a[m][m]);
     printf("\n\n");
   
     return 0;
}




program:
To output the given number for the given input which is a string(not case sensitive).
Input : B
Output : 2
Input Format
The first line has an integer T. T testcases follow.
The next T lines follow a string S on each line.
Length(S)>0
Output Format
Print the sequence number.
Sample Input
3
ac
Ab
ALL
Sample Output
29
28
1000


 #include <stdio.h>
#include<conio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
clrscr();
int a,testcase,i,len,temp,j,add=0;

    scanf("%d",&testcase);
    for(i=0;i<testcase;i++)
    {
    char str[20];
    scanf("%s",str);
    len=strlen(str);
    for(j=0;j<len;j++)
        {
        temp=str[j]-96;
        if(temp<0)
        {
        temp=temp+32;
        }
    add=add+temp*pow(26,len-j-1);

    }
    printf("\n%d",add);
    add=0;
    }
    getch();
    return 0;
}



Problem Statement
Given a boolean matrix mat[M][N] of size M X N, modify it such that if a matrix cell mat[i][j] is 1 (or true) then make all the cells of ith row and jth column as 1.
Input Format
First Line contains an integer T,T testcases follow
Each testcase contains two integers M and N .
The next M lines contains N integers,array[M][N]
Output Format
Print testcase number followed by its output
Sample Input
3
2 2
1 0
0 0
2 3
0 0 0
0 0 1
3 4
1 0 0 1
0 0 1 0
0 0 0 0
Sample Output
1
1 1
1 0
2
0 0 1
1 1 1
3
1 1 1 1
1 1 1 1
1 0 1 1
 #include<stdio.h>
 int main()
 { 

 int f,i,j,n,a[10][10],v[10][10],k,l,t,t1,q,w,c;
 scanf("%d",&n);
  for(f=0;f<n;f++)
   {
   scanf("%d",&t);
   scanf("%d",&t1);
    for(i=0;i<t;i++)
    {
     for(j=0;j<t1;j++)
      {

      scanf("%d",&a[i][j]);
     v[i][j]=a[i][j];
    }
  }
    for(k=0;k<t;k++)
  {
    for(l=0;l<t1;l++)
    {
    if(a[k][l]==1)
     {
     q=k;
     w=l;
    int b;
    for(b=0;b<t1;b++)
      {
      v[q][b]=1;
//printf("\na[%d][%d]=%d",q,b,a[q][b]);
     }
        for(c=0;c<t;c++)
        {
        v[c][w]=1;
//printf("\na[%d][%d]=%d",c,w,a[c][w]);
        }
       
     }
     }
       }
  for(i=0;i<t;i++)
    {
     for(j=0;j<t1;j++)
     {
      printf("%d ",v[i][j]);
         v[i][j]=0;
    }      printf("\n");
      
      
  }
  }
return 0;
}
 
 
 
 
Problem Statement
You are given with a string. Find the character count. It is case sensitive. Atlast delete the characters with maximum count.
Input Format
A Single line contains a string s
length(s)>0
Output Format
print the resultant string
Sample Input
abc AbC aac
Sample Output
bc AbC c
Explanation
a 3
b 2
c 2
A 1
C 1
delete all 'a' and print the string



#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
int h[52]={0},len,i,j,temp,var;
    char a[1000];
    scanf("%[^\n]s",a);
    len=strlen(a);
    for(i=0;i<len;i++)
        {
        if(a[i]>96){
      h[a[i]-97]++;
        }
        else{
            h[a[i]-39]++;
        }
    }
    temp=h[0];
    for(i=1;i<52;i++)
        {
    if(temp<h[i]){
        temp=h[i];
    }
       }
        for(i=1;i<52;i++)
        {
            if(temp==h[i]){
               var=i;
                i=52;
            }
    }
    if(var<26){
        var=var+97;
    }
    else {
        var=var +39;
    }
    for(i=0;i<len;i++)
        {
        if(a[i]!=var){
            printf("%c",a[i]);
    }
    }
   
    return 0;
}



Problem Statement
Check whether the given string can form "Z" pattern in a n*n grid
Input Format
First line contains an integer T,T testcases follow
Each testcase contains a string s
Output Format
If the string forms Z pattern in n*n grid print the pattern,else print "NO"
Sample Input
2
Zohocorporationteam
hacker

Sample Output
z  o  h  o  c  o  r

               p

            o

         r

      a

   t

i  o  n  t  e  a  m
NO
Explanation
In testcase 1, the string "Zohocorporationteam" forms "Z" pattern in 7*7 grid.
In testcase 2,the string "hacker" could not form "Z" pattern in n*n grid


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

  int i,j,len,n,k=0,T,te;
    char a[100],nat[100][100];
    scanf("%d",&T);
    for(te=0;te<T;te++)
        {
    scanf("%s",a);
    len=strlen(a);
    n=(len+2)/3;
   
    if((len+2)%3==0)
        {
    for(i=0;i<n;i++)
        {
        for(j=0;j<n;j++)
            {
            nat[i][j]=' ';
        }}
       
        for(i=0,j=0;j<n;j++)
            {
            nat[i][j]=a[k];
            k++;
        }
for(i=1,j=n-2;i<n-1,j>0;i++,j--)
    {
    nat[i][j]=a[k];
    k++;
}
for(i=n-1,j=0;j<n;j++)
    {
    nat[i][j]=a[k];
    k++;
}
   for(i=0;i<n;i++)
       {
       for(j=0;j<n;j++)
           {
           printf("%c",nat[i][j]);
       }
       printf("\n");
   }
    }
    else
        {
        printf("\nNO\n");
    }}
    return 0;
}




ONLINE  BOOK SHOPPING: 



#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
void menu();
void cart(int);
typedef struct ebay
{
int id;
char name[100];
};
void main()
{ clrscr();
menu();
getch();
}
void card(int w)
{

int a[17],i,j,n,c[20],temp=0,temp1=0,temp2=0;
printf("\nenter the card no\n");
for(i=0;i<16;i++)
{
scanf("%d",&a[i]);
}
for(i=15,j=14;i>=0,j>=0;i--,j--)
{
temp=temp+a[i];
i--;
temp1=a[j]*2;
temp2=temp2+(temp1/10+temp1%10);
j--;
}
if((temp+temp2)%10==0)
{
//printf("valid card");
float price=w*100;
float discount, money=rand()%1000;
discount=price*((rand()%100)/(float)100);
if(money<price)
{
printf("\ninsuffecent money\n");
}
{
printf("\price:%f",price);
printf("\nDiscount:%f",discount);
printf("\ntotal:%f",price-discount);
printf("\nbalanccard amount:%f",money-price);
}
}
else
{
printf("\ninvalid card");
}
}
void menu()
{
ebay a[10];
int n;
printf("*******ONLINE BOOK STORE********************");
printf("\nMENU\n1.BOOK\n2.exit\n");
scanf("%d",&n);
printf("*********************************************\n");
switch(n)
{
case 1:
int w,i;
printf("no of item=");
scanf("%d",&w);
printf("\nenter the item ID and NAME\n");
for(i=0;i<w;i++)
{
scanf("%d%s",&a[i].id,a[i].name);
}
printf("***********************************************\n");
for(i=0;i<w;i++)
{
printf("\nitem id=%d\nitem name=%s",a[i].id,a[i].name);
}
printf(" \n************************************************");
int v;
printf("\n0.cash\n1.card\n");
scanf("%d",&v);
printf("*************************************************");
if(v)
{
card(w);
}
else
{
int price=w*100;
printf("\nprice=%d",price);
printf("\n-----------------------------------------------");
printf("\n--------------------WELLCOME---------------------");
}
break;
case 2:
exit(0);
break;
default:
printf("\nselect correct choise");
}
}


Problem Statement
Display the given pattern.
Input Format
Given an integer N.
Output Format
Print the pattern.
Sample Input
4
Sample Output
        0

      1 0 1

    2 1 0 1 2       

  3 2 1 0 1 2 3

4 3 2 1 0 1 2 3 4

4 3 2 1 0 1 2 3 4

  3 2 1 0 1 2 3

    2 1 0 1 2 

      1 0 1

        0 
 
 
 
 
 
 
#include<stdio.h>
int main()
    {
    int i,j,n;
    scanf("%d",&n);
    for(i=0;i<=n;i++)
        {
        for(j=n-i;j>=0;j--)
            {
            printf(" ");
        }
    for(j=i;j>=0;j--)
        {
        printf("%d",j);
    }
    
    for(j=1;j<=i;j++)
        {
        printf("%d",j);
    }
    printf("\n");
    
    }

    for(i=0;i<=n;i++)
        {
        for(j=i;j>=0;j--)
            {
            printf(" ");
        }
    for(j=n-i;j>=0;j--)
        {
        printf("%d",j);
    }
    
    for(j=1;j<=n-i;j++)
        {
        printf("%d",j);
    }
    printf("\n");
    
    }

} 
  

14 comments:

  1. Replies
    1. Yes please send me tomorrow we r having zoho placement

      Delete
  2. Can u pls say the flow for a1b5........
    Pls

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. logic is ok but minor mistakes have to be resolved

    ReplyDelete
  5. Thanks a lot need more programs.

    ReplyDelete
  6. Eg 1: Input: a1b10

    Output: abbbbbbbbbb

    Eg: 2: Input: b3c6d15

    Output: bbbccccccddddddddddddddd

    The number varies from 1 to 99.
    -------------------------------------
    #include
    #include

    using namespace std;
    void print(char,int);

    int main(){

    string inputString;
    int i=0, num=0;
    bool isDoubleDigit = false;
    cout<<"\n Enter the string = ";
    cin>>inputString;
    while(inputString[i] != '\0'){
    if(inputString[i] >= 48 && inputString[i] <=57) {
    num = inputString[i] - 48;
    if(inputString[i+1]>=48 && inputString[i+1]<=57 ){
    num = (num*10) + inputString[i+1] - 48;
    isDoubleDigit = true;
    i++;
    }
    } else {

    cout<<inputString[i];
    }

    if(num){
    int index = i - (isDoubleDigit ? 2 : 1);
    print(inputString[index <= -1 ? 0 : index],num);
    num=0;
    }
    i++;

    }

    }

    void print(char letter, int num) {
    for(int j=1; j<num; j++)
    cout<<letter;
    }

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete