Showing posts with label c program. Show all posts
Showing posts with label c program. Show all posts

Thursday, 9 May 2013

Add an element to an array of integers




DESCRIPTION: This program adds an element to an array of integers at the valid position specified by the user.


PROGRAM:


#include <stdio.h>
#include<stdlib.h>
 main()
{
   int array[100], position, i, n, value;
   printf("Enter number of elements in array\n");
   scanf("%d", &n);
   printf("Enter %d elements\n", n);
   for (i= 0; i< n; i++)
      scanf("%d", &array[i]);
   printf("Enter the position where you wish to insert an element\n");
   scanf("%d", &position);
   if(position>n+1)
   {
     printf("Invalid\n");
     exit(0);
   }
   printf("Enter the value to insert\n");
   scanf("%d", &value);
   for (i= n-1;i>= position-1;i--)
      array[i+1] = array[i];
   array[position-1] = value;
   printf("Resultant array is\n");
   for (i= 0; i<= n; i++)
      printf("%d ", array[i]);
}


OUTPUT WINDOW:



Delete an element from an array of integers



DESCRIPTION: This program deletes an element from an array of integers at the valid position specified by the user.


PROGRAM:


#include<stdio.h>
#include<stdlib.h>
main()
{
  int n,arr[20],i,j,pos;
  printf("enter the no of elements\n");
  scanf("%d",&n);
  printf("enter the elements:\n");
  for(i=0;i<n;i++)
  scanf("%d",&arr[i]);
  printf("enter the position to delete the element:\n");
  scanf("%d",&pos);
  if(pos>n)
 {
  printf("invalid\n");
  exit(0);
 }
  for(j=pos-1;j<n;j++)
  arr[j]=arr[j+1];
  for(i=0;i<n-1;i++)
  printf("%d ",arr[i]);
}



OUTPUT WINDOW:




Product of 2 numbers without using Arithmetic Operators



DESCRIPTION: This program finds the product of two numbers without using any of the arithmetic operators. Here we have made use of the looping methods to find the product of two numbers.


PROGRAM:


#include<stdio.h>
#include<stdlib.h>
void main()
{
    int n1,n2,temp=0,temp1,temp2;
    printf("Enter the 2 numbers\n");
    scanf("%d%d", &n1,&n2);
    if(n1==0&&n2==0)
    {
        printf("undefined\n");
        exit(EXIT_SUCCESS);
    }
    if(n1==0||n2==0)
  {
      printf("product=0\n");
      exit(EXIT_SUCCESS);
  }
    temp1=n1;
   temp2=n2;
    while(n1--)
   {
    while(n2--)
        temp++;
    n2=temp2;
   }
printf("%d*%d=%d",temp1,n2,temp);
}




OUTPUT WINDOW:


Sum of 2 numbers without using Arithmetic Operators


DESCRIPTION: This program finds the sum of two numbers without using any of the arithmetic operators. Here we have made use of the looping methods to find the sum of two numbers.


PROGRAM:


#include<stdio.h>
#include<stdlib.h>
void main()
{
    int n1,n2,sum,temp;
    printf("enter the 2 number's\n");
    scanf("%d%d", &n1,&n2);
   sum=n2;
   temp=n1;
   while(temp>0)
   {
       sum++;
       temp--;
   }
   printf("%d+%d=%d",n1,n2,sum);
}


OUTPUT WINDOW:


Pattern of strings-2



DESCRIPTION: This program prints the string pattern as below-
                                                                 hello
                                                                 hell
                                                                 hel
                                                                 he
                                                                 h


PROGRAM:


#include<stdio.h>
main()
{
    char str[50];
    int i,j,n;
    printf("enter the string\n");
    scanf("%s",str);
    for(i=0;str[i]!='\0';i++);
    n=i;
    for(i=0;i<n;i++)
    {
        for(j=0;j<i;j++)
        printf(" ");
        for(j=i;j<n;j++)
        printf("%c",str[j]);
        printf("\n");
    }
}



OUTPUT WINDOW:





Pattern of strings-1



DESCRIPTION: This program prints the string pattern as below-
                                                                hello
                                                                  ello
                                                                    llo
                                                                     lo
                                                                      o


PROGRAM:



#include<stdio.h>
main()
{
    char str[50];
    int i,j,n;
    printf("enter the string\n");
    scanf("%s",str);
    for(i=0;str[i]!='\0';i++);
    n=i;
    for(i=0;i<n;i++)
    {
        for(j=0;j<i;j++)
        printf(" ");
        for(j=i;j<n;j++)
        printf("%c",str[j]);
        printf("\n");
    }
}



OUTPUT WINDOW:



Triangle-1



DESCRIPTION: This program prints the triangle as below-
                                                                1
                                                                01
                                                                101
                                                                0101
                                                                10101


PROGRAM:


#include<stdio.h>
main()
{
int i,j,n,a,b;
printf("Enter the number of lines\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
    if(a%2==0)
    {
        a=1;
        b=0;
    }
    else
    {
        a=0;
        b=1;
    }
    for(j=0;j<=i;j++)
    {
        if(j%2==0)
        printf("%d",a);
        else
        printf("%d",b);
    }
    printf("\n");
}
}



OUTPUT WINDOW:



Pyramid-3



DESCRIPTION: This program prints the pyramid as below-
                                                                 1
                                                               121
                                                             12321
                                                           1234321


PROGRAM:


#include<stdio.h>
main()
{
    int i,j,k,n,m;
    printf("enter no. of lines\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        for(m=0;m<n-i;m++)
        printf(" ");
        for(j=0,k=i;j<=i||k!=0;j++)
        {
            if(j<=i)
            printf("%d",j+1);
            else if(k!=0)
            {
            printf("%d",k--);
            }
        }
        printf("\n");
    }
}



OUTPUT WINDOW:



Pyramid-2


DESCRIPTION: This program prints the pyramid as below-
                                                                 1
                                                               1  1
                                                             1  2  1
                                                           1  3  3  1
                                                         1  4  6  4  1


PROGRAM:


#include<stdio.h>
 fact(int x)
{
    int f=1,i;
    for(i=x;i>0;i--)
    f=f*i;
    return f;
}
main()
{
   int i,j,n,k;
    printf("enter the value of n\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        for(j=0;j<n-i;j++)
        printf(" ");
        for(k=0;k<=i;k++)
        printf("%2d",fact(i)/(fact(k)*fact(i-k)));
        printf("\n");
    }
}



OUTPUT WINDOW:


Pyramid-1


DESCRIPTION: This program prints the pyramid as below-
                                                                 1
                                                               232
                                                             34543
                                                           4567654


PROGRAM:


#include<stdio.h>
main()
{
    int p,j,k,l,n;
    printf("enter the value of n\n");
    scanf("%d",&n);
    for(p=0;p<n;p++)
    {
       for(j=0;j<(n-p-1);j++)
       printf(" ");
       for(k=p+1;k<(2*p+2);k++)
       printf("%d",k);
       for(l=2*p;l>p;l--)
       printf("%d",l);
        printf("\n");
    }
 }


OUTPUT WINDOW:


Inverted pyramid-1


DESCRIPTION: To print the inverted pyramid pattern as below-
                                                                 1234321
                                                                   12321
                                                                     121
                                                                       1


PROGRAM:


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


OUTPUT WINDOW:


Number of ones in a binary number


DESCRIPTION: This program finds the total number of ones in a given binary number.


PROGRAM: 


#include<stdio.h>
main()
{
    int count=0,num,rem;
    printf("enter the number\n");
    scanf("%d",&num);
    while(num!=0)
    {
        rem=num%10;
        if(rem==1)
        count++;
        num=num/10;
    }
    printf("number of ones=%d", count);
}


OUTPUT WINDOW:




To find minimum and second minimum number in an array of integers


DESCRIPTION: This is the program to find the minimum number and the second minimum number in an array.


PROGRAM:


#include<stdio.h>
main()
{
    int min,s_min,i,n;
    int a[100];
    printf("enter the number of elements in the array\n");
    scanf("%d",&n);
    printf("enter the array elements\n");
    for(i=0;i<n;i++)
    scanf("%d",&a[i]);
    if(a[0]<a[1])
    {
        min=a[0];
        s_min=a[1];
    }
    else
    {
        min=a[1];
        s_min=a[0];
    }
    for(i=0;i<n;i++)
    {
        if(a[i]<min)
        {
            s_min=min;
             min=a[i];
        }
        if(a[i]>min&&a[i]<s_min)
        s_min=a[i];
    }
    printf("Minimum=%d\nSecond minimum=%d\n",min,s_min);
}


OUTPUT WINDOW:


To find maximum and second maximum number in an array of integers


DESCRIPTION: This is the program to find the maximum number and the second maximum number in an array.


PROGRAM:


#include<stdio.h>
main()
{
    int max=0,s_max=0,i,n;
    int a[100];
    printf("enter the number of elements in the array\n");
    scanf("%d",&n);
    printf("enter the array elements\n");
    for(i=0;i<n;i++)
    scanf("%d",&a[i]);
    if(a[0]>a[1])
   {
       max=a[0];
    s_max=a[1];
   }
   else
   {
       max=a[1];
       s_max=a[0];
   }
    for(i=0;i<n;i++)
    {
        if(a[i]>max)
        {
            s_max=max;
             max=a[i];
        }
        if(a[i]<max&&a[i]>s_max)
        s_max=a[i];
    }
    printf("Maximum=%d\nSecond maximum=%d\n",max,s_max);
}



OUTPUT WINDOW:




Count number of words in a text


DESCRIPTION: This program checks for the number of words present in the text using the spaces in the text to distinguish words.


PROGRAM:


#include<stdio.h>
main()
{
  char a[50],i,count=1;
  printf("Enter the string\n");
  gets(a);
  for(i=0;a[i]!='\0';i++)
  {
      if(a[i]==' '&&a[i+1]!=' ')
      count++;
  }
  printf("Number of words =%d",count);
}



OUTPUT WINDOW:



Deleting character from a text


DESCRIPTION:This program makes use of the keyword continue to delete the appropriate character present in the string or the text.
The continue statement passes control to the next iteration of the nearest enclosing dofor, or while statement in which it appears, bypassing any remaining statements in the dofor, or while statement body.


PROGRAM:

#include<stdio.h>
main()
{
  char a[50],b[50],i,j,ch;
  printf("Enter the string\n");
  gets(a);
  printf("Enter the character to be deleted\n");
  scanf("%c",&ch);
  for(i=0,j=0;a[i]!='\0';i++)
  {
      if(a[i]==ch)
      continue;
      b[j]=a[i];
      j++;
  }
  b[j]='\0';
  puts(b);
}


OUTPUT WINDOW:








Delete unnecessary spaces in a text


DESCRIPTION: This program makes use of the keyword continue to delete the unnecessary spaces present in the string.The continue statement passes control to the next iteration of the nearest enclosing dofor, or while statement in which it appears, bypassing any remaining statements in the dofor, or while statement body.


PROGRAM:


#include<stdio.h>
main()
{
  char a[50],b[50],i,j;
  printf("Enter the string\n");
  gets(a);
  for(i=0,j=0;a[i]!='\0';i++)
  {
      if(a[i-1]==' '&&a[i]==' ')
      continue;
      b[j]=a[i];
      j++;
  }
  b[j]='\0';
  puts(b);
}


OUTPUT WINDOW:


Delete spaces from a string


DESCRIPTION: This program makes use of the keyword continue to delete the spaces present in the string.
The continue statement passes control to the next iteration of the nearest enclosing dofor, or while statement in which it appears, bypassing any remaining statements in the dofor, or while statement body.


PROGRAM:

#include<stdio.h>
main()
{
  char a[50],b[50],i,j;
  printf("Enter the string\n");
  gets(a);
  for(i=0,j=0;a[i]!='\0';i++)
  {
      if(a[i]==' '&&a[i+1]!=' ')
      continue;
      b[j]=a[i];
      j++;
  }
  b[j]='\0';
  puts(b);
}


OUTPUT WINDOW:


Delete vowels from a string



DESCRIPTION: This program makes use of the keyword continue to delete the vowels present in the string.
The continue statement passes control to the next iteration of the nearest enclosing dofor, or while statement in which it appears, bypassing any remaining statements in the dofor, or while statement body.


PROGRAM:

#include<stdio.h>
main()
{
  char a[50],b[50],i,j;
  printf("enter string\n");
  gets(a);
  for(i=0,j=0;a[i]!='\0';i++)
  {
      if(a[i]=='a'||a[i]=='e'||a[i]=='i'||a[i]=='o'||a[i]=='u'||a[i]=='A'||a[i]=='E'||a[i]=='I'||a[i]=='O'||a[i]=='U')
      continue;
      b[j]=a[i];
      j++;
  }
  b[j]='\0';
  puts(b);
}


OUTPUT WINDOW: