Skip to main content

Command Palette

Search for a command to run...

Programming and Problem solving

Published
•4 min read•View as Markdown
Programming and Problem solving

PRACTICAL-1

Objective: Write a C program to evaluate an expression ax2 + bx + c.

CODE:-

#include <stdio.h>

int main(){

    int a,b,c,x,eval;

    printf("Enter the value of a, b, c, x:-");

    scanf("%d%d%d%d",&a,&b,&c,&x);

    eval=a*x*x+b*x+c;

    printf("Result is:- %d \n",eval);

    return 0;

}

Output:-

Enter the value of a, b, c, x: - 10 5 7 2

Result is:- 57.


PRACTICAL-2

Objective: Write a C program to find the sum of first n even natural numbers

CODE:-

#include <stdio.h>

int main(){

    int n,sum;

    printf("Enter the value of n:- ");

    scanf("%d",&n);

    sum=n*(n+1);

    printf("Sum of first %d even natural number is:- %d \n",n,sum );

    return 0;

}

Output:-

Enter the value of n:- 20

Sum of first 20 even natural number is:- 420


PRACTICAL-3

Objective: Write a C program to find weather the given year is leap year or not.

CODE:-

#include <stdio.h>

int main(){

    int year;

    printf("Enter the year:- ");

    scanf("%d",&year);

    if(year%4==0 || year%400==0){

        printf("%d is the leap year\n",year);

    }

    else if(year%100==0){

         printf("%d is not the leap year\n",year);

    }

    else{

         printf("%d is not the leap year\n",year);

    }

    return 0;

}

Output:-

Enter the year:- 2026

2026 is not the leap year


PRACTICAL-4

Objective: Write a 'C' program to accept input and print whether the given input is a character, digit, or special symbol using a switch-case.

CODE:-

#include <stdio.h>

int main()

{

    char num;

    printf("Enter Any Character :-");

    scanf("%c", &num);

   switch(num>='a'&& num<='z'||num>='A'&& num<='Z')

          {

                    case 1:

                              printf("It's alphabet");

                              break;

                              case 0:

                                        switch(num>='0' && num<='9')

                                        {

                                                  case 1:

                                                            printf("It's digit");

                                                            break;

                                                            case 0:

                                                                      printf("It's special symbol");

                                                                      break;

                                        }

                              break;       

          }

    return 0;

}

Output:-

Enter Any Character :- $

It's special symbol


PRACTICAL-5

Objective: Write a C program t accept a number and print the name of the month using switch case

CODE:-

#include <stdio.h>

int main(){

     int n;

    printf("Enter the number of year:- ");

    scanf("%d",&n);

    switch (n)

    {

    case 1: printf("January\n");

            break;

    case 2: printf("February\n");

            break;          

    case 3: printf("March\n");

            break;  

    case 4: printf("April\n");

            break;

     case 5: printf("May\n");

            break;

     case 6: printf("June\n");

            break;

    case 7: printf("July\n");

            break;

    case 8: printf("August\n");

            break;

    case 9: printf("September\n");

            break;

    case 10: printf("October\n");

            break;

    case 11: printf("November\n");

            break;                                                                

    default: printf("December\n");

        break;

    }

    return 0;

}

Output :-

Enter the number of year:- 9

September


PRACTICAL-6

Objective: Write a C program to find maximum and minimum number from an array of integer.

CODE:-

#include<stdio.h>

int main(){

    int arr[6];

    printf("Enter any 6 numbers:- ");

    for(int i=0;i<6;i++){

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

    }

    int max=arr[0];

    int min=arr[0];

    for(int i=0;i<6;i++){

        if(max<arr[i]){

            max=arr[i];

        }

        if(min>arr[i]){

            min=arr[i];

        }

    }

    printf("The largest element of the array is:-%d\n",max);

    printf("The smallest element of the array is:-%d\n",min);

    return 0;

}

Output :-

Enter any 6 numbers:- 22 76 13 9 20 2

The largest element of the array is:- 76

The smallest element of the array is:2


PRACTICAL-7

Objective: Write a C program to find the transpose of a matrix

CODE:-

#include<stdio.h>

int main(){

    int arr[3][3];

    printf("Enter the elements in 3X3 matrix:- ");

    for(int i=0;i<3;i++){

        for(int j=0;j<3;j++){

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

        }

    }

    printf("Transpose of matrix is:- \n");

    for(int i=0;i<3;i++){

        for(int j=0;j<3;j++){

        printf("%d ",arr[j][i]);

        }printf("\n");

    }

    return 0;

}

Output :-

Enter the elements in 3X3 matrix:- 3 6 8

2 5 9

7 8 9

Transpose of matrix is:- 3 2 7

6 5 8

8 9 9


PRACTICAL-8

Objective: Write a C program to find the sum of diagonals of a matri

CODE:-

#include<stdio.h>

int main(){

    int n,sum=0;



    printf("Enter the size of square matrix:- ");

    scanf("%d",&n);

    printf("Enter the values of matrix:- ");

    int arr[n][n];

    for(int i=0;i<n;i++){

        for(int j=0;j<n;j++){

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

        }

    }

    printf("Matrix is:- ");

    for(int i=0;i<n;i++){

        for(int j=0;j<n;j++){

            printf("%d ",arr[i][j]);

        }

        printf("\n");

    }

    for(int i=0;i<n;i++){

        for(int j=0;j<n;j++){

        if(i==j){

            sum=sum+arr[i][j];

        }

     }

    }

    printf("Sum of Diagonal element is = %d",sum);

    return 0;

}

Output:-

Enter the size of square matrix:- 3

Enter the values of matrix:-

4 6 2

3 5 8

4 9 7

Matrix is:-

4 6 2

3 5 8

4 9 7

Sum of Diagonal element is = 16