1 Write a program in C program to check whether a number can be expressed as sum of two prime numbers.
#include <stdio.h>
#include <stdbool.h>
bool isPrime(int num)
{
if (num <= 1)
{
return false;
}
for (int i = 2; i <= num - 1; i++)
{
if (num % i == 0)
{
return false;
}
}
return true;
}
bool checkSumOfTwoPrimes(int num)
{
for (int i = 2; i < num; i++)
{
int j = num - i;
if (isPrime(i) && isPrime(j))
{
printf("%d + %d\n", i, j);
return true;
}
}
return false;
}
int main()
{
int n;
printf("Enter the Number: ");
scanf("%d", &n);
bool result = checkSumOfTwoPrimes(n);
if (result)
{
printf("This is Sum of 2 Prime Number");
}
else
{
printf("Tihis is not");
}
printf("\n");
return 0;
}
2 Write a program in C to print all perfect numbers in a given range using the function.
#include <stdio.h>
#include <stdbool.h>
bool CheckPerfectNumber(int num)
{
int sum = 0;
for (int i = 1; i < num; i++)
{
if (num % i == 0)
{
sum = sum + i;
}
}
if (sum == num)
{
return true;
}
else
{
return false;
}
}
int main()
{
int start, end;
printf("Enter a range: ");
scanf("%d", &start);
scanf("%d", &end);
for (int i = start; i <= end; i++)
{
if (CheckPerfectNumber(i))
{
printf("%d ", i);
}
}
printf("\n");
}
3 Write a program to sum the series 1/11 + 1/2! +1/3+...........+1/n!...
4 Write a program using function to calculate x to the power of y, where y can be either negative or positive.
#include <stdio.h>
#include <stdbool.h>
int CalculatePower(int n, int x)
{
float n2 = n;
if (x < 0)
{
for (int i = 1; i < -x; i++)
{
n2 = n2 / n;
}
}
else
{
for (int i = 1; i < x; i++)
{
n2 = n2 * n;
}
}
return n2;
}
int main()
{
int n, x;
printf("Enter a range: ");
scanf("%d", &n);
scanf("%d", &x);
float result = CalculatePower(n, x);
printf("%f", result);
}
5 Write a program in C to swap two integer numbers using call by reference and call by value method.
#include <stdio.h>
#include <stdbool.h>
void swapByReference(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void swapByValue(int a, int b)
{
int temp = a;
a = b;
b = temp;
printf("After swapping by value: a = %d, b = %d\n", a, b);
}
int main()
{
int a, b;
printf("Enter a range: ");
scanf("%d", &a);
scanf("%d", &b);
swapByValue(&a, &b);
printf("Number Swapped %d , %d", a, b);
}
6 Write a program in C to calculate the sum of array elements by passing an array to a function.
7 Write a recursive function in C to generate the Fibonacci series upto 'n' terms. 8 Write a program to calculate GCD using recursive function.
9 Write a program in C to sort n numbers in ascending order using insertion sort.
10 Write a program in C to implement binary search in an array using function
bool isPrime(int num)
{
if (num <= 1)
{
return false;
}
for (int i = 2; i <= num - 1; i++)
{
if (num % i == 0)
{
return false;
}
}
return true;
}
6 Write a program in C to calculate the sum of array elements by passing an array to
a function.