cp 3 to 9
cp 3 to 9
Submitted By
NAME OF THE STUDENT ENROLLMENT NO.
PARV AGRAWAL BTCS24O1090
3rd assignment
Write a C program for printing prime factors of a number using functions.
ANSWER –
#include <stdio.h>
void primeFactors(int n)
{
while (n % 2 == 0) {
printf("%d ", 2);
n = n / 2;
}
for (int i = 3; i * i <= n; i = i + 2) {
while (n % i == 0) {
printf("%d ", i);
n = n / i;
}
}
if (n > 2)
printf("%d ", n);
}
int main()
{
int n;
printf(" enter the number for which you want to find prime factors n : ");
scanf("%d",&n);
primeFactors(n);
return 0;
}
OUTPUT
enter the number for which you want to find prime factors n : 225
3355
ANSWER –
#include <stdio.h>
int main()
{
int a, b, i, j, flag;
printf("Enter lower bound of the interval: ");
scanf("%d", &a);
printf("Enter upper bound of the interval: ");
scanf("%d", &b);
printf("Prime numbers between %d and %d are: ", a, b);
for (i = a; i <= b; i++)
{
if (i == 1 || i == 0)
continue;
flag = 1;
for (j = 2; j <= i / 2; ++j)
{
if (i % j == 0)
{
flag = 0;
break;
}
}
if (flag == 1)
printf("%d ", i);
}
return 0;
}
OUTPUT
ANSWER
#include <stdio.h>
int decToBinary(int n)
{
int binaryNum[32];
int i = 0;
while (n > 0) {
binaryNum[i] = n % 2;
n = n / 2;
i++;
}
for (int j = i - 1; j >= 0; j--)
printf("%d", binaryNum[j]);
return binaryNum;
}
int main()
{
int n ;
decToBinary(n);
printf("enter the number which yo want to covert binary : ");
scanf("%d",&n);
printf(" %d " , decToBinary(n));
return 0;
}
OUTPUT
1000
#include <stdio.h>
#include <math.h>
int binaryToDecimal(int n) {
while (n != 0) {
remainder = n % 10;
decimal += remainder * pow(2, i);
n = n / 10;
i++;
return decimal;
int main() {
scanf("%d", &binary);
decimal = binaryToDecimal(binary);
return 0;
OUTPUT
16.
Write a C program to convert octal to binary & vice versa.
ANSWER
1) OCTAL TO BINARY
#include <stdio.h>
#include <stdio.h>
void octalToBinary(int octal) {
int binary[32];
int i = 0;
if (octal == 0) {
printf("Binary: 000\n");
return;
}
while (octal != 0) {
int octal_digit = octal % 10;
switch(octal_digit) {
case 0: printf("000"); break;
case 1: printf("001"); break;
case 2: printf("010"); break;
case 3: printf("011"); break;
case 4: printf("100"); break;
case 5: printf("101"); break;
case 6: printf("110"); break;
case 7: printf("111"); break;
}
octal = octal / 10;
}
printf("\n");
}
int main() {
int octal;
printf("Enter an octal number: ");
scanf("%d", &octal);
OUTPUT
Enter an octal number: 125467
Binary equivalent: 2101502239
#include <stdio.h>
int main()
{
long int binary_number, octal_number = 0, j = 1, remainder;
#include <stdio.h>
void findMeanMedianMode(int arr[], int n) {
int sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
}
float mean = (float)sum / n;
int temp;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
float median;
if (n % 2 == 0) {
median = (arr[n / 2 - 1] + arr[n / 2]) / 2.0;
} else {
median = arr[n / 2];
}
int mode = arr[0];
int count = 1;
int maxCount = 1;
for (int i = 1; i < n; i++) {
if (arr[i] == arr[i - 1]) {
count++;
} else {
if (count > maxCount) {
maxCount = count;
mode = arr[i - 1];
}
count = 1;
}
}
printf("Mean: %.2f\n", mean);
printf("Median: %.2f\n", median);
printf("Mode: %d\n", mode);
}
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
findMeanMedianMode(arr, n);
return 0;
}
OUTPUT
Enter 10 elements:
10
20
30
40
45
50
60
78
110
Mean: 45.20
Median: 42.50
Mode: 9
4th assignment
Write a program in C to make such a pattern like a right angle triangle
with a number, which will repeat a number in a row.
ANSWER
#include <stdio.h>
int main() {
int i, j, rows;
OUTPUT
22
333
4444
55555
666666
7777777
88888888
999999999
10101010101010101010
ANSWER
include <stdio.h>
int main() {
int rows;
return 0;
}
OUTPUT
1
23
456
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
#include <stdio.h>
int main() {
int rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
if ((i + j) % 2 == 0) {
printf("1");
} else {
printf("0");
}
}
printf("\n");
}
return 0;
}
OUTPUT
Input number of rows : 7
1
01
101
0101
10101
010101
1010101
ANSWER
#include <stdio.h>
int main() {
int n;
printf("Enter the number of rows: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
printf(" ");
}
for (int j = 1; j <= (2 * i - 1); j++) {
printf("*");
}
printf("\n");
}
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= n - i; j++) {
printf(" ");
}
for (int j = 1; j <= (2 * i - 1); j++) {
printf("*");
}
printf("\n");
}
return 0;
}
OUTPUT
ANSWER
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
printf(" ");
int number = 1;
printf("\n");
return 0;
OUTPUT
1 1
1 2 1
1 3 3 1
1 4 6 4 1
5th assignment
Write a program in C to check whether a number can be expressed as the
sum of two prime numbers.
ANSWER
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int check_primenumber(int n){
int i , prime = 1;
if (n==0 || n==1)
{
prime =0;}
else{
for ( i = 2; i < n/2; i++)
{
if (n%i==0)
{
prime=0;
break;
}
}
} return prime;
}
int main()
{
int n, flag= 0 ;
printf(" enter the number you want to express as sum of two prime numbers \
n");
scanf("%d",&n);
for ( int i = 2; i < n/2; i++)
{
if ((check_primenumber(i)==1))
{
if ((check_primenumber(n-i)==1))
{
printf("%d=%d+%d \n",n,i,n-i);
}
}
}
if (flag = 0){
printf("not a sum of two prime numbers");
}
return 0;
}
OUTPUT
enter the number you want to express as sum of two prime numbers
28
28=5+23
28=11+17
ANSWER
#include <stdio.h>
void main()
{
int i, n, n1, s1 = 0, j;
long fact
OUTPUT
ANSWER
1) BINARY TO HEXADECIMAL
#include <stdio.h>
#include <string.h>
int main()
{
int hexConstant[] = {0, 1, 10, 11, 100, 101, 110, 111, 1000,
1001, 1010, 1011, 1100, 1101, 1110, 1111};
OUTPUT
Hexadecimal number = A1
2) HEXADECIMAL TO BINARY
#include <stdio.h>
#include <string.h>
Write a program in C to find the number and sum of all integers between
100 and 200 which are divisible by 9.
ANSWER
#include <stdio.h>
int main (){
int i , a= 0;
for ( i = 100 ; i < 201; i++)
{
if (i %9 == 0 )
{
a=a+i;
}
}
printf(" The sum of all divisible integers from 100 to 200 is = %d",a);
return 0;
}
OUTPUT
#include <stdio.h>
int main(){
int n;
printf("enter a number for which you want to find second highest factor : ");
scanf("%d",&n);
for (int i = n/2; i >=1 ; i--)
{
if (n%i==0 && i<n){
printf("%d ",i);
break;
}
}
return 0;
}
OUTPUT
enter a number for which you want to find second highest factor : 145
29
6th assignment
Write a C Program to Find the Maximum and Minimum in an Array.
ANSWER
#include <stdio.h>
int main() {
int n, i, max, min;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
max = arr[0];
min = arr[0];
for (i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
if (arr[i] < min) {
min = arr[i];
}
}
printf("Maximum element: %d\n", max);
printf("Minimum element: %d\n", min);
return 0;
}
OUTPUT
element - 0 : 5
element - 1 : 10
element - 2 : 15
element - 3 : 20
element - 4 : 46
element - 5 : 13
element - 6 : 16
element - 7 : 4548
element - 8 : 16
element - 9 : 4
Minimum element is : 4
Write C Program to Find the Largest Element in an Array.
Answer
#include <stdio.h>
int main() {
int array[n];
int i,max, n;
printf("Input the number of elements to be stored in the array :");
scanf("%d", &n);
printf("Input %d elements in the array :\n", n);
for (i = 0; i < 10; i++) {
scanf("%d", &array[i]);
}
max = array[0];
for (i = 1; i < 10; i++) {
if (array[i] > max) {
max = array[i];
}
}
printf("The maximum value in the array is: %d\n", max);
return 0;
}
OUTPUT
15
16
14
17
44
49
46
13
464
155
ANSWER
#include <stdio.h>
int main() {
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]);
scanf("%d", &key);
if (arr[i] == key) {
found = 1;
break;
}
if (!found) {
return 0;
OUTPUT
Answer
#include <stdio.h>
Int found = 0;
Found = 1;
}
}
If (!found) {
Int main() {
Int target;
Scanf(“%d”, &target);
findPairs(arr, n, target);
return 0;
OUTPUT
(6, 10)
(4, 12)
#include <stdio.h>
If (n == 0 || n == 1)
Int main() {
Int num;
Scanf(“%d”, &num);
If (num < 0) {
} else {
Return 0;
Output
Enter a number to calculate its factorial: 5
7th assignment
Answer
#include <stdio.h>
#include <stdbool.h>
Void generateFibonacci(int n) {
Int a = 0, b = 1, next;
While (a <= n) {
Printf(“%d “, a);
Next = a + b;
A = b;
B = next;
Printf(“\n”);
}
Bool isFibonacci(int num) {
Int a = 0, b = 1, next;
Next = a + b;
A = b;
B = next;
Return (a == num);
Int main() {
Scanf(“%d”, &limit);
generateFibonacci(limit);
Scanf(“%d”, &num);
If (isFibonacci(num)) {
} else {
}
Return 0;
Output
Enter the limit to generate Fibonacci numbers: 100
Fibonacci numbers up to 100:
0 1 1 2 3 5 8 13 21 34 55 89
Answer
#include <stdio.h>
Int main() {
Int num;
Printf(“Enter a number: “);
Scanf(“%d”, &num);
Printf(“%d is %s a power of 2\n”, num, (num > 0 && (num & (num – 1)) ==
0) ? “” : “not”);
Return 0;
Output
Enter a number: 8
8 is a power of 2
Enter a number: 10
10 is not a power of 2
Int j = 0;
For (int I = 0; I < n – 1; i++) {
If (arr[i] != arr[I + 1]) {
Arr[j++] = arr[i];
}
}
Arr[j++] = arr[n – 1];
Return j;
}
Int main() {
Int arr[] = {1, 2, 2, 3, 3, 4, 5, 5, 6};
Int n = sizeof(arr) / sizeof(arr[0]);
N = removeDuplicates(arr, n);
Return 0;
}
Output
Int main() {
Int I, j, k;
Int size1D, size2D_rows, size2D_cols, size3D_x, size3D_y, size3D_z;
If (arr1D == NULL) {
Printf(“Memory allocation failed for 1D array\n”);
Return -1;
}
If (arr2D == NULL) {
Printf(“Memory allocation failed for 2D array\n”);
Free(arr1D); // Free previously allocated memory
Return -1;
}
If (arr3D == NULL) {
Printf(“Memory allocation failed for 3D array\n”);
Free(arr1D);
For (I = 0; I < size2D_rows; i++) {
Free(arr2D[i]);
}
Free(arr2D);
Return -1;
}
Return 0;
}
Output
Enter the size of 1D array: 5
Enter elements for 1D array:
12345
1D Array elements are:
12345
Enter the number of rows and columns for 2D array: 2 3
Enter elements for 2D array:
123
456
2D Array elements are:
123
456
Answer
#include <stdio.h>
}
int main() {
char buffer[20]; // Ensure the buffer is large enough for the string and
null terminator
getGreeting(buffer, sizeof(buffer));
printf("%s\n", buffer);
return 0;
Output
Hello, World!
8th assignment
Write a C program which produces its own source code as its output.
Answer
#include <stdio.h>
int main() {
FILE *fp;
char ch;
fp = fopen(__FILE__, "r");
if (fp == NULL) {
return 1;
do {
ch = getc(fp);
if (ch != EOF) {
putchar(ch);
fclose(fp);
return 0;
Output
#include <stdio.h>
int main() {
FILE *fp;
char ch;
fp = fopen(__FILE__, "r");
if (fp == NULL) {
return 1;
}
// Read and print each character until EOF
do {
ch = getc(fp);
if (ch != EOF) {
putchar(ch);
fclose(fp);
return 0;
#include <stdio.h>
Void multiplyMatrices(int firstMatrix[][10], int secondMatrix[][10], int
resultMatrix[][10], int row1, int col1, int row2, int col2) {
For (int I = 0; I < row1; ++i) {
For (int j = 0; j < col2; ++j) {
resultMatrix[i][j] = 0; // Initialize result matrix element to 0
for (int k = 0; k < col1; ++k) {
resultMatrix[i][j] += firstMatrix[i][k] * secondMatrix[k][j];
}
}
}
}
Int main() {
Int row1, col1, row2, col2;
Int firstMatrix[10][10], secondMatrix[10][10], resultMatrix[10][10];
If (col1 != row2) {
Printf(“Matrix multiplication is not possible.\n”);
Return 0;
}
Printf(“Resultant matrix:\n”);
displayMatrix(resultMatrix, row1, col2);
return 0;
}
Output
Enter rows and columns for the first matrix: 2 3
Enter rows and columns for the second matrix: 3 2
Enter elements of the first matrix:
123
456
Enter elements of the second matrix:
78
9 10
11 12
Resultant matrix:
58 64
139 154
Answer
#include <stdio.h>
#include <string.h>
#include <ctype.h>
Start++;
End--;
If (tolower(str[start]) != tolower(str[end])) {
Start++;
End--;
}
Return 1; // Palindrome
Int main() {
Char str[100];
If (isPalindrome(str)) {
} else {
Return 0;
Output
2nd time
Answer
#include <stdio.h>
Void decimalToBinary(int n) {
Int I = 0;
If (n == 0) {
Printf(“0”);
Return;
While (n > 0) {
Binary[i] = n % 2;
N = n / 2;
I++;
Int main() {
Int decimalNumber;
Scanf(“%d”, &decimalNumber);
decimalToBinary(decimalNumber);
return 0;
Ouput
Answer
#include <stdio.h>
Int main() {
Int year;
// Input the year
Scanf(“%d”, &year);
} else {
Return 0;
Ouput
Ex-1
Ex-2
Answer
#include <stdio.h>
If (number < 0) {
Return -1;
If (number == 0 || number == 1) {
While ((guess * guess – number) > epsilon || (number – guess * guess) >
epsilon) {
}
Return guess;
Int main() {
Double number;
Scanf(“%lf”, &number);
Return 0;
Output
Enter a number to find its square root: 25
Square root of 25.00000 is 5.00000
Ex2
Enter a number to find its square root: 2
Square root of 2.00000 is 1.41421
Ex-3
Enter a number to find its square root: -4
Error: Square root of a negative number is undefined.
Answer
#include <stdio.h>
#include <string.h>
if (interval <= 0) {
return;
printf("%c", str[i]);
if ((i + 1) % interval == 0) {
}
}
if (length % interval != 0) {
printf("\n");
int main() {
char str[100];
int interval;
scanf("%d", &interval);
splitString(str, interval);
return 0;
Output
Ex-1
Hel
loW
orl
Ex-2
Prog
Ramm
ingI
sFun
Int main() {
Int n;
Int matrix[10][10];
return 0;
}
Ouput
#include <stdio.h>
#include <string.h>
Int n = strlen(str);
Printf(“Permutations of the string are:\n”);
generatePermutations(str, 0, n – 1);
return 0;
}
Output