0% found this document useful (0 votes)
2 views

notepad c programing

The document contains a series of C programming exercises, each demonstrating different programming concepts such as variable rotation, digit extraction, sum of digits, leap year determination, and matrix operations. Each exercise includes a code snippet that implements the specified functionality, along with comments explaining the code. The exercises cover a wide range of topics including recursion, sorting, and string manipulation.

Uploaded by

Rahul Dixit
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

notepad c programing

The document contains a series of C programming exercises, each demonstrating different programming concepts such as variable rotation, digit extraction, sum of digits, leap year determination, and matrix operations. Each exercise includes a code snippet that implements the specified functionality, along with comments explaining the code. The exercises cover a wide range of topics including recursion, sorting, and string manipulation.

Uploaded by

Rahul Dixit
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

​ Given the values of the variables x, y and z, write a

1
program to rotate their values such that x has the
value of y, y has the value of z, and z has the value of
x

#include <stdio.h>

int main() {
int x, y, z;

// Input values of x, y, and z


printf("Enter the values of x, y, and z: ");
scanf("%d %d %d", &x, &y, &z);

// Rotate values
int temp = x;
x = y;
y = z;
z = temp;

// Display rotated values


printf("Rotated values: x=%d, y=%d, z=%d\n", x, y,
z);
return 0;
}

2 Write a program that reads a floating point number


and then displays the right-most digit of the integral
part of the number.

#include <stdio.h>

int main() {
float number;

// Input a floating point number


printf("Enter a floating point number: ");
scanf("%f", &number);

// Extract right-most digit of integral part


int rightMostDigit = (int)number % 10;

// Display the result


printf("Right-most digit of the integral part: %d\n",
rightMostDigit);
return 0;
}

3 Write a C program to calculate the sum of digits of


given number.

#include <stdio.h>

int main() {
int number, digit, sum = 0;

// Input a number
printf("Enter a number: ");
scanf("%d", &number);

// Calculate the sum of digits


while (number > 0) {
digit = number % 10;
sum += digit;
number /= 10;
}

// Display the result


printf("Sum of digits: %d\n", sum);
return 0;
}

4 Program to find largest and smallest number from


four given number.

#include <stdio.h>

int main() {
int num1, num2, num3, num4;

// Input four numbers


printf("Enter four numbers: ");
scanf("%d %d %d %d", &num1, &num2, &num3,
&num4);

// Find the largest number


int largest = (num1 > num2) ? ((num1 > num3) ?
((num1 > num4) ? num1 : num4) : ((num3 > num4) ?
num3 : num4)) : ((num2 > num3) ? ((num2 > num4) ?
num2 : num4) : ((num3 > num4) ? num3 : num4));

// Find the smallest number


int smallest = (num1 < num2) ? ((num1 < num3) ?
((num1 < num4) ? num1 : num4) : ((num3 < num4) ?
num3 : num4)) : ((num2 < num3) ? ((num2 < num4) ?
num2 : num4) : ((num3 < num4) ? num3 : num4));
// Display the results
printf("Largest number: %d\n", largest);
printf("Smallest number: %d\n", smallest);

return 0;
}

5 Program to find whether a year is leap or not


#include <stdio.h>

int main() {
int year;

// Input a year
printf("Enter a year: ");
scanf("%d", &year);

// Check for leap year


if ((year % 4 == 0 && year % 100 != 0) || (year % 400
== 0)) {
printf("%d is a leap year.\n", year);
} else {
printf("%d is not a leap year.\n", year);
}
return 0;
}

6 Write a C program in which enter any number by


the user and perform the operation of Sum of digits
of
entered number.
#include <stdio.h>

int main() {
int number, digit, sum = 0;

// Input a number
printf("Enter a number: ");
scanf("%d", &number);

// Calculate the sum of digits


while (number > 0) {
digit = number % 10;
sum += digit;
number /= 10;
}
// Display the result
printf("Sum of digits: %d\n", sum);

return 0;
}

7 Write a C Program to convert Decimal number to


Binary number
#include <stdio.h>

void decToBinary(int n) {
// Base case
if (n == 0) {
return;
}

// Recursive call
decToBinary(n / 2);

// Print binary equivalent


printf("%d", n % 2);
}

int main() {
int decimalNumber;

// Input a decimal number


printf("Enter a decimal number: ");
scanf("%d", &decimalNumber);

// Display binary equivalent


printf("Binary equivalent: ");
decToBinary(decimalNumber);

return 0;
}

8 Find the sum of this series upto n terms


1+2+3+4+5+6+………..

#include <stdio.h>

int main() {
int n, sum = 0;

// Input the number of terms


printf("Enter the number of terms (n): ");
scanf("%d", &n);

// Calculate the sum of the series


for (int i = 1; i <= n; i++) {
sum += i;
}

// Display the result


printf("Sum of the series up to %d terms: %d\n", n,
sum);

return 0;
}

9 Program to print Armstrong‘s numbers from 1 to


100.

#include <stdio.h>
#include <math.h>

int isArmstrong(int num) {


int originalNum, remainder, n = 0, result = 0;

originalNum = num;
while (originalNum != 0) {
originalNum /= 10;
++n;
}

originalNum = num;

while (originalNum != 0) {
remainder = originalNum % 10;
result += pow(remainder, n);
originalNum /= 10;
}

if (result == num)
return 1;
else
return 0;
}

int main() {
printf("Armstrong numbers from 1 to 100:\n");

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


if (isArmstrong(i))
printf("%d\n", i);
}

return 0;
}

10 Write a program to convert years into Minute,


Hours, Days, Months, Seconds using switch ()
statements

#include <stdio.h>

int main() {
int years;

// Input the number of years


printf("Enter the number of years: ");
scanf("%d", &years);

int choice;

// Display conversion options


printf("Choose conversion:\n");
printf("1. Minutes\n");
printf("2. Hours\n");
printf("3. Days\n");
printf("4. Months\n");
printf("5. Seconds\n");

// Input choice
printf("Enter your choice (1-5): ");
scanf("%d", &choice);

// Perform conversion based on the choice using


switch statement
switch (choice) {
case 1:
printf("%d years is equal to %d minutes.\n",
years, years * 365 * 24 * 60);
break;
case 2:
printf("%d years is equal to %d hours.\n",
years, years * 365 * 24);
break;
case 3:
printf("%d years is equal to %d days.\n", years,
years * 365);
break;
case 4:
printf("%d years is equal to %d months.\n",
years, years * 12);
break;
case 5:
printf("%d years is equal to %lld seconds.\n",
years, (long long)years * 365 * 24 * 60 * 60);
break;
default:
printf("Invalid choice.\n");
break;
}

return 0;
}

11 Write a C menu driven program

#include <stdio.h>

// Function declarations
void option1();
void option2();
void option3();
int main() {
int choice;

do {
// Display menu
printf("\nMenu:\n");
printf("1. Option 1\n");
printf("2. Option 2\n");
printf("3. Option 3\n");
printf("4. Exit\n");

// Input choice
printf("Enter your choice (1-4): ");
scanf("%d", &choice);

// Perform action based on choice using switch


statement
switch (choice) {
case 1:
option1();
break;
case 2:
option2();
break;
case 3:
option3();
break;
case 4:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice. Please enter a valid
option.\n");
break;
}
} while (choice != 4);

return 0;
}

// Sample functions for each option


void option1() {
printf("Option 1 selected.\n");
}

void option2() {
printf("Option 2 selected.\n");
}
void option3() {
printf("Option 3 selected.\n");
}

12 Write a program to generate the various pattern of


numbers

#include <stdio.h>

int main() {
int n;

// Input the number of rows


printf("Enter the number of rows: ");
scanf("%d", &n);

// Pattern 1
printf("Pattern 1:\n");
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("%d ", j);
}
printf("\n");
}

// Pattern 2
printf("\nPattern 2:\n");
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("%d ", i);
}
printf("\n");
}

return 0;
}

13 Write a C Program to print the reverse of an


integer number

#include <stdio.h>

int main() {
int num, reversed = 0, remainder;

// Input an integer
printf("Enter an integer: ");
scanf("%d", &num);
// Reverse the number
while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}

// Display the reversed number


printf("Reversed number: %d\n", reversed);

return 0;
}

14 Write a C program to perform the factorial of


given number

#include <stdio.h>

// Function to calculate factorial


int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}

int main() {
int num;

// Input a number
printf("Enter a number: ");
scanf("%d", &num);

// Display the factorial


printf("Factorial of %d: %d\n", num,
factorial(num));

return 0;
}

15 Write a C program in which a function prime that


returns 1 if its argument is a prime and return zero
otherwise.

#include <stdio.h>

// Function to check if a number is prime


int isPrime(int n) {
if (n <= 1) {
return 0; // Not prime
}

for (int i = 2; i * i <= n; i++) {


if (n % i == 0) {
return 0; // Not prime
}
}

return 1; // Prime
}

int main() {
int num;

// Input a number
printf("Enter a number: ");
scanf("%d", &num);

// Check if the number is prime


if (isPrime(num)) {
printf("%d is a prime number.\n", num);
} else {
printf("%d is not a prime number.\n", num);
}

return 0;
}

16 Write a C program to calculate factorial of a


number using recursion.

#include <stdio.h>

// Function to calculate factorial using recursion


int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}

int main() {
int num;

// Input a number
printf("Enter a number: ");
scanf("%d", &num);

// Display the factorial


printf("Factorial of %d: %d\n", num,
factorial(num));

return 0;
}

17 Write a C program in which enter 10 elements by


the user and perform the operation of sorting in
ascending order

#include <stdio.h>

int main() {
int array[10], i, j, temp;

// Input 10 elements
printf("Enter 10 elements:\n");
for (i = 0; i < 10; i++) {
printf("Enter element %d: ", i + 1);
scanf("%d", &array[i]);
}

// Sorting in ascending order using bubble sort


for (i = 0; i < 9; i++) {
for (j = 0; j < 9 - i; j++) {
if (array[j] > array[j + 1]) {
// Swap elements
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}

// Display the sorted array


printf("Sorted array in ascending order:\n");
for (i = 0; i < 10; i++) {
printf("%d ", array[i]);
}
printf("\n");

return 0;
}
18 Write a C program to perform to perform Matrix
addition and multiplication operations.

#include <stdio.h>

void matrixAddition(int a[3][3], int b[3][3], int


result[3][3]) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
result[i][j] = a[i][j] + b[i][j];
}
}
}

void matrixMultiplication(int a[3][3], int b[3][3], int


result[3][3]) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
result[i][j] = 0;
for (int k = 0; k < 3; k++) {
result[i][j] += a[i][k] * b[k][j];
}
}
}
}
void displayMatrix(int matrix[3][3]) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}

int main() {
int matrixA[3][3], matrixB[3][3], resultAddition[3]
[3], resultMultiplication[3][3];

// Input matrices A and B


printf("Enter matrix A (3x3):\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
scanf("%d", &matrixA[i][j]);
}
}

printf("Enter matrix B (3x3):\n");


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
scanf("%d", &matrixB[i][j]);
}
}

// Perform matrix addition


matrixAddition(matrixA, matrixB, resultAddition);

// Perform matrix multiplication


matrixMultiplication(matrixA, matrixB,
resultMultiplication);

// Display results
printf("Matrix A:\n");
displayMatrix(matrixA);

printf("Matrix B:\n");
displayMatrix(matrixB);

printf("Matrix Addition:\n");
displayMatrix(resultAddition);

printf("Matrix Multiplication:\n");
displayMatrix(resultMultiplication);

return 0;
}

19 Write a program to determine the length of the


string and find its equivalent ASCII codes.

#include <stdio.h>
#include <string.h>

int main() {
char inputString[100];

// Input a string
printf("Enter a string: ");
gets(inputString);

// Calculate and display the length of the string


printf("Length of the string: %d\n",
strlen(inputString));

// Display equivalent ASCII codes


printf("Equivalent ASCII codes:\n");
for (int i = 0; i < strlen(inputString); i++) {
printf("%c: %d\n", inputString[i], inputString[i]);
}
return 0;
}

20 Write a program to delete all the occurrences of


the vowels in a given text. Assume that the text
length
will be of one line

#include <stdio.h>
#include <string.h>

// Function to check if a character is a vowel


int isVowel(char ch) {
return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o'
|| ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' ||
ch == 'U');
}

int main() {
char inputText[100], resultText[100];
int j = 0;

// Input a text
printf("Enter a text: ");
gets(inputText);
// Delete occurrences of vowels
for (int i = 0; i < strlen(inputText); i++) {
if (!isVowel(inputText[i])) {
resultText[j++] = inputText[i];
}
}

resultText[j] = '\0'; // Null-terminate the result string

// Display the result text without vowels


printf("Text after deleting vowels: %s\n",
resultText);

return 0;
}

21 Write a program to maintain the library record for


100 books with book name, author‘s name,
and edition, year of publishing and price of the book.

#include <stdio.h>

struct Book {
char name[100];
char author[100];
char edition[20];
int year;
float price;
};

int main() {
struct Book library[100];

// Input details for each book in the library


for (int i = 0; i < 100; i++) {
printf("\nEnter details for book #%d:\n", i + 1);

printf("Enter book name: ");


scanf("%s", library[i].name);

printf("Enter author's name: ");


scanf("%s", library[i].author);

printf("Enter edition: ");


scanf("%s", library[i].edition);

printf("Enter year of publishing: ");


scanf("%d", &library[i].year);
printf("Enter price of the book: ");
scanf("%f", &library[i].price);
}

// Display the library records


printf("\nLibrary Records:\n");
for (int i = 0; i < 100; i++) {
printf("\nDetails for book #%d:\n", i + 1);
printf("Book Name: %s\n", library[i].name);
printf("Author's Name: %s\n", library[i].author);
printf("Edition: %s\n", library[i].edition);
printf("Year of Publishing: %d\n", library[i].year);
printf("Price of the Book: %.2f\n", library[i].price);
}

return 0;
}

You might also like