Friday, June 26, 2020

To find the factors of the numbers given in an array and to sort the numbers in descending order according to the factors present in it.
Input:
Given array : 8, 2, 3, 12, 16
Output:
12, 16, 8, 2, 3


PROGRAM:

#include<stdio.h>
int main()
{
    int a[100],b[100],i,j,n,c=0;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=0;i<n;i++)
    {
        c=0;
        for(j=1;j<=a[i];j++)
        {
            if((a[i]%j)==0)
            {
                c++;
               
            }
        }
        b[i]=c;
        }
   
for(i=0;i<n;i++)
{
    int t,t1;
    for(j=i+1;j<n;j++)
    {
        if(b[i]<b[j])
        {
            t=b[i];
            b[i]=b[j];
            b[j]=t;
           
            t1=a[i];
            a[i]=a[j];
            a[j]=t1;
        }
       
    }
}
for(i=0;i<n;i++)
{
    printf(" %d",a[i]);
}
return 0;
}
OUTPUT:
5                                                                                                                     
8 2 3 12 16                                                                                                           
 12 16 8 2 3

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

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 

PROGRAM:

 
#include<stdio.h> 
int main() 
{ 
    int n,i,j,e=0,o=0,t1,t; 
    scanf("%d",&n); 
    int a[10],b[10],c[10]; 
    for(i=0;i<n;i++) 
    { 
        scanf("%d",&a[i]); 
         
        if(i%2==0) 
        { 
            b[e]=a[i]; 
            e++; 
        }  
        else 
        { 
            c[o]=a[i]; 
            o++; 
         }  
    } 
    
    for(i=0;i<o;i++) 
    { 
     for(j=0;j<o;j++) 
         { 
          if(c[i]<c[j]) 
           { 
               t=c[i]; 
               c[i]=c[j];       
               c[j]=t; 
            } 
          }     
     } 
     for(i=0;i<e;i++) 
     { 
      for(j=0;j<e;j++) 
       { 
         if(b[i]>b[j]) 
           { 
               t1=b[i]; 
               b[i]=b[j]; 
               b[j]=t1; 
           } 
        } 
      }    
    e=0;o=0; 
    for(i=0;i<n;i++) 
    { 
        if(i%2==0) 
        {     
           printf("%d ",b[e]); 
           e++; 
        } 
       else 
        { 
            printf("%d ",c[o]); 
            o++; 
        } 
    } 
} 
 
OUTPUT:

7                                                                                                                     
13 2 4 15 12 10 5                                                                                                     
13 2 12 10 5 15 4

Given a set of numbers like <10, 36, 54,89,12> we want to find sum of weights based on the following conditions
    1. 5 if a perfect square
    2. 4 if multiple of 4 and divisible by 6
    3. 3 if even number

And sort the numbers based on the weight and print it as follows
<10,its_weight>,<36,its weight><89,its weight>
Should display the numbers based on increasing order.

PROGRAM:

#include<stdio.h>
#include<math.h>
int main()
{
    int a[100],b[100],i,j,n,c[100];
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=0;i<n;i++)
    {
        b[i]=0;
       
      int sq;
      sq=sqrt(a[i]);
     
        if(sq*sq==a[i])
        {
            b[i]=b[i]+5;
        }
        if((a[i]%4==0)&&(a[i]%6==0))
        {
            b[i]=b[i]+4;
        }
        if(a[i]%2==0)
        {
            b[i]=b[i]+3;
        }
    }
    int t,t1;
    for(i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(b[i]<b[j])
        {
            t=b[i];
            b[i]=b[j];
            b[j]=t;
           
            t1=a[i];
            a[i]=a[j];
            a[j]=t1;
        }
        }
       
    }
    for(i=0;i<n;i++)
    {
        printf("%d\t",a[i]);
    }

    return 0;
}

   

OUTPUT:

5                                                                                                                       
10 36 54 89 12                                                                                                          
36      12      54      10      89 

C Program to Multiply Two Matrices Using Multi-dimensional Arrays

C Program to Multiply Two Matrices Using Multi-dimensional Arrays

PROGRAM:

#include<stdio.h>
int main()
{
    int a[10][10],b[10][10],c[10][10],i,j,k,n,m;
    scanf("%d%d",&n,&m);
   
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            scanf("%d",&b[i][j]);
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            c[i][j]=0;
            for(k=0;k<n;k++)
            {
                c[i][j]=c[i][j]+a[i][k]*b[k][j];
            }
        }
    }
   
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            printf(" %d",c[i][j]);
        } printf("\n");
    }
}

OUTPUT:

3 3                                                                                                                     
4 5 6                                                                                                                   
1 2 3                                                                                                                   
4 6 7                                                                                                                   
                                                                                                                        
3 4 5                                                                                                                   
4 5 6                                                                                                                   
7 4 3                                                                                                                   
 74 65 68                                                                                                               
 32 26 26                                                                                                               
 85 74 77          

Sum of odd positions and even positions in array


Sum of odd positions and even positions in an array

PROGRAM:


#include <stdio.h>
int main()
{
  int n, a[100],b[100],i,j,even=0,odd=0;
  scanf("%d",&n);
  for(i=0;i<n;i++)
  {
    scanf("%d",&a[i]);
  }
  for(i=0;i<n;i++)
  {
         
      if(i%2==0)
      {
          even=even+a[i];
      }   
      else
      {
          odd=odd+a[i];
      }
     
  }
  printf("Sum of even position is %d\n",even);
  printf("Sum of odd position is %d",odd);
}


OUTPUT:

5                                                                                                                     
1 3 5 7 8                                                                                                             
Sum of even position is 14                                                                                            
Sum of odd position is 10

Remove duplicate elements from array, stored into another array and sort in ascending order

Remove duplicate elements from an array, stored into another array and sort in ascending order

PROGRAM:
#include <stdio.h>
int main()
{
  int n, a[100],b[100],i,j,k=0;
  scanf("%d",&n);
  for(i=0;i<n;i++)
  {
      scanf("%d",&a[i]);
  }
  for(i=0;i<n;i++)
  {
      int c=0;
      for(j=i+1;j<n;j++)
      {
          if(a[i]==a[j])
          {
              c=1;
              break;
          }
      }
     
      
    if(c==0)
      {
          b[k]=a[i];
          k++;
      }
  }
  
  for(i=0;i<k;i++)
  {
      int t;
      for(j=i+1;j<k;j++)
      {
          if(b[i]>b[j])
          {
            t=b[i];
            b[i]=b[j];
            b[j]=t;
          }
          
      }
  }  
      
      for(i=0;i<k;i++)
      {
      printf(" %d",b[i]);
      }
  
}
OUTPUT:
5                                                                                                                     
12 34 34 56 78                                                                                                        
Sorted Array is                                                                                                       
12      34      56      78                                                                                            
              

Simple Program to remove Duplicate Element in an Array

Simple Program to remove Duplicate Element in an Array

PROGRAM:

#include <stdio.h>
int main()
{
  int n, a[100], b[100], count = 0, c, d;
  printf("Enter number of elements in array\n");
  scanf("%d", &n);
  printf("Enter %d integers\n", n);
  for (c = 0; c < n; c++)
    scanf("%d", &a[c]);
  for (c = 0; c < n; c++)
  {
     
    for (d = 0; d < count; d++)
    {
      if(a[c] == b[d])
        break;
    }
    if (d == count)
    {
      b[count] = a[c];
      count++;
    }
  }
  printf("Array obtained after removing duplicate elements:\n");
  for (c = 0; c < count; c++)
    printf("%d\n", b[c]);
  return 0;
}

OUTPUT:

5                                                                                                                     
Enter 5 integers                                                                                                      
12 34 12 34 56                                                                                                        
Array obtained after removing duplicate elements:                                                                     
12                                                                                                                    
34                                                                                                                    
56   

C Program to Sort the Array in an Ascending Order

C Program to Sort the Array in an Ascending Order


PROGRAM:


#include<stdio.h>
int main() 
{
    int a[5],i,j,n,t;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
       
    for(i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(a[i]>a[j])
            {
                t=a[i];
                a[i]=a[j];
                a[j]=t;
            }
        }
    }
    
    
    for(i=0;i<n;i++)
    {
        printf("%d\t",a[i]);
    }
    
return 0;    
}

OUTPUT:

5                                                                                                                     
12                                                                                                                    
67                                                                                                                    
34                                                                                                                    
23                                                                                                                    
87                                                                                                                    
12      23      34      67      87

C Program to Find Maximum Element in an Array

C Program to Find Largest Element in an Array

PROGRAM:

#include<stdio.h>
int main() 
{
    int a[5],max=0,i,n;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    
    for(i=0;i<n;i++)
    {
        if(max<a[i])
        max=a[i];
    }
    printf("Maximum Element is: %d\n",max);
    
return 0;    
}

OUTPUT:
5                                                                                                                     
10                                                                                                                    
45                                                                                                                    
34                                                                                                                    
12                                                                                                                    
78                                                                                                                    
Maximum Element is: 78

Program for average of an array

Program for an average of an array


PROGRAM:

#include<stdio.h>
int main() 
{
    int a[5],i,n,sum=0;
    float avg;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        sum=sum+a[i];
           
    }
    avg=sum/n;
    printf("%f",avg);
   
}
OUTPUT:
5                                                                                                                     
10                                                                                                                    
40                                                                                                                    
30                                                                                                                    
20                                                                                                                    
10                                                                                                                    
22.000000 

C Program to Print Pyramids and Patterns

C Program to Print Pyramids and Patterns

PROGRAM:
#include<stdio.h>
int main() 
{
    int i, space, rows, k=0;
   
    scanf("%d", &rows);
    for (i=1; i<=rows; ++i,k=0)
    {
       
        for (space=1; space<=rows-i; ++space)
        {
            printf("  ");
           
        }
        while (k!=2*i-1)
        {
            printf("* ");
            ++k;
        }
        printf("\n");
    }   
    return 0;
}
OUTPUT:

5                                                                                                                       
        *                                                                                                               
      * * *                                                                                                             
    * * * * *                                                                                                           
  * * * * * * *                                                                                                         
* * * * * * * * *

C Program to Display Prime Numbers Between Two Intervals

C Program to Display Prime Numbers Between Two Intervals

PROGRAM:
#include<stdio.h>
int main() 
{
    int i,j,a,b;
    printf("Enter intervals:");
    scanf("%d %d",&a,&b);
    for(i=a;i<=b;i++)
    {
        int c=0;
        for(j=2;j<i;j++)
        {
            if(i%j==0)
            {
                c=1;
                break;
            }
        }
         if(c==0)
         {
             printf(" %d",i);
         }
       
 
    }
    return 0;
}

OUTPUT:

Enter intervals:10 100                                                                                                
 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97