-->

Saturday, July 14, 2018

program to get sum of all elements of two one dimensional arrays.

using turbo c++
----------------------------------------------------------------------------------------------------------
//program to get sum of all elements of two one dimensional arrays.
#include<stdio.h>
#include<conio.h>
void main()
{
int array_1[100],array_2[100];
int i;
int total_element;
printf("enter size of array\n");
scanf("'%d",&total_element);
printf("enter elements of first array\n");
for(i=0;i<total_element;i++)
{

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

printf("now enter for second array\n");
for(i=0;i<total_element;i++)
{
 
           scanf("%d",&array_2[i]);
}
 printf("the summed array is\n");
for(i=0;i<total_element;i++)
{
           printf("%d\n",array_1[i]+array_2[i]);
}
getch();
}

--------------------------------------------------------------------------------------------------------
using codeblocks or dev c++/online compiler
-------------------------------------------------------------------------------------------------
//program to get sum of all elements of two one dimensional arrays.
#include<stdio.h>
int main()
{
int array_1[100],array_2[100];
int i;
int total_element;
printf("enter size of array\n");
scanf("'%d",&total_element);
printf("enter elements of first array\n");
for(i=0;i<total_element;i++)
{

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

printf("now enter for second array\n");
for(i=0;i<total_element;i++)
{
 
           scanf("%d",&array_2[i]);
}
 printf("the summed array is\n");
for(i=0;i<total_element;i++)
{
           printf("%d\n",array_1[i]+array_2[i]);
}
return 0;
}




program to store any 'n' elements and display those which are >15.

using turbo C++
-----------------------------------------------------------------------
// program to store any 'n' elements and display those which are >15.
#include <stdio.h>
#include<conio.h>
void main()
{
    int a[100],n,i;
    printf("enter total number\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("enter number for location=%d\n",i);
        scanf("%d",&a[i]);
    }
    for(i=0;i<n;i++)
    {
        if(a[i]>15)
                {
                   printf("%d\n",a[i]);
                 }
    }
    getch();
}
-------------------------------------------------------------------------------------------------------------------
using online compiler/devC++/codeblocks with return concept
---------------------------------------------------------------------------------------------------------------------
// program to store any 'n' elements and display those which are >15.
#include <stdio.h>

int main()
{
    int a[100],n,i;
    printf("enter total number\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("enter number for location=%d\n",i);
        scanf("%d",&a[i]);
    }
    for(i=0;i<n;i++)
    {
          if(a[i]>15)
                {
                   printf("%d\n",a[i]);
                 }
    }
    return 0;
}

program to swap any two values stored in array.Let there are 'n' numbers.


using turbo c++
-----------------------------------------------------------------------------------------------------------------------

//program to swap any two values stored in array.Let there are 'n' numbers.

#include <stdio.h>
#include<conio.h>
void main()
{
    int a[100],n,i,location,location1,j;
    printf("enter total number\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("enter number for location=%d\n",i);
        scanf("%d",&a[i]);
    }
    printf("numbers before swapping, are:\n");
    for(i=0;i<n;i++)
    {

        printf("%d\n",a[i]);
                 
    }
    printf("enter number's first location to be swapped\n");
    scanf("%d",&location);
    printf("enter number's second location to be swapped\n");               
    scanf("%d",&location1);                                                 
    j=a[location-1];
    a[location-1]=a[location1-1];
    a[location1-1]=j;
    printf("numbers after swapping, are:\n");
    for(i=0;i<n;i++)
    {

        printf("%d\n",a[i]);
                 
    }
    
    getch();


}

-----------------------------------------------------------------------------------------------------------------------
using codeblocks/dev c++/online compiler with return concept
---------------------------------------------------------------------------------------------------------
//program to swap any two values stored in array.Let there are 'n' numbers.

#include <stdio.h>

int main()
{
    int a[100],n,i,location,location1,j;
    printf("enter total number\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("enter number for location=%d\n",i);
        scanf("%d",&a[i]);
    }
    printf("numbers before swapping, are:\n");
    for(i=0;i<n;i++)
    {

        printf("%d\n",a[i]);
                 
    }
    printf("enter number's first location to be swapped\n");
    scanf("%d",&location);
    printf("enter number's second location to be swapped\n");               
    scanf("%d",&location1);                                                 
    j=a[location-1];
    a[location-1]=a[location1-1];
    a[location1-1]=j;
    printf("numbers after swapping, are:\n");
    for(i=0;i<n;i++)
    {

        printf("%d\n",a[i]);
                 
    }
    
    return 0;

}

program to delete an element stored in an array

using turbo c++ compiler
--------------------------------------------------------------------------------------------------------------
// program to delete an element stored in an array

#include <stdio.h>
#include<conio.h>
void main()
{
    int a[100],n,i,location,j;
    printf("enter total number\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("enter number for location=%d\n",i);
        scanf("%d",&a[i]);
    }
    printf("numbers are:\n");
    for(i=0;i<n;i++)
    {

        printf("%d\n",a[i]);
               
    }
    printf("enter number's location to be deleted\n");
    scanf("%d",&location);
    for(i=0;i<n;i++)
    {
        if(i==location-1)
        {
            for(j=i;j<=n-1;j++)
            a[j]=a[j+1];                                         
        }                                                         
         
       
    }
    printf("number after deleting particular, are:\n");
    for(i=0;i<=n-2;i++)
    {

        printf("%d\n",a[i]);
               
    }
    getch();
}

---------------------------------------------------------------------------------
using devc++/codeblocks/online compiler with return concept.
----------------------------------------------------------------------------------
// program to delete an element stored in an array

#include <stdio.h>

int main()
{
    int a[100],n,i,location,j;
    printf("enter total number\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("enter number for location=%d\n",i);
        scanf("%d",&a[i]);
    }
    printf("numbers are:\n");
    for(i=0;i<n;i++)
    {

        printf("%d\n",a[i]);
               
    }
    printf("enter number's location to be deleted\n");
    scanf("%d",&location);
    for(i=0;i<n;i++)
    {
        if(i==location-1)
        {
            for(j=i;j<=n-1;j++)
            a[j]=a[j+1];                                         
        }                                                         
           
    }
    printf("number after deleting particular, are:\n");
    for(i=0;i<=n-2;i++)
    {

        printf("%d\n",a[i]);
               
    }
    return 0;
}

program to display all numbers which are stored in odd positioned cell of array


using turbo C++
-----------------------------------------------------------------------
// program to display all numbers which are stored in odd positioned cell of array
#include <stdio.h>
#include<conio.h>
void main()
{
    int a[100],n,i;
    printf("enter total number\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("enter number for location=%d\n",i);
        scanf("%d",&a[i]);
    }
    printf("numbers are:\n");
    for(i=0;i<n;i++)
    {
        if(i%2!=0)
                {
                   printf("%d\n",a[i]);
                 }
    }
    getch();
}


-------------------------------------------------------------------------------------------------------------------
using online compiler/devC++/codeblocks with return concept
---------------------------------------------------------------------------------------------------------------------
// program to display all numbers which are stored in odd positioned cell of array

#include <stdio.h>

int main()
{
    int a[100],n,i;
    printf("enter total number\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("enter number for location=%d\n",i);
        scanf("%d",&a[i]);
    }
    printf("numbers are:\n");
    for(i=0;i<n;i++)
    {
        if(i%2!=0)
                {
                   printf("%d\n",a[i]);
                 }
    }
    return 0;
}

program to display all numbers which are stored in even positioned cell of array


using turbo C++
-----------------------------------------------------------------------
// program to display all numbers which are stored in even positioned cell of array
#include <stdio.h>
#include<conio.h>
void main()
{
    int a[100],n,i;
    printf("enter total number\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("enter number for location=%d\n",i);
        scanf("%d",&a[i]);
    }
    printf("numbers are:\n");
    for(i=0;i<n;i++)
    {
        if(i%2==0)
                {
                   printf("%d\n",a[i]);
                 }
    }
    getch();
}


-------------------------------------------------------------------------------------------------------------------
using online compiler/devC++/codeblocks with return concept
---------------------------------------------------------------------------------------------------------------------
// program to display all numbers which are stored in even positioned cell of array

#include <stdio.h>

int main()
{
    int a[100],n,i;
    printf("enter total number\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("enter number for location=%d\n",i);
        scanf("%d",&a[i]);
    }
    printf("numbers are:\n");
    for(i=0;i<n;i++)
    {
        if(i%2==0)
                {
                   printf("%d\n",a[i]);
                 }
    }
    return 0;
}

program to get sum of numbers among 'n' numbers


using turbo C++
-----------------------------------------------------------------------

// program to get sum of  numbers among 'n' numbers
#include <stdio.h>
#include<conio.h>
void main()
{
    int a[100],n,i,sum=0,number_to_be_summed;
    printf("enter total number\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("enter number for location=%d",i);
        scanf("%d",&a[i]);
    }
    printf("enter total number to be summed");
    scanf("%d",&number_to_be_summed);
    for(i=0;i<number_to_be_summed;i++)
    {
       
       sum=sum+a[i];
    }
    printf("sum=%d",sum);
    getch();
}


-------------------------------------------------------------------------------------------------------------------
using online compiler/devC++/codeblocks with return concept
---------------------------------------------------------------------------------------------------------------------
// program to get sum of  numbers among 'n' numbers

#include <stdio.h>

int main()
{
    int a[100],n,i,sum=0,number_to_be_summed;
    printf("enter total number\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("enter number for location=%d",i);
        scanf("%d",&a[i]);
    }
    printf("enter total number to be summed");
    scanf("%d",&number_to_be_summed);
    for(i=0;i<number_to_be_summed;i++)
    {
       
       sum=sum+a[i];
    }
    printf("sum=%d",sum);
    return 0;
}

Saturday, June 30, 2018

C program to print the given star pattern S

/* C program to print the given star pattern
* * * * *

* * * * *

* * * * *

* * * * *

* * * * * * * * * * * * * * * * *

                                    * * * * *

                                    * * * * *

                                    * * * * *

                                    * * * * *
 */
#include<conio.h>
#include <stdio.h>
void main()
{
    int i,j,k,m;
    for(i=1;i<=5;i++)      //for number of rows. there are five rows so loop moves fives times
    {
       
        for(j=1;j<=5;j++) // for number of columns
        {
        printf("* ");
        }
        printf("\n");      // for next rows
    }
    for(i=1;i<=15;i++)      // for middle line (star;long line)
    {
        printf("* ");
    }
    printf("\n");
    for(i=1;i<=5;i++)      //for number of rows. there are five rows so loop moves fives times
    {
        for(m=1;m<=20;m++)   // for spaces. suppose there are 20 spaces.
        {
            printf(" ");
        }
        for(j=1;j<=5;j++) // for number of columns display. it is second part.
        {
        printf("* ");
        }
        printf("\n");      // for next rows
    }
   
getch();
}

C program to print the given star pattern * * * * *

/* C program to print the given star pattern
  * * * * *

            * * * * *

                       * * * * *

                                * * * * *
                                           * * * * *
                                * * * * *

                        * * * * *

            * * * * *
    * * * * *
 */
#include<conio.h>
#include <stdio.h>

void main()
{
    int i,j,k,m;
for(i=5;i>=1;i--)      //for number of rows. there are five rows so loop moves fives times
  {
    for(k=i;k<=7;k++)
      {
      printf(" ");   // this part is for space. every time , on display, it increases space.
      }
    for(j=1;j<=5;j++) //it displays star five times.It displays in columns
      {
        printf("* ");
      }
    printf("\n");
    }
    for(i=1;i<=4;i++)      //for number of rows. there are four rows so loop moves four times
    {
       
        for(k=i;k<=6;k++)
            {
            printf(" ");   // this part is for space. every time , on display, it decreases space.
            }
        for(j=1;j<=5;j++) //it displays star five times.It displays in columns
            {
            printf("* ");
            }m--;
        printf("\n");
    }
getch();
}

C program to print the given number pattern 121

/* C program to print the given number pattern 121
1
121
12321
1234321
123454321
 */
#include <stdio.h>
#include<conio.h>
void main()
{
    int i, j, N;

    printf("Enter rows: ");
    scanf("%d", &N);

    for(i=1; i<=N; i++)
    {
        // Prints the first part of the pattern
        for(j=1; j<=i; j++)
        {
            printf("%d", j);
        }
        // Prints the second part of the pattern
        for(j=i-1; j>=1; j--)
        {
            printf("%d", j);
        }
        printf("\n");
    }
    getch();
}
                                                                                   source:codeforwin.org

C program to print alphabetical character pattern A

/**
 C program to print alphabetical character pattern A
A
AB
ABC
ABCD
ABCDE
 */
#include <stdio.h>
#include<conio.h>
void main()
{

int i, j;
for (i=1;i<=5;i++)
{
for (j=0;j<i;j++) {
printf("%c", 'A'+ j);
}
printf("\n");
}
return 0;
}

C program to print alphabetical pattern ABCDE

/**
 C program to print alphabetical  pattern ABCDE
ABCDE
ABCD
ABC
AB
A
 */
#include <stdio.h>
#include<conio.h>
void main()
{

int i, j;
for (i=5;i>=1;i--) {
for (j=0;j<i;j++) {
printf("%c", 'A'+ j);
}
printf("\n");
}
getch();
}

C program to print square numeric pattern 12345

**
 * C program to print square numeric pattern
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

 */
#include <stdio.h>
#include<conio.h>
void main()
{
    int i, j, N;

    /* Input number of rows from user */
    printf("Enter number of rows: ");
    scanf("%d", &N);

    /* Iterate over each row */
    for(i=1; i<=N; i++)
    {
                /* Print given number in next line */
                printf("1 2 3 4 5\n");
   
    }

    getch();
}

C program to print box number pattern with alternative 2 and 0

/**
 *
 2 2 2 2 2
 0 0 0 0 0
 2 2 2 2 2
 0 0 0 0 0
 2 2 2 2 2
 */
#include <stdio.h>
#include<conio.h>
void main()
{
    int rows, cols, i, j;

    /* Input rows and columns from user */
    printf("Enter number of rows: ");
    scanf("%d", &rows);
    printf("Enter number of columns: ");
    scanf("%d", &cols);

    for(i=1; i<=rows; i++)
    {
        for(j=1; j<=cols; j++)
        {
            if((i%2)!=0)
            printf("2");
            else
            printf("0");
        }

        printf("\n");
    }

    getch();
}

C program to print box number pattern with alternative 1 and 0

/**
 * C program to print box number pattern
 1 1 1 1 1
 0 0 0 0 0
 1 1 1 1 1
 0 0 0 0 0
 1 1 1 1 1
 */
#include <stdio.h>
#include<conio.h>
void main()
{
    int rows, cols, i, j;

    /* Input rows and columns from user */
    printf("Enter number of rows: ");
    scanf("%d", &rows);
    printf("Enter number of columns: ");
    scanf("%d", &cols);

    for(i=1; i<=rows; i++)
    {
        for(j=1; j<=cols; j++)
        {
            printf("%d", (i%2));
        }

        printf("\n");
    }

    getch();
}

                                                                                                                  source: codeforwin.org

C program to print square numeric pattern

/**
 * C program to print square numeric pattern
   0 2 0 2 0
   0 2 0 2 0
   0 2 0 2 0
   0 2 0 2 0
   0 2 0 2 0

 */
#include <stdio.h>
#include<conio.h>
void main()
{
    int i, j, N;

    /* Input number of rows from user */
    printf("Enter number of rows: ");
    scanf("%d", &N);

    /* Iterate over each row */
    for(i=1; i<=N; i++)
    {
                /* Print given number in next line */
                printf("0 2 0 2 0\n");
   
    }

    getch();
}

C program to print square star pattern with zeros

/**
 * C program to print hollow square star pattern
   x x x x x
   x 0 0 0 x
   x 0 0 0 x
   x 0 0 0 x
   x x x x x


 */

#include <stdio.h>
#include<conio.h>
void main()
{
    int i, j, N;

    /* Input number of rows from user */
    printf("Enter number of rows: ");
    scanf("%d", &N);

    /* Iterate over each row */
    for(i=1; i<=N; i++)
    {
        /* Iterate over each column */
        for(j=1; j<=N; j++)
        {
            if(i==1 || i==N || j==1 || j==N)
            {
                /* Print star for 1st, Nth row and column */
                printf("x");
            }
            else
            {
                printf("0");
            }
        }

        /* Move to the next line/row */
        printf("\n");
    }

    getch();
}

                                                                                                    source:codeforwin.org

C program to print hollow numeric star pattern

/**
 * C program to print hollow numeric star pattern
 1 1 1 1 1
 1          1
 1          1
 1          1
 1 1 1 1 1

 */

#include <stdio.h>
#include<conio.h>
void main()
{
    int i, j, N;

    /* Input number of rows from user */
    printf("Enter number of rows: ");
    scanf("%d", &N);

    /* Iterate over each row */
    for(i=1; i<=N; i++)
    {
        /* Iterate over each column */
        for(j=1; j<=N; j++)
        {
            if(i==1 || i==N || j==1 || j==N)
            {
                /* Print star for 1st, Nth row and column */
                printf("1");
            }
            else
            {
                printf(" ");
            }
        }

        /* Move to the next line/row */
        printf("\n");
    }

    getch();
}

C program to print box number pattern with alternative 0 and 1


/**
 * C program to print box number pattern 
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1
 */

#include <stdio.h>
#include<conio.h>
void main()
{
    int rows, cols, i, j, k;

    /* Input rows and columns from user */
    printf("Enter number of rows: ");
    scanf("%d", &rows);
    printf("Enter number of columns: ");
    scanf("%d", &cols);

    k = 1;

    for(i=1; i<=rows; i++)
    {
        for(j=1; j<=cols; j++)
        {
            if(k == 1)
            {
                printf("1");
            }
            else
            {
                printf("0");
            }

            // If k = 1  then k *= -1 => -1
            // If k = -1 then k *= -1 =>  1
            k *= -1;
        }

        if(cols % 2 == 0)
        {
            k *= -1;
        }

        printf("\n");
    }

    getch();
}
                                                           source :codeforwin.org

C program to print box number pattern with zero in center


/**
 * C program to print box number pattern with zero in center
11011
11011
00000
11011
11011 

*/

#include <stdio.h>
#include<conio.h>
void main()
{
    int rows, cols, i, j;
    int centerRow, centerCol;

    /* Input rows and columns from user */
    printf("Enter number of rows: ");
    scanf("%d", &rows);
    printf("Enter number of columns: ");
    scanf("%d", &cols);

    centerRow = (rows+1) / 2;
    centerCol = (cols+1) / 2;

    for(i=1; i<=rows; i++)
    {
        for(j=1; j<=cols; j++)
        {
            // Print 0 for central rows or columns
            if(centerCol == j || centerRow == i)
            {
                printf("0");
            }
            else if((cols%2 == 0 && centerCol+1 == j) || (rows%2 == 0 && centerRow+1 == i))
            {
                // Print an extra 0 for even rows or columns
                printf("0");
            }
            else
            {
                printf("1");
            }
        }

        printf("\n");
    }

    getch();
}