Saturday, February 24, 2024

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;

}