Total Pageviews

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;

}

14. Write a program which takes the radius of a circle as input from the user, passes it to another function that computes the area and the circumference of the circle and displays the value of area and circumference from the main() function.

 #include<stdio.h>

const float PI = 3.1415927;

float area(float radius);

float circum(float radius);

void main() {

 float radius;


 printf("Enter radius: ");

 scanf("%f", &radius);

 printf("Area : %.3f\n", area(radius));

 printf("Circumference: %.3f\n", circum(radius));


}


float area(float radius) {

  return PI * radius * radius;

}


float circum(float radius) {

  return 2 * PI * radius;

}


12. Write a program that swaps two numbers using pointers.

 #include <stdio.h>


void swap(int *a, int *b) {

    int temp;

    temp = *a;

    *a = *b;

    *b = temp;

}


int main() {

    int num1, num2;

    int *ptr1, *ptr2;

    printf("Enter two numbers:\n");

    scanf("%d%d", &num1, &num2);

    ptr1 = &num1;

    ptr2 = &num2;

    printf("before swapping:\n");

    printf("Number 1 = %d\n", num1);

    printf("Number 2 = %d\n", num2);

    swap(ptr1, ptr2);

    printf("After swapping:\n");

    printf("Number 1 = %d\n", num1);

    printf("Number 2 = %d\n", num2);


    return 0;

}

8. Write a macro that swaps two numbers. WAP to use it.

 #include <stdio.h>


#define SWAP(x, y) (x ^= y ^= x ^= y)


int main()

{

    int num1, num2;


    

    printf("Enter any two number to swap: ");

    scanf("%d%d", &num1, &num2);


    printf("Values before swapping\n");

    printf("num1 = %d, num2 = %d\n\n", num1, num2);


    SWAP(num1, num2);


    printf("Values after swapping\n");

    printf("num1 = %d, num2 = %d\n", num1, num2);


    return 0;

}

7.WAP to compute the factors of a given number.

 #include <stdio.h> 


int main() 

int num, i;

    printf("Enter a positive integer: ");

    scanf("%d", &num);

    printf("Factors of %d are: ", num);

    for (i = 1; i <= num; ++i) {

        if (num % i == 0) {

            printf("%d ", i);

        }

    }

return 0;

}


6. Write a function to find whether a given no. is prime or not. Use the same to generate the prime numbers less than 100.

 #include <stdio.h> 

int checkPrime(int N) 

int flag = 1; 

for (int i = 2; i <= N / 2; i++) { 

if (N % i == 0) { 

flag = 0; 

break; 

return flag ; 



int main() 

for(int i=1;i<=100;i++)

if(checkPrime(i)&&i!=1)

    printf("%d ",i);


return 0;

}


Friday, February 23, 2024

5. Write a function that checks whether a given string is Palindrome or not. Use this function to find whether the string entered by user is Palindrome or not.

 #include <stdio.h>

#include <string.h>



void Lower_case(char str[]) {

    int i = 0;

    while (str[i] != '\0') {

        if (str[i] > 64 && str[i] < 91) 

            str[i] += 32; 


        i++; 

    } 


void CheckPalindrome(char str[]) 


    

    int left = 0; 

    int right = strlen(str) - 1; 


    while (right > left) 

    {

        if (str[left++] != str[right--]) {

            printf("The String %s is not a palindrome", str);

            return;

        }

    }

    printf("The String %s is palindrome", str);

}

int main() 

{

    char str1[50];

    printf("enter a string");

    gets(str1);

    Lower_case(str1);

    CheckPalindrome(str1);

    

    return 0;

}

4. WAP to compute the sum of the first n terms of the following series S =1-2+3-4+5…………….

 #include <stdio.h>


int main()

{

    int n;

    int sum=0,i;

    printf("Enter the range of number:");

    scanf("%d",&n);


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

    {

        if(i%2==0)

            sum-=i;

        else

            sum+=i;

    }


    printf("The sum of the series = %d",sum);

    return 0;

}


3. WAP to compute the sum of the first n terms of the following series S = 1+1/2+1/3+1/4+……

 #include <stdio.h>


int main()

{

    double number, sum = 0, i;

 

    printf("\n enter the number ");

    scanf("%lf", &number);

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

    {

        sum = sum + (1 / i);

        if (i == 1)

            printf("\n 1 +");

        else if (i == number)

            printf(" (1 / %lf)", i);

        else

            printf(" (1 / %lf) + ", i);

    }

    printf("\n The sum of the given series is %.2lf", sum);


    return 0;

}


2. WAP to reverse a number.

 #include <stdio.h>


int main()

{

    int n, reverse = 0, remainder;


  printf("Enter an integer: ");

  scanf("%d", &n);


  while (n != 0) {

    remainder = n % 10;

    reverse = reverse * 10 + remainder;

    n /= 10;

  }


  printf("Reversed number = %d", reverse);


    return 0;

}


1. WAP to print the sum and product of digits of an integer.

 #include <stdio.h>


int main()

{

    int n;

    int dig, sum,pro;

     

    printf("\nEnter an integer number :");

    scanf("%d",&n);

  

    /*Calculating Sum and Product*/

    sum=0;

    pro=1;

     

    while(n>0)

    {

        dig=n%10; /*get digit*/

        sum+=dig;

        pro*=dig;

        n=n/10;

    }

     

    printf("\nSUM of all Digits is : %d",sum);

    printf("\nPRODUCT of all digits: %d",pro);

    return 0;

}