Tuesday, March 5, 2024

8. 1²+2²+3²+4²+...+10² :—

8. 1²+2²+3²+4²+...+10² :—


#include<stdio.h>

int main()

{

    int i,sum;

    sum=0;

    for(i=1;i<=10;i++)

    {

        sum=sum+i*i;

    }

    printf("%d",sum);

    return 0;

}


7. 1+2+3+4+...+10 :—

7. 1+2+3+4+...+10 :—


#include<stdio.h>

int main()

{

    int i,sum;

    sum=0;

    for(i=1;i<=10;i++)

    {

        sum=sum+i;

    }

    printf("%d",sum);

    return 0;

}


6. Multiplication table of n by user :—


#include<stdio.h>

int main()

{

    int i,j,n;

    printf("enter value of n ");

    scanf("%d",&n);

    for(i=1,j=n;i<=10;i++)

    {

        printf("%d×%d=%d\n",i,j,i*j);

    }

    return 0;

}


5.Input a number from user and then print the factorial of the number :—

5.Input a number from user and then print the factorial of the number :—


#include<stdio.h>

int main()

{

    int i,j,n;

    printf("enter a number ");

    scanf("%d",&n);

    j=1;

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

    {

        j=j*i;

    }    

    printf("factorial of %d=%d\n",n,j);

    return 0;

}


4. Grade calculation :—


#include<stdio.h>

int main()

{

    int n;

    printf("enter value of 'n' ");

    scanf("%d",&n);

    if(n>=80)

    {

        printf("Greade A");

    }    

    else

    {

        if(n>=60)

        {

            printf("Grade B");

        }

        else

        {

            if(n>=40)

            {

                printf("Grade C");

            }

            else

            {

                printf("F");

            }

        }

    }

    return 0;

}


3. Input basic salary. Then find DA=50% of basic salary, HRA=30% of basic salary, PF=20% of basic salary, Medical=5% of basic salary, Gross Salary=Basic salary+DA+HRA-PF-Medical. If gross salary>40000, Bonus=5000, otherwise Bonus=6000.

3. Input basic salary. Then find DA=50% of basic salary, HRA=30% of basic salary, PF=20% of basic salary, Medical=5% of basic salary, Gross Salary=Basic salary+DA+HRA-PF-Medical. If gross salary>40000, Bonus=5000, otherwise Bonus=6000.


#include<stdio.h>

int main()

{

    int bs,da,hra,pf,md,b,gs;

    printf("enter basic salary ");

    scanf("%d",&bs);

    da=bs*50/100;

    hra=bs*30/100;

    pf=bs*20/100;

    md=bs*5/100;

    gs=bs+da+hra-pf-md;

    printf("gross salary=%d\n",gs);

    if(gs>40000)

    {

        gs=gs+5000;

    }

    else

    {

        gs=gs+6000;

    }

    printf("gross salary after bonus=%d",gs);

    return 0;

}


2. Input a year and check it is leap year or not :—

 


2. Input a year and check it is leap year or not :—


#include<stdio.h>

int main()

{

    int y;

    printf("enter a year ");

    scanf("%d",&y);

    if(y%4==0)

    {

        if(y%100==0)

        {

            if(y%400==0)

            {

                printf("%d is leap year",y);

            }

            else

            {

                printf("%d is not leap year",y);

            }

        }

        else

        {

            printf("%d is leap year",y);

        }

    }

    else

    {

        printf("%d is not leap year",y);

    }

    return 0;

}


1. Largest of 3 numbers using nested :—

 1. Largest of 3 numbers using nested :—


#include<stdio.h>

int main()

{

    int a,b,c;

    printf("enter 3 values ");

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

    if(a>=b)

    {

        if(a>=c)

        {

            printf("%d is the largest",a);

        }

        else

        {

            printf("%d is the largest",c);

        }

    }

    else

    {

        if(b>=c)

        {

            printf("%d is the largest",b);

        }

        else

        {

            printf("%d is the largest",c);

        }    

    }

    return 0;

}


Saturday, February 24, 2024

17. Given two ordered arrays of integers, write a program to merge the two-arrays to get an ordered array.

 #include <stdio.h>

int main()

{

    int n1,n2,n3;            

    int a[20], b[20], c[40];

    printf("Enter the size of first array: ");

    scanf("%d",&n1);

    printf("Enter the array elements: ");

    for(int i = 0; i < n1; i++)      

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

    printf("Enter the size of second array: ");

    scanf("%d",&n2);

    printf("Enter the array elements: ");

    for(int i = 0; i < n2; i++)      

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

    n3 = n1 + n2;

    int i = 0, j = 0, k = 0;


    while (i < n1 && j < n2)    

    {

        if (a[i] < b[j])

            c[k++] = a[i++];    

        else

            c[k++] = b[j++];

    }

  

    while (i < n1)    

        c[k++] = a[i++];

  

    while (j < n2)    

        c[k++] = b[j++];

    

    printf("Final array after merging: ");

    for(int i = 0; i < n3 ; i++)       

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

    return 0;   

}

15. Write a program to find sum of n elements entered by the user. To write this program, allocate memory dynamically using malloc() / calloc() functions or new operator.

 #include <stdio.h>

#include <stdlib.h>


int main()

{

    int* ptr; 

    int limit; 

    int i; 

    int sum; 


    printf("Enter limit of the array: ");

    scanf("%d", &limit);

    ptr = (int*)malloc(limit * sizeof(int));


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

        printf("Enter element %02d: ", i + 1);

        scanf("%d", (ptr + i));

    }

    

    printf("\nEntered array elements are:\n");

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

        printf("%d\n", *(ptr + i));

    }

    sum = 0; //assign 0 to replace garbage value

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

        sum += *(ptr + i);

    }

    printf("Sum of array elements is: %d\n", sum);

        free(ptr); 

    return 0;

}