Tuesday, February 11, 2020

Pattern Applications of C


C program to eliminate vowels in the list:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
               clrscr();
               char str[20];
               int len, i, j;
               printf("Enter a string : ");
               gets(str);
               len=strlen(str);
               for(i=0; i<len; i++)
               {
                               if(str[i]=='a' || str[i]=='e' || str[i]=='i' ||
                               str[i]=='o' || str[i]=='u' || str[i]=='A' ||
                               str[i]=='E' || str[i]=='I' || str[i]=='O' ||
                               str[i]=='U')
                               {
                                              for(j=i; j<len; j++)
                                              {
                                                             str[j]=str[j+1];
                                              }
                               len--;
                               }
               }
               printf("After deleting the vowels, the string will be : %s",str);
               getch();
}

C program to sort first k elements of the array in ascending order and remaining elements (len-k) in descending order.

#include <stdio.h>
int main(void)
{
            int a[10], i=0,k, j=0, n, t;

            printf ("\n Enter the no. of elements: ");
            scanf ("%d", &n);
            printf ("\n");
            printf ("\n Enter the array elements");

            for (i = 0; i <n; i++)
            {
                        scanf ("%d", &a[i]);
            }
    printf("Enter k value:");
    scanf("%d",&k);
            for (j=0 ; j<(n-1) ; j++)
            {
                        for (i=0 ; i<(n-1) ; i++)
                        {
                                    if (a[i+1] < a[i])
                                    {
                                                t = a[i];
                                                a[i] = a[i + 1];
                                                a[i + 1] = t;
                                    }
                        }
            }

            for (i=0 ; i<k ; i++)
            {
                        printf (" %d", a[i]);
            }
            for (i=n-1 ; i>=k ; i--)
            {
                        printf (" %d", a[i]);
            }

      /* indicate successful completion */
      return 0;
}

Write a C program to print the pattern: If N is an integer, print N lines, N=4, S=3
3
4 4
5 5 5
6 6 6 6

#include <stdio.h>
int main(void)
{
            int i,j,s,n;
            printf ("\n Enter the N and S value:");
            scanf ("%d %d", &n,&s);
            for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
if(j<i)
printf(“%d”,s);
else
printf(“%d”,s);
}
printf(“\n”);
s++;
} 
}
To print the following pattern: add the following code above the last bracket “}” in the previous program.
N=4, S=3
3
4 4
5 5 5
6 6 6 6
6 6 6 6
5 5 5
4 4
3
 for(i=n;i>=1;i--)
{
    s--;
for(j=1;j<=i;j++)
{
if(j<i)
printf("%d",s);
else
printf("%d",s);
}
printf("\n");
} 
}

To generate the following pattern:
N=4
1
2 * 2
3 * 3 * 3
4 * 4 * 4 * 4
4 * 4 * 4 * 4
3 * 3 * 3
2 * 2
1
#include <stdio.h>
int main(void)
{
            int i,j,n;
            printf ("\n Enter the N value:");
            scanf ("%d", &n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
if(j<i)
printf("%d*",i);
else
printf("%d",i);
}
printf("\n");
}
for(i=n;i>=1;i--)
{
for(j=1;j<=i;j++)
{
if(j<i)
printf("%d*",i);
else
printf("%d",i);
}
printf("\n");
}
}
C program to print the pattern:
N=4
1
2 * 3
4 * 5 * 6
7 * 8 * 9 * 10
7 * 8 * 9 * 10
4 * 5 * 6
2 * 3
1
#include <stdio.h>
 int main(void) {
            int n,i,j,k=1,temp;
            printf("Enter number:");
            scanf("%d",&n);
            for(i=1;i<=n;i++)
            {
                        for(j=1;j<=i;j++)
                        {
                                    if(j<i)
                                    {
                                    printf("%d*",k);
                                    k++;
                                    }
                                    else
                                    {
                                        printf("%d",k);
                                        k++;
                                    }
                        }
                        printf("\n");
            }
            k-=n;
            temp=k;
            for(i=n;i>=1;i--)
            {
                        for(j=1;j<=i;j++)
                        {
                                    if(j<i)
                                    {
                                    printf("%d*",temp);
                                    temp++;
                                    }
                                    else
                                    {
                                        printf("%d",temp);
                                        temp++;
                                    }
                        }
                        printf("\n");
                        k-=(i-1);
                        temp=k;
            }
return 0;
}
C program to print the following pattern: N=4
1 * 2 * 3 * 4
9 * 10 * 11 * 12
13 * 14 * 15 * 16
5 * 6 * 7 * 8
#include <stdio.h>
 int main(void) {
            int N,i;
            printf("Enter number:");
            scanf("%d",&N);
            for(i=0; i<N; i+=2)
            printf("%d*%d*%d*%d\n", (4*i+1), (4*i+2), (4*i+3), (4*i+4));
    if(N%2)
            i-=2;  
    for(i--; i>0; i-=2)
            printf("%d*%d*%d*%d\n", (4*i+1), (4*i+2), (4*i+3), (4*i+4));
return 0;
}

No comments:

Post a Comment