20_10

Write a C program for calculator program with Basic operations (+, -, *, /,%) of two numbers using switch statement.

Source code:

 /*Write a C program for calculator program with Basic operations (+, -, *, /,%) of two numbers using switch statement. */

#include <stdio.h>

int main()

{

int num1,num2;

float result =0;

char ch;    /*to store operator choice */

printf("Enter the 1st number: ");

scanf("%d",&num1); 

printf("Enter the 2nd number: ");

scanf("%d",&num2); 

printf("Enter the operation(+,-,*,/) : ");

scanf(" %c",&ch); /* Operation to perform +,-,*,/  */

switch(ch)    

    {

        case '+':

            result=num1+num2;

            break;

             

        case '-':

            result=num1-num2;

            break;

         

        case '*':

            result=num1*num2;

            break;

             

        case '/':

            result=(float)num1/(float)num2;

            break;

             

        case '%':

            result=num1%num2;

            break;

        default:

            printf("Invalid operation.\n");

    }

printf("Result: %d %c %d = %f\n",num1,ch,num2,result);

return 0;

}

Output:
C program for calculator program with Basic operations of two numbers using switch statement


Write a C code to find the number of times a particular element (num) exist in an array.

Source code:

 //Write a C code to find the number of times a particular element (num) exist in an array.

#include<stdio.h>

int main() 

{

int j, array[5], num;

for(j=0;j<5;++j)

{

printf("Enter the value for %d: ",j);

scanf("%d",&array[j]);

}

printf("Enter the number to find : ");

scanf("%d", &num); /* the number to find */

int count=0; /*to store number of occurrence of the given num */

int i;

for(i = 0; i < 5; i++) 

{

if(array[i]==num) 

count= count+1;

printf("The number %d occurs %d times", num, count);

    return 0;

}

Output:
C code to find the number of times a particular element exist in an array


Write a C program to divide an array into two arrays of even and odd elements.

Source code:

 //Write a C program to divide an array into two arrays of even and odd elements.

#include <stdio.h>

int main() 

{

int j,i, array[5];

for(j=0;j<5;++j)

{

printf("Enter the value for %d: ",j);

scanf("%d",&array[j]);

}

int even[5], odd[5];

int e = 0 , d = 0; /* e and d is for counting number of even or odd) */  

for(i = 0; i< 5; i++) {

    if(array[i]%2 == 0) {

        even[e] = array[i];

        e++;

    }

    else {

         odd[d] = array[i];

         d++;

    }

}

printf("Even array: ");

for(i= 0; i< e; i++)

    printf("%d  ", even[i]);

    

printf("\n");

printf("Odd array: ");

for(i= 0; i< d; i++)

    printf("%d  ", odd[i]);   

return 0;

}

Output:

C program to divide an array into two arrays of even and odd elements


Write a C code to print the largest and the second largest element of an array.

Source code:

 //Write a C code to print the largest and the second largest element of an array.

#include <stdio.h>

int main() 

{

int j, array[5], largest, second;

for(j=0;j<5;++j)

{

printf("Enter the value for %d: ",j);

scanf("%d",&array[j]); 

}

int i;


   if(array[0] > array[1]) 

   {

      largest = array[0];

      second  = array[1];

   }

   else 

   {

      largest = array[1];

      second  = array[0];

   }


   for(i = 2; i < 5; ++i) 

   {

       if( largest < array[i] ) 

       {

          second = largest;

          largest = array[i];

       }

   else if( second < array[i] ) 

       {

          second =  array[i];

       }

    }

printf("Largest - %d, Second - %d", largest, second);   

return 0;

}

Output:

C code to print the largest and the second largest element of an array


Write a C program to find the largest element of a given array.

Source code:

 //Write a C program to find the largest element of a given array

#include <stdio.h>

int main() 

{

int j, array[5];

for(j=0;j<5;++j)

{

printf("Enter the value for %d: ",j);

scanf("%d",&array[j]);

}

int i, largest;

    largest = array[0];

   

    for(i = 0; i < 5; i++) 

{

    if( largest < array[i] ) 

        largest = array[i];

   } 

   printf("%d", largest); 

   return 0;

}

Output:
C program to find the largest element of a given array


Write a C program to check whether a triangle can be formed by the given value for the angles.

Source code:

 //Write a C program to check whether a triangle can be formed by the given value for the angles

#include <stdio.h>

int main()

{

    int anga, angb, angc, sum, flag;

    printf("Enter three angles:");

    scanf("%d %d %d", &anga, &angb, &angc); 

/* Calculate the sum of all angles of triangle */

    sum = anga + angb + angc;


    /* Check whether sum=180 then its a valid triangle otherwise not */

    if(sum == 180)

    flag = 1;

    else

        flag = 0;

if (flag == 1)

    {

        printf("The triangle is valid.");

    }

    else

    {

        printf("The triangle is not valid.");

    }

    return 0;

}


Or, Source code:


#include <stdio.h>

int main()

{

    int anga, angb, angc, sum;

    printf("Enter three angles:");

    scanf("%d %d %d", &anga, &angb, &angc); 

    

    sum = anga + angb + angc;


    if(sum == 180)

    printf("The triangle is valid.");

    else

        printf("The triangle is not valid.");

    return 0;

}

Output:

C program to check whether a triangle can be formed by the given value for the angles


Write a program in C to convert a decimal number to binary using recursion.

 Source code: //Write a program in C to convert a decimal number to binary using recursion. #include<stdio.h> long convertB_to_D(int d...