Tuesday, November 26, 2019

Write a program in C to print all unique elements in an array

#include<stdio.h>
int main()
{
    int arr[20], freq[20];
    int size, i, j, count;

    /* Input size of array and elements in array */
    printf("Enter size of array: ");
    scanf("%d", &size);
    printf("Enter elements in array: ");
    for(i=0; i<size;i++)   

  {
        scanf("%d", &arr[i]);
        freq[i] = -1;
    }

    /* Find frequency of each element */
    for(i=0; i<size;i++)   

    {
        count = 1;
        for(j=i+1; j<size;j++)        

      {
            if(arr[i] == arr[j])
            {
                count++;
                freq[j] = 0;
            }
        }

        if(freq[i] != 0)
        {
            freq[i] = count;
        }
    }

    /* Print all unique elements of array */
    printf("\nUnique elements in the array are: ");
    for(i=0; i<size;i++)   

   {
        if(freq[i] == 1)
        {
            printf("%d ", arr[i]);
        }
    }

    return 0;
}

--------------------------------------------------------------------------------------------------------
OUTPUT:
Enter size of array: 6
Enter elements in array: 2
4
5
2
3
4

Unique elements in the array are: 5 3 

------------------------------------------------------------------------------------------------------
Enter size of array: 9
Enter elements in array: 1
2
3
4
5
6
1
2
3

Unique elements in the array are: 4 5 6

Write a C program to check whether a given number is an Armstrong number or not.

#include<stdio.h>
int main()
{
int n,r,sum=0,temp;
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
printf("armstrong number ");
else
printf("Given number is not a armstrong number");
return 0;
}

OUTPUT:
enter the number=371
armstrong number

enter the number=45
Given number is not a armstrong number

Armstrong number

Armstrong number is a number that is equal to the sum of cubes of its digits. For example 0, 1, 153, 370, 371 and 407 are the Armstrong numbers.

Let's try to understand why 153 is an Armstrong number.

153 = (1*1*1)+(5*5*5)+(3*3*3)
where:
(1*1*1)=1
(5*5*5)=125
(3*3*3)=27

1+125+27=153 it is an Armstrong number

Write a program in C to display the n terms of harmonic series and their sum. 1 + 1/2 + 1/3 + 1/4 + 1/5 ... 1/n terms.

#include <stdio.h>
int main()
{
   int i,num1;
   float num2=0;
   printf("Input the number of terms in the series: \n");
   scanf("%d",&num1);
  
   for(i=1;i<=num1;i++)
   {
       if(i<num1)      
      {
          printf("1/%d + ",i);
          num2+=1/(float)i;
       }

     if(i==num1)
     {
          printf("1/%d ",i);
          num2+=1/(float)i;
     }
   }
        printf("\nSum of Series upto %d terms : %f \n",num1,num2);

  return 0;
}


OUTPUT:
Input the number of terms in the series:
5
1/1 + 1/2 + 1/3 + 1/4 + 1/5
Sum of Series upto 5 terms : 2.283334

Input the number of terms in the series:
10
1/1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 + 1/7 + 1/8 + 1/9 + 1/10
Sum of Series upto 10 terms : 2.928968

Write a C Program to find the factorial of the given number

#include<stdio.h>
      int main()
    {
      int k, n;        
      int fact = 1;  
      printf("Enter an input number to find its factorial \n");
      scanf("%d", &n);
        
    if(n==0)
     {
       printf("Entered number must be above zero \n");
     }
      else
     {
     
      for (k = 1; k <= n; k++)
      fact = fact * k;
      printf("Factorial of the given number is %d = %d\n", n, fact);
    
     }
     
      return 0;
    }

OUTPUT:
Enter an input number to find its factorial
4
Factorial of the given number is 4 = 24

Enter an input number to find its factorial
9
Factorial of the given number is 9 = 362880

Saturday, November 23, 2019

Write a program in C to display the n terms of even natural number and their sum



#include<stdio.h>
int main()
{
   int i,n,sum=0;

   printf("Enter the n value : ");
   scanf("%d",&n);
   printf("\nThe even numbers are :");
 

  for(i=1;i<=n;i++)
  {
    if(i%2==0)
   {
     printf("%d ",i);
     sum+=i;
   }
  }


printf("\nThe Sum of Even Natural Number upto %d terms : %d \n",n,sum);
return 0;
}


OUTPUT:
Enter the n value : 5
The even numbers are :2 4
The Sum of Even Natural Number upto 5 terms : 6

Enter the n value : 10
The even numbers are :2 4 6 8 10
The Sum of Even Natural Number upto 10 terms : 30

Write a program in C which is a Menu-Driven Program to compute the area of the various geometrical shape

#include<stdio.h>
int main ()
{
      int input_choice;
      int radious;
      int length,width;
      int base,hight;
      float area=0;

      printf("Enter 1 for area of circle\n");
      printf("Enter 2 for area of rectangle\n");
      printf("Enter 3 for area of triangle\n");
      printf("Enter your choice : ");
      scanf("%d",&input_choice);
      switch(input_choice)
      {
           case 1:
                 printf("Input radious of the circle : ");
                 scanf("%d",&radious);
                 area=3.14*radious*radious;
                 break;
            case 2:
                  printf("Input length and width of the rectangle : ");
                  scanf("%d \n %d ",&length,&width);
                  area=length*width;
                  break;
            case 3:
                  printf("Input the base and hight of the triangle :");
                  scanf("%d \n %d",&base,&hight);
                  area=(0.5)*base*hight;
                  break;
        
            default:
                  printf("select the correct choice\n");   
    }
          printf("The area is : %f\n",area);
          return 0;
}


OUTPUT:

Enter 1 for area of circle
Enter 2 for area of rectangle
Enter 3 for area of triangle
Enter your choice : 1
Input radious of the circle : 4
The area is : 50.240002

Enter 1 for area of circle
Enter 2 for area of rectangle
Enter 3 for area of triangle
Enter your choice : 5
select the correct choice
The area is : 0.000000







Write a C program to calculate the factorial of a given number


#include<stdio.h>

int main()

{

  int k, n;              

  int fact = 1;         


printf("Enter an input number to find its factorial \n");

scanf("%d", &n);



   if(n==0)

  {

      printf("Entered number must be above zero \n");

  }

  else

  {


     for (k = 1; k <= n; k++)

     fact = fact * k;

  }
printf("Factorial of the given number is %d = %d\n", n, fact);



return 0;
}

OUTPUT:
Enter an input number to find its factorial
6
Factorial of the given number is 6 = 720

Enter an input number to find its factorial
0
Entered number must be above zero

Write a C program to convert a string to a long integer

#include<stdio.h>
int main()

{

  long longinteger;

  int no;
 
  // string to long integer converison
  longinteger = atol("56789");

  // string to integer converison
  no=atoi("345");


  printf("The string converted to long int is %ld\n", longinteger-2);

  printf("The string converted to int is %d\n", no+2);


  return 0;

 }

}

OUTPUT:
The string converted to long int is 56787
The string converted to int is 347