1.
WAP to remove vowels from a Strings
#include <stdio.h>
#include <string.h>
int main() {
char str[100], t[100];
int i, j = 0;
printf("Enter a string: ");
scanf("%[^\n]s", str);
for (i = 0; str[i] != '\0'; i++) {
if (str[i] != 'a' && str[i] != 'e' && str[i] != 'i' && str[i] != 'o' && str[i] != 'u' && str[i]
!= 'A' && str[i] != 'E' && str[i] != 'I' && str[i] != 'O' && str[i] != 'U') {
t[j++] = str[i];
}
}
t[j] = '\0';
printf("String without vowels: %s\n", t);
return 0;
}
2. WAP to Sum of all elements 2d array
#include <stdio.h>
int main() {
int rows, cols;
// Input the dimensions of the 2D array
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
int array[rows][cols]; // Declare the 2D array
// Input the elements of the 2D array
printf("Enter the elements of the array:\n");
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
printf("Element [%d][%d]: ", i, j);
scanf("%d", &array[i][j]);
}
}
int sum = 0;
// Calculate the sum of all elements in the 2D array
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++)
{
sum += array[i][j];
}
}
// Output the sum of all elements
printf("Sum of all elements in the 2D array: %d\n", sum);
return 0;
}
3. WAP Swap nibbles
#include <stdio.h>
// Function to swap nibbles in a byte
unsigned char swapNibbles(unsigned char x) {
return ((x & 0x0F) << 4) | ((x & 0xF0) >> 4);
}
int main() {
unsigned char byte;
// Input the byte from the user
printf("Enter a byte (0-255): ");
scanf("%hhu", &byte);
// Swap the nibbles
unsigned char swappedByte = swapNibbles(byte);
// Output the result
printf("Original byte: 0x%02X\n", byte);
printf("Swapped nibbles: 0x%02X\n", swappedByte);
return 0;
}
4. WAP Size of data type in Macro
// C program to create own sizeof using macro
#include <stdio.h>
#define MySizeOf(var) (char*)(&var + 1) - (char*)(&var)
int main()
{
short num1 = 0;
int num2 = 0;
long num3 = 0;
char ch = 0;
printf("Size of num1: %ld\n", MySizeOf(num1));
printf("Size of num2: %ld\n", MySizeOf(num2));
printf("Size of num3: %ld\n", MySizeOf(num3));
printf("Size of ch : %ld\n", MySizeOf(ch));
return 0;
} Print add elements in an array
#include <stdio.h>
int main() {
int n;
// Input the number of elements in the array
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int array[n]; // Declare the array with the specified size
// Input the elements of the array
printf("Enter the elements of the array:\n");
for(int i = 0; i < n; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &array[i]);
}
int sum = 0;
// Calculate the sum of all elements in the array
for(int i = 0; i < n; i++) {
sum += array[i];
}
// Output the sum of all elements
printf("Sum of all elements in the array: %d\n", sum);
return 0;
}
5. Using Structure Sort the cars based on the year
#include <stdio.h>
#include <string.h>
// Define the car structure
struct Car {
char make[50];
char model[50];
int year;
};
// Function to swap two Car structures
void swap(struct Car *a, struct Car *b) {
struct Car temp = *a;
*a = *b;
*b = temp;
}
// Function to sort cars by year using bubble sort
void sortCarsByYear(struct Car cars[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (cars[j].year > cars[j + 1].year) {
swap(&cars[j], &cars[j + 1]);
}
}
}
}
int main() {
int n;
// Input the number of cars
printf("Enter the number of cars: ");
scanf("%d", &n);
// Declare an array of Car structures
struct Car cars[n];
// Input the details of each car
for (int i = 0; i < n; i++) {
printf("Enter details of car %d:\n", i + 1);
printf("Make: ");
scanf("%s", cars[i].make);
printf("Model: ");
scanf("%s", cars[i].model);
printf("Year: ");
scanf("%d", &cars[i].year);
}
// Sort the cars by year
sortCarsByYear(cars, n);
// Output the sorted list of cars
printf("\nCars sorted by year:\n");
for (int i = 0; i < n; i++) {
printf("Car %d: Make: %s, Model: %s, Year: %d\n", i + 1, cars[i].make,
cars[i].model, cars[i].year);
}
return 0;
}
6. Reverse string using recursion
#include<stdio.h>
void rev_char(char *str)
{
if(*(str+1))
{
rev_char(str+1);
}
printf("%c",*str);
}
int main()
{
char str[100];
printf("Enter the string to reverse :");
scanf("%[^\n]",str);
rev_char(str);
printf("\n");
return 0;
}
7. check whether it form palindrome or not
#include <stdio.h>
int main() {
int n, reversed = 0, remainder, original;
printf("Enter an integer: ");
scanf("%d", &n);
original = n;
// reversed integer is stored in reversed variable
while (n != 0) {
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}
// palindrome if orignal and reversed are equal
if (original == reversed)
printf("%d is a palindrome.", original);
else
printf("%d is not a palindrome.", original);
return 0;
}
8. WAP to print leap year or not
#include <stdio.h>
int main(){
int y;
printf("Enter the year to check: ");
scanf("%d",&y);
if (((y % 4 == 0) && (y % 100!= 0)) || (y%400 == 0))
printf("It is a leap year");
else
printf("It is not a leap year");
return 0;
}
9. WAP to print hailstone series
#include<stdio.h>
int main(){
int n;
printf("Enter the number to generate hailstone sequence: ");
scanf("%d", &n);
while(n > 1){
if(n%2 == 0)
n = n / 2;
else
n = (3 * n)+ 1;
printf("%d\n", n);
}
}
10. Using macro to find Number of seconds in the year
#include <stdio.h>
// Define macro to calculate the number of seconds in a year
#define SECONDS_IN_YEAR(year) ((year % 4 == 0 && year % 100 != 0) || (year %
400 == 0) ? (366 * 24 * 60 * 60) : (365 * 24 * 60 * 60))
int main() {
int year;
// Input the year from the user
printf("Enter the year: ");
scanf("%d", &year);
// Calculate and print the number of seconds in the year
printf("Number of seconds in the year %d: %ld\n", year,
(long)SECONDS_IN_YEAR(year));
return 0;
}
11. WAP to print sort array
#include <stdio.h>
void main()
{
int arr1[100];
int n, i, j, tmp;
// Prompt user for input
printf("\n\nSort elements of array in ascending order:\n");
printf("----------------------------------------------\n");
printf("Input the size of array : ");
scanf("%d", &n);
// Input elements for the array
printf("Input %d elements in the array :\n", n);
for (i = 0; i < n; i++)
{
printf("element - %d : ", i);
scanf("%d", &arr1[i]);
}
// Sorting elements in ascending order using the Bubble Sort algorithm
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (arr1[j] < arr1[i])
{
// Swap elements if they are in the wrong order
tmp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = tmp;
}
}
}
// Print sorted elements in ascending order
printf("\nElements of array in sorted ascending order:\n");
for (i = 0; i < n; i++)
{
printf("%d ", arr1[i]);
}
printf("\n\n");
}
12. WAP to check the given string is palindrome or not
#include <stdio.h>
#include <string.h>
#include <ctype.h>
// Function to check if a string is a palindrome
int isPalindrome(char str[]) {
int left = 0;
int right = strlen(str) - 1;
while (left < right) {
// Ignore non-alphanumeric characters and compare case-insensitively
while (left < right && !isalnum(str[left])) left++;
while (left < right && !isalnum(str[right])) right--;
if (tolower(str[left]) != tolower(str[right])) {
return 0; // Not a palindrome
}
left++;
right--;
}
return 1; // Palindrome
}
int main() {
char str[100];
// Input the string from the user
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
// Remove newline character from the end if it exists
str[strcspn(str, "\n")] = '\0';
// Check if the string is a palindrome
if (isPalindrome(str)) {
printf("The string is a palindrome.\n");
} else {
printf("The string is not a palindrome.\n");
}
return 0;
}
13. WAP to multiplication table
#include <stdio.h>
int main()
{
int n, i;
printf("Enter a number: ");
scanf("%d", &n);
printf("Multiplication table of %d:\n ", n);
printf("--------------------------\n");
for (i = 1; i <= 10; i++)
printf("%d x %d = %d\n", n, i, n * i);
return 0;
}
14. WAP to reverse the given array
#include<stdio.h>
int main()
{
int n, arr[n], i;
printf("Enter the size of the array: ");
scanf("%d", &n);
printf("Enter the elements: ");
for(i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
int rev[n], j = 0;
for(i = n-1; i >= 0; i--)
{
rev[j] = arr[i];
j++;
}
printf("The Reversed array: ");
for(i = 0; i < n; i++)
{
printf("%d ", rev[i]);
}
}
15. WAP to sort negative elements and positive elements in an array
#include <stdio.h>
// Function to swap two integers
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
// Function to partition the array into negative and positive elements
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j <= high - 1; j++) {
if (arr[j] < pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
// Function to sort negative and positive elements separately
void sortNegPos(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
sortNegPos(arr, low, pi - 1); // Sort negative elements
sortNegPos(arr, pi + 1, high); // Sort positive elements
}
}
int main() {
int arr[] = {5, -3, 10, -6, 8, -1, -4, 7};
int n = sizeof(arr) / sizeof(arr[0]);
// Separate negative and positive elements
int i = -1;
for (int j = 0; j < n; j++) {
if (arr[j] < 0) {
i++;
swap(&arr[i], &arr[j]);
}
}
// Sort negative and positive elements separately
sortNegPos(arr, 0, i); // Sort negative elements
sortNegPos(arr, i + 1, n - 1); // Sort positive elements
// Print the sorted array
printf("Sorted array with negative elements first:\n");
for (int k = 0; k < n; k++) {
printf("%d ", arr[k]);
}
printf("\n");
return 0;
}
16. WAP to calculate X power y using recursion
#include <stdio.h>
// Function to calculate power using recursion
long long power(int base, int exponent) {
if (exponent == 0) {
return 1; // Base case: any number raised to the power of 0 is 1
}
return base * power(base, exponent - 1); // Recursive step
}
int main() {
int base, exponent;
// Input base and exponent from the user
printf("Enter the base: ");
scanf("%d", &base);
printf("Enter the exponent: ");
scanf("%d", &exponent);
// Calculate the power
long long result = power(base, exponent);
// Output the result
printf("%d raised to the power of %d is: %lld\n", base, exponent, result);
return 0;
}
17. WAP to in a String you have to make alternative upper and lower
Eg. Str= “hello world”
a. Str=”HELLO WORLD”
#include <stdio.h>
#include <ctype.h>
// Function to convert string to alternate upper and lower case
void convertAlternateCase(char str[]) {
int i = 0;
int upperFlag = 1; // Start with uppercase
while (str[i] != '\0') {
if (isalpha(str[i])) { // Check if the character is a letter
if (upperFlag) {
str[i] = toupper(str[i]);
} else {
str[i] = tolower(str[i]);
}
upperFlag = !upperFlag; // Toggle the flag
}
i++;
}
}
int main() {
char str[100];
// Input the string from the user
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
// Remove newline character from the end if it exists
str[strcspn(str, "\n")] = '\0';
// Convert the string to alternate upper and lower case
convertAlternateCase(str);
// Output the converted string
printf("Converted string: %s\n", str);
return 0;
}
18. Write a Macro set nth bit , clear and biggest of three number
#include <stdio.h>
// Macro to set the nth bit of a number
#define SET_BIT(num, n) ((num) | (1U << (n)))
// Macro to clear the nth bit of a number
#define CLEAR_BIT(num, n) ((num) & ~(1U << (n)))
// Macro to find the biggest of three numbers
#define BIGGEST_OF_THREE(a, b, c) (((a) > (b) && (a) > (c)) ? (a) : ((b) > (c)) ?
(b) : (c))
int main() {
int num = 5; // Example number (binary 0101)
int n = 1; // Example bit position to set and clear
// Set the nth bit
int set_result = SET_BIT(num, n);
printf("Original number: %d\n", num);
printf("After setting bit %d: %d\n", n, set_result);
// Clear the nth bit
int clear_result = CLEAR_BIT(num, n);
printf("After clearing bit %d: %d\n", n, clear_result);
// Find the biggest of three numbers
int a = 10, b = 20, c = 15;
int biggest = BIGGEST_OF_THREE(a, b, c);
printf("The biggest of %d, %d, and %d is: %d\n", a, b, c, biggest);
return 0;
}
1. Write a C Program to convert a hexadecimal number into binary without using
an array.
#include <stdio.h>
int main() {
unsigned int hex;
printf("Enter the hexadecimal: ");
scanf("%X", &hex);
printf("Binary equivalent of 0x%X is: ", hex);
// If the input is 0, print 32 zeros
if (hex == 0) {
for (int i = 0; i < 32; i++) {
printf("0");
}
} else {
// Iterate over each bit starting from the leftmost
for (int i = 31; i >= 0; i--) {
// Extract the bit at position i
int bit = (hex >> i) & 1;
printf("%d", bit); // Print the bit
}
}
printf("\n");
return 0;
}
2. Write a C program to find the sum of ‘n’ odd natural numbers and display the
numbers too.
#include <stdio.h>
int main() {
int n, sum = 0;
// Ask the user for the number of odd natural numbers
printf("Enter the number of odd natural numbers to sum: ");
scanf("%d", &n);
// Display the odd natural numbers and calculate the sum
printf("The odd numbers are: %d\n", n);
for(int i = 0; i < n; i++) {
int odd_number = 2 * i + 1;
printf("%d ", odd_number);
sum += odd_number;
}
// Display the sum
printf("\nThe Sum of %d odd natural numbers is: %d\n", n, sum);
return 0;
}
3. Write a C Program to insert New value in the array(sorted list)
#include <stdio.h>
int main() {
int arr[100], n, i, k, j;
printf("Enter the size of the array: ");
scanf("%d", &n);
printf("Enter the elements of the array: ");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the new value to be inserted: ");
scanf("%d", &k);
// Find the position where the new value should be inserted
for (i = 0; i < n; i++) {
if (arr[i] > k) {
break;
}
}
// Shift all the elements after the insertion point one position to the right
for (j = n - 1; j >= i; j--) {
arr[j + 1] = arr[j];
}
// Insert the new value at the desired position
arr[i] = k;
// Print the sorted array with the new value inserted
printf("Array elements after inserting : ");
for (i = 0; i < n + 1; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
4. Write a Program in C to find the smallest positive number missing from an
unsorted array
#include <stdio.h>
#include <stdlib.h>
// Function to swap two integers
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
// Function to find the smallest positive number missing from an unsorted array
int findMissingPositive(int arr[], int size) {
int max = 0;
// Find the maximum element in the array
for (int i = 0; i < size; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
// If max is negative or zero, return 1
if (max <= 0) {
return 1;
}
// Create an auxiliary array to mark the presence of positive numbers
int *aux = (int *)calloc(max, sizeof(int));
// Mark the presence of positive numbers in the auxiliary array
for (int i = 0; i < size; i++) {
if (arr[i] > 0) {
aux[arr[i] - 1] = 1;
}
}
// Find the first missing positive number
for (int i = 0; i < max; i++) {
if (aux[i] == 0) {
free(aux);
return i + 1;
}
}
free(aux);
return max + 1;
}
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter array elements: ");
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
int missing = findMissingPositive(arr, size);
printf("The smallest positive number missed is: %d\n", missing);
return 0;
}
5. Write a function given a string s, print ‘Yes’ if it has a vowel in it else print ‘no’.
#include <stdio.h>
// Function to check if a string contains a vowel
int hasVowel(const char *str) {
while (*str) {
if (*str == 'a' || *str == 'e' || *str == 'i' || *str == 'o' || *str == 'u' ||
*str == 'A' || *str == 'E' || *str == 'I' || *str == 'O' || *str == 'U') {
return 1; // Return 1 if a vowel is found
}
str++; // Move to the next character in the string
}
return 0; // Return 0 if no vowel is found
}
int main() {
char str1[] = "prime prime EMERTXE";
if (hasVowel(str1))
printf("Yes, vowel is present in the string\n");
else
printf("No, vowel is not present in the string\n");
char str2[] = "try";
if (hasVowel(str2))
printf("Yes, vowel is present in the string\n");
else
printf("No, vowel is not present in the string\n");
return 0;
}
6. Write a c function Given string ‘s’ print the sum of weight of the string. A weight
of character is defined as the ascii value of corresponding character.
#include <stdio.h>
// Function to calculate the sum of the ASCII values of characters in a string
int sumOfWeights(const char *s) {
int sum = 0;
// Iterate through the string and add the ASCII value of each character to the sum
while (*s) {
sum += *s;
s++;
}
return sum;
}
int main() {
char str[] = "afgh";
int sum = sumOfWeights(str);
printf("String weight: %d\n", sum);
return 0;
}
7. Write a c program to find the GCD (Greatest Common Divisor) of two numbers
using a function
include <stdio.h>
// Function to find the GCD of two numbers
int findGCD(int num1, int num2) {
// Base case
if (num2 == 0)
return num1;
// Recursive call to find GCD
return findGCD(num2, num1 % num2);
}
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
// Check if numbers are positive
if (num1 <= 0 || num2 <= 0) {
printf("Error: Enter Positive values\n");
return 0;
}
int gcd = findGCD(num1, num2);
printf("GCD of %d and %d is %d\n", num1, num2, gcd);
return 0;
}
8. Write a C program to merge two arrays into a third array using pointers.
#include <stdio.h>
int main()
{
int n1,n2,n3; //Array Size Declaration
int a[10000], b[10000], c[20000];
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;
for(int i = 0; i < n1; i++)
c[i] = a[i];
for(int i = 0; i < n2; i++)
c[i + n1] = b[i];
printf("The merged array: ");
for(int i = 0; i < n3; i++)
printf("%d ", c[i]); //Print the merged array
//printf("\nFinal array after sorting: ");
for(int i = 0; i < n3; i++){
int temp;
for(int j = i + 1; j < n3; j++) {
if(c[i] > c[j]) {
temp = c[i];
c[i] = c[j];
c[j] = temp;
}
}
}
for(int i = 0; i < n3 ; i++) //Print the sorted Array
// printf(" %d ",c[i]);
return 0;
}
9. Write a program to find the transpose of a matrix
#include <stdio.h>
int main() {
int a[10][10], transpose[10][10], r, c;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
// asssigning elements to the matrix
printf("\nEnter matrix elements:\n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
// printing the matrix a[][]
printf("\nEntered matrix: \n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf("%d ", a[i][j]);
if (j == c - 1)
printf("\n");
}
// computing the transpose
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
transpose[j][i] = a[i][j];
}
// printing the transpose
printf("\nAfter transpose:\n");
for (int i = 0; i < c; ++i)
for (int j = 0; j < r; ++j) {
printf("%d ", transpose[i][j]);
if (j == r - 1)
printf("\n");
}
return 0;
}
10. Write a C program which find the no. of days in the given month
#include <stdio.h>
int main() {
int month;
printf("Enter the month : ");
scanf("%d", &month);
// Define your logic here
switch (month) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
printf("No. of days in the given month is 31\n");
break;
case 4: case 6: case 9: case 11:
printf("No. of days in the given month is 30\n");
break;
case 2:
printf("No. of days in the given month is 28/29\n");
break;
default:
printf("Invalid input\n");
break;
}
return 0;
}
19. Find the index of given number in a matrix
#include <stdio.h>
#define ROWS 3
#define COLS 3
void find_index(int matrix[ROWS][COLS], int target) {
int row, col;
int found = 0;
for (row = 0; row < ROWS; row++) {
for (col = 0; col < COLS; col++) {
if (matrix[row][col] == target) {
printf("Element %d found at index [%d][%d]\n", target, row, col);
found = 1;
}
}
}
if (!found) {
printf("Element %d not found in the matrix.\n", target);
}
}
int main() {
int matrix[ROWS][COLS] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int target = 5;
find_index(matrix, target);
return 0;
}
20. Using 2D array Reverse each row
#include <stdio.h>
#define ROWS 3
#define COLS 3
void reverse_row(int matrix[ROWS][COLS]) {
int row, col, temp;
for (row = 0; row < ROWS; row++) {
int start = 0;
int end = COLS - 1;
while (start < end) {
// Swap elements
temp = matrix[row][start];
matrix[row][start] = matrix[row][end];
matrix[row][end] = temp;
start++;
end--;
}
}
}
void print_matrix(int matrix[ROWS][COLS]) {
int row, col;
printf("Reversed Matrix:\n");
for (row = 0; row < ROWS; row++) {
for (col = 0; col < COLS; col++) {
printf("%d ", matrix[row][col]);
}
printf("\n");
}
}
int main() {
int matrix[ROWS][COLS] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
printf("Original Matrix:\n");
print_matrix(matrix);
reverse_row(matrix);
print_matrix(matrix);
return 0;
}
21. WAP to print the indices of all the occurrence of string in the last string
#include <stdio.h>
#include <string.h>
void find_occurrences(char *main_string, char *substring) {
int index = -1;
while ((index = strstr(main_string + index + 1, substring) - main_string) != -1) {
printf("Found at index: %d\n", index);
}
}
int main() {
char main_string[] = "Hello, Hello, Hello, world!";
char substring[] = "Hello";
printf("Indices of occurrences:\n");
find_occurrences(main_string, substring);
return 0;
}
22. WAP a function to count the no. of jumps to reach the last
#include <stdio.h>
int minJumps(int arr[], int n) {
if (n <= 1)
return 0;
// If first element is 0, then cannot move
if (arr[0] == 0)
return -1;
// Initialize variables
int maxReach = arr[0]; // Maximum index that can be reached from current position
int steps = arr[0]; // Number of steps allowed from current position
int jumps = 1; // Number of jumps made so far
// Iterate through the array
for (int i = 1; i < n; i++) {
// If reached the end of array
if (i == n - 1)
return jumps;
// Update maxReach if current index plus its value is greater
if (i + arr[i] > maxReach)
maxReach = i + arr[i];
// Reduce steps
steps--;
// If no steps left, make a jump to maxReach
if (steps == 0) {
jumps++;
// Check if the current index can be reached or not
if (i >= maxReach)
return -1;
steps = maxReach - i; // Remaining steps
}
}
return -1; // If cannot reach the last element
}
int main() {
int arr[] = {2, 3, 1, 1, 4}; // Example array
int n = sizeof(arr) / sizeof(arr[0]);
int minJumpsRequired = minJumps(arr, n);
if (minJumpsRequired != -1)
printf("Minimum number of jumps required to reach the end: %d\n",
minJumpsRequired);
else
printf("Cannot reach the end of the array.\n");
return 0;
}
23. WAP a function to print the Spiral matrix
#include <stdio.h>
#define MAX 10
void fillSpiral(int n, int mat[MAX][MAX]) {
int val = 1; // Start filling with 1
int top = 0; // Initialize the top boundary
int bottom = n - 1; // Initialize the bottom boundary
int left = 0; // Initialize the left boundary
int right = n - 1; // Initialize the right boundary
while (val <= n * n) {
// Fill the top row from left to right
for (int i = left; i <= right; i++) {
mat[top][i] = val++;
}
top++;
// Fill the right column from top to bottom
for (int i = top; i <= bottom; i++) {
mat[i][right] = val++;
}
right--;
// Fill the bottom row from right to left
for (int i = right; i >= left; i--) {
mat[bottom][i] = val++;
}
bottom--;
// Fill the left column from bottom to top
for (int i = bottom; i >= top; i--) {
mat[i][left] = val++;
}
left++;
}
}
void printMatrix(int n, int mat[MAX][MAX]) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%2d ", mat[i][j]);
}
printf("\n");
}
}
int main() {
int n;
int mat[MAX][MAX];
// Input the size of the matrix
printf("Enter the size of the matrix (max %d): ", MAX);
scanf("%d", &n);
// Check if the input size is within the allowed limit
if (n > MAX) {
printf("Size exceeds maximum allowed value of %d\n", MAX);
return 1;
}
// Fill the matrix in spiral order and print it
fillSpiral(n, mat);
printMatrix(n, mat);
return 0;
}
24. WAP to print patterns triangle, Square, Pyramid, diamond
#include <stdio.h>
void printTriangle(int n) {
int i, j;
for (i = 0; i < n; i++) {
for (j = 0; j <= i; j++) {
printf("* ");
}
printf("\n");
}
}
int main() {
int n = 5; // Height of the triangle
printf("Triangle Pattern:\n");
printTriangle(n);
return 0;
}\
Square pattern
#include <stdio.h>
void printSquare(int n) {
int i, j;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("* ");
}
printf("\n");
}
}
int main() {
int n = 5; // Size of the square
printf("Square Pattern:\n");
printSquare(n);
return 0;
}
Pyramid pattern
#include <stdio.h>
void printPyramid(int n) {
int i, j;
for (i = 0; i < n; i++) {
for (j = 0; j < n - i - 1; j++) {
printf(" ");
}
for (j = 0; j <= i; j++) {
printf("* ");
}
printf("\n");
}
}
int main() {
int n = 5; // Height of the pyramid
printf("Pyramid Pattern:\n");
printPyramid(n);
return 0;
}
Diamond pattern
#include <stdio.h>
void printDiamond(int n) {
int i, j;
// Upper half
for (i = 0; i < n; i++) {
for (j = 0; j < n - i - 1; j++) {
printf(" ");
}
for (j = 0; j <= i; j++) {
printf("* ");
}
printf("\n");
}
// Lower half
for (i = n - 2; i >= 0; i--) {
for (j = 0; j < n - i - 1; j++) {
printf(" ");
}
for (j = 0; j <= i; j++) {
printf("* ");
}
printf("\n");
}
}
int main() {
int n = 5; // Height of the diamond
printf("Diamond Pattern:\n");
printDiamond(n);
return 0;
}
25. WAP to print the given system is little endian or Big endian
#include <stdio.h>
int main() {
unsigned int num = 1;
char *ptr = (char *)#
if (*ptr) {
printf("The system is little endian.\n");
} else {
printf("The system is big endian.\n");
}
return 0;
}
26. WAP to print prime numbers
#include <stdio.h>
int isPrime(int num) {
if (num <= 1) {
return 0; // Not prime
}
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
return 0; // Not prime
}
}
return 1; // Prime
}
void printPrimes(int start, int end) {
printf("Prime numbers between %d and %d are:\n", start, end);
for (int i = start; i <= end; i++) {
if (isPrime(i)) {
printf("%d ", i);
}
}
printf("\n");
}
int main() {
int start = 2; // Start of the range
int end = 100; // End of the range
printPrimes(start, end);
return 0;
}
27. WAP to replace an 0’s with 1’s in a integer
#include <stdio.h>
int main() {
int number = 1023040; // Example number
int originalNumber = number;
int replacedNumber = 0;
int multiplier = 1;
while (number != 0) {
int digit = number % 10;
if (digit == 0) {
digit = 1;
}
replacedNumber += digit * multiplier;
multiplier *= 10;
number /= 10;
}
printf("Original number: %d\n", originalNumber);
printf("Number after replacing 0's with 1's: %d\n", replacedNumber);
return 0;
}