Saturday, July 28, 2012

Number triangle

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


1
12
123
1234
12345
1234
123
12
1


Ans.


/*c program for print the number pyramid as specific given design*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int r,c,n;
 printf("Enter loop repeat number(rows): ");
 scanf("%d", &n);
 for(r=1; r<=n; r++)
 {
   for(c=1; c<=r; c++)
      printf("%d",c);
   printf("\n");
 }
 for(r=n; r>1; r--,n--)
 {
   for(c=1; c<n; c++)
      printf("%d",c);
   printf("\n");
 }
 getch();
 return 0;
}


/**************Output**************/


Output of number pyramid C program
Screen shot for number triangle C program

Thursday, July 26, 2012

Number pyramid

Q. Write a C program to print the following number structure:


          1
         2 2
        3 3 3
       4 4 4 4
      5 5 5 5 5
     6 6 6 6 6 6


Ans.


/*c program for print the following number triangle*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int r,c,sp,n=6;
 for(r=1; r<=6; r++)
 {
  for(sp=1; sp<=n; sp++)
     printf(" ");
  for(c=1; c<=r; c++)
  {
     printf("%d",r);
     printf(" ");
  }
  printf("\n");
  n=n-1;
 }
 getch();
 return 0;
}


/************Output************/


Output of number pyramid C program
Screen shot for number triangle C program

Tuesday, July 24, 2012

Number structure

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


           1
         1 2 3
       1 2 3 4 5
     1 2 3 4 5 6 7
   1 2 3 4 5 6 7 8 9


Ans.


/*c program for above number triangle codes*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int r,c,sp,num;
 printf("Enter loop repeat number(rows): ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
 {
  for(sp=1; sp<=num-r; sp++)
     printf("  ");   //it is 2 blank space
  for(c=1; c<=2*r-1; c++)
     printf(" %d",c);
  printf("\n");
 }
 getch();
 return 0;
}


/**************Output**************/


Output of number pyramid C program
Screen shot for number triangle C program

Number pyramid

Q. Write a C program to print the following number structure:


          1
         1 2
        1 2 3
       1 2 3 4
      1 2 3 4 5 
     1 2 3 4 5 6


Ans.


/*c program to print the above number pyramid*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int r,c,sp,num;
 printf("Enter loop repeat number(rows): ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
 {
   for(sp=num; sp>=r; sp--)
      printf(" ");
   for(c=1; c<=r; c++)
      printf(" %d", c);
   printf("\n");
 }
 getch();
 return 0;
}


/************Output************/


Output of C program for number pyramid C program
Screen shot for number pyramid C program

Wednesday, July 18, 2012

Print Even position character

Q. Write a C program to accept a string from user and print only its even number position character.


For example:
Example of print only even number stored character C program
Example of print only even number stored character C program

Ans.


/*c program for display character only that are stored in even position*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int i;
 char str[30];
 printf("Enter any string: ");
 gets(str);
 for(i=0; str[i]!='\0'; i++)
 {
   if(i%2==0)
      printf("%c",str[i]);
 }
 getch();
 return 0;
}


/************Output************/


Output of display only even character from string C program
Screen shot for print only even position character in string

Sunday, July 15, 2012

Number rectangle structure

Q. Write a C program to display the following number structure:


1234554321
1234__4321
123____321
12______21
1________1


Ans.


/* c program for number rectangle or number pyramid*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int num,c,sp,r=1;
 printf("Enter loop repeat number(rows): ");
 scanf("%d",&num);
 printf("\n");
 for(; num>=1; num--,r++)
 {
  for(c=1; c<=num; c++)
     printf("%d",c);
  for(sp=r; sp>1; sp--)
     printf("_");
  for(sp=r; sp>1; sp--)
     printf("_");
  for(c=num; c>=1; c--)
     printf("%d",c);
  printf("\n");
 }
 getch();
 return 0;
}


/***************Output****************/
Output of number structure pyramid C program
Screen-shot for number pyramid structure of C program

Sunday, July 8, 2012

Search Sub String individual

Q. Write a C program to accept two string from user, first is main string and second is searching sub string from main string, and count how many times its repeat individual in main string.
OR
Q. WAP to accept two string and find out how many times individual sub-string found in main string.


For example:


First main string: This is a good program
Second search string: is
Result: 1


Ans.


/*c program for search sub string from main string and counting how many times its repeat individual*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
 int i,j,count=0;
 char str[40],fstr[10];
 printf("Enter main string: ");
 gets(str);
 printf("Enter sub string: ");
 gets(fstr);
 for(i=0; str[i]!='\0'; i++)
 {
  if(str[i-1]==' ')
   if(str[i]==fstr[0])
   {
    for(j=0; str[i]==fstr[j] && fstr[j]!='\0'; i++,j++);
       i--;
    if(fstr[j]=='\0')
       count++;
   }
 }
 printf("%d",count);
 getch();
 return 0;
}


/***************Output***************/


Output of search individual sub string from main string C program
Screen shot for search sub-string individual from main string


Related Programs:

  1. Reverse all string
  2. Reverse all words but not string
  3. Reverse only first character of string & add extra word
  4. Display string vertically
  5. Search sub string from main string
  6. Change Title case of string
  7. Position and repetition of character in string

Saturday, July 7, 2012

Search Sub String

Q. Write a C program to accept two string from user and find sub string from main string.


For example:


main string: This is a good program
sub string: is
result: 2


Ans.


/*c program to find how many times sub string found in main string*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
 int i,j,count=0;
 char str[40],fstr[10];
 printf("Enter main string: ");
 gets(str);
 printf("Enter sub string: ");
 gets(fstr);
 for(i=0; str[i]!='\0'; i++)
 {
  if(str[i]==fstr[0])
  {
   for(j=0; str[i]==fstr[j] && fstr[j]!='\0'; i++,j++);
   if(fstr[j]=='\0')
      count++;
  }
 }
 printf("%d",count);
 getch();
 return 0;
}


/***************Output***************/


Output of search sub-string from main string C program
Screen shot of find sub-string in main string C program

Related programs:

  1. Reverse all string
  2. Reverse all words but not string
  3. Reverse each first character of word & add extra word
  4. Display string vertically
  5. Change title case of string
  6. Position and repetition of character in string
  7. Search sub string individual from main string

String Title case

Q. Write a C program to accept a string from user change it to title case i.e. first character of each word in string must be capital letter.


For example if string is: are you confident to write c programs?
Result: Are You Confident To Write C Program?


Ans.


/*c program for change case of string or title case of string*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
 int tmp,i;
 char str[30];
 printf("Enter any string: ");
 gets(str);
 for(i=0; str[i]!='\0'; i++)
 {
   if(str[i-1]==' ' || i==0)
   {
     if(str[i]>='a' && str[i]<='z')
       str[i]=str[i]-32;
     else
       if(str[i]>='A' && str[i]<='Z')
     str[i]=str[i]+32;
   }
   printf("%c",str[i]);
 }
 getch();
 return 0;
}


/****************Output****************/

Output of C program of string title case
Screen shot of title case C program


Related programs:

  1. Reverse all string
  2. Reverse all words but not string
  3. Reverse each first character of word & add extra word
  4. Display string vertically
  5. Search sub-string from main string
  6. Search sub-string individual from main string
  7. Position and repetition of character in string

Number triangle

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

     1
    21
   321
  4321
 54321

Ans.

/*c program code for number pyramid*/
#include<stdio.h>
int main()
{
 int num,r,c,sp;
 printf("Enter number of rows: ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
 {
   for(sp=num-r; sp>0; sp--)
      printf(" ");
   for(c=r; c>=1; c--)
      printf("%d",c);
   printf("\n");
 }
 getch();
 return 0;
}

/****************Output**************/

Output of C program for number triangle
Screen shot for number triangle C program

Number pyramid/triangle

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

54321
 4321
  321
   21
    1

Ans.

/*c program code for number triangle*/
#include<stdio.h>
int main()
{
 int num,r,c,sp;
 printf("Enter number of rows: ");
 scanf("%d", &num);
 for(r=1; r<=5; r++,num--)
 {
   for(sp=r; sp>1; sp--)
     printf(" ");
   for(c=num; c>=1; c--)
     printf("%d",c);
   printf("\n");
 }
 getch();
 return 0;
}

/*********Output**********/
Output of C program for number triangle
Screen shot for number triangle

Friday, July 6, 2012

First character reverse and add extra word

Q. Write a C program to accept a string from user and put the first character of word at the end of string and add one extra character.
Example:
Enter String: This Is A Good Blog
Result: hisTX sIX AX oodGX logBX


Ans.


/*c program for taking first character of every word and put it at the end of word and then add one extra word.*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
 char tmp,str[30];
 int i;
 printf("Enter string : ");
 gets(str);
 printf("\n-- Result of program --\n\n");
 tmp=str[0];
 for(i=1; str[i]!='\0'; i++)
 {
  if(str[i]==' ')
  {
    printf("%cX",tmp);
    printf(" ");
    i++;
    tmp=str[i];
  }
  else
    printf("%c",str[i]);
 }
 printf("%cX",tmp);
 getch();
 return 0;
}


/***************Output*****************/
Output of arrangement character in string C program
Screen shot for reverse each first character of word and adding one extra word C program


Related program:

  1. Reverse all string
  2. Reverse all words but not string
  3. Change title case of string
  4. Display string vertically
  5. Search sub string from main string
  6. Search sub string individual from main string
  7. Position and repetition of character in string