0% found this document useful (0 votes)
9 views13 pages

Final C Imp

The document contains various C programming examples covering sorting algorithms (insertion sort, selection sort, bubble sort), file modes, conditional operators, and data structures like structs and unions. It also includes examples for mathematical operations such as finding prime numbers, palindromes, Armstrong numbers, and calculating factorials. Additionally, it discusses pointers, binary search, and converting decimal to binary.

Uploaded by

architj0415
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views13 pages

Final C Imp

The document contains various C programming examples covering sorting algorithms (insertion sort, selection sort, bubble sort), file modes, conditional operators, and data structures like structs and unions. It also includes examples for mathematical operations such as finding prime numbers, palindromes, Armstrong numbers, and calculating factorials. Additionally, it discusses pointers, binary search, and converting decimal to binary.

Uploaded by

architj0415
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

INSERTION SORT

#include <stdio.h>

void insertionSort(int array[], int size) {

int i, j, key;

for (i = 1; i < size; i++) {

key = array[i];

j = i - 1;

while (j >= 0 && array[j] > key) {

array[j + 1] = array[j];

j = j - 1; }

array[j + 1] = key;

int main() {

int array[] = {5, 9, 1, 3, 2};

int size = sizeof(array) / sizeof(array[0]);

printf("Original array: ");

for (int i = 0; i < size; i++) {

printf("%d ", array[i]); }

printf("\n");

insertionSort(array, size);

printf("Sorted array in ascending order: ");

for (int i = 0; i < size; i++) {

printf("%d ", array[i]); }

printf("\n");

return 0; }
FILE MODES

1. "r" - Read Mode:


 Opens the file for reading.
 If the file does not exist, fopen() returns NULL.
 The file pointer is positioned at the beginning of the file.
 Example: FILE *file = fopen("example.txt", "r");
2. "w" - Write Mode:
 Opens the file for writing.
 If the file exists, its contents are truncated (cleared).
 If the file does not exist, a new file is created.
 The file pointer is positioned at the beginning of the file.
 Example: FILE *file = fopen("example.txt", "w");
3. "a" - Append Mode:
 Opens the file for appending (writing at the end of the
file).
 If the file exists, the file pointer is positioned at the end of
the file.
 If the file does not exist, a new file is created.
 Example: FILE *file = fopen("example.txt", "a");
4. "r+" - Read/Write Mode:
 Opens the file for both reading and writing.
 The file must exist; otherwise, fopen() returns NULL.
 The file pointer is positioned at the beginning of the file.
 Example: FILE *file = fopen("example.txt", "r+");
5. "w+" - Read/Write Mode:
 Opens the file for both reading and writing.
 If the file exists, its contents are truncated (cleared).
 If the file does not exist, a new file is created.
 The file pointer is positioned at the beginning of the file.
 Example: FILE *file = fopen("example.txt", "w+");
6. "a+" - Read/Append Mode:
 Opens the file for reading and appending (writing at the
end of the file).
 If the file exists, the file pointer is positioned at the end of
the file.
 If the file does not exist, a new file is created.
 Example: FILE *file = fopen("example.txt", "a+");
S.
No. Text file Binary File

Binary files cannot easily be


transferred from one computer
The text files can easily be
system to another due to variations
1. transferred from one computer
in the internal variations in the
system to another.
internal representation which varies
from computer to computer.

It stores data using ASCII format


It stores data in binary format i.e.
2. i.e. human-readable graphic
with the help of 0 and 1.
characters.

These files create portability


4. These files are easily portable.
problems.

The ios:: binary mode has to be


6. Any file is by default text file. used with binary files while opening
them.

Error in a textual file can be easily Error in a binary file corrupts the file
7.
recognized and eliminated. and is not easily detected.

In a text file, a new line character


is first converted to a carriage
In binary file, no such conversion
return-line feed combination and
8. from newline to carriage return-line
then written to the disk. Vice
feed combination is done.
versa happens when a line is read
from the text file.

In a text file, a special character


with ASCII code 26 is inserted at
There is no such special character in
9. the end of the file. This character
the binary file to signal EOF.
signals the EOF to the program
when encountered.

Text files are used to store data Binary files are used to store data
10.
more user friendly. more compactly.

Mostly .txt and .rtf are used as Can have any application defined
11.
extensions to text files. extension.
Conditional Operator

The conditional operator is also known as a ternary operator.


The conditional statements are the decision-making statements
which depends upon the output of the expression. It is
represented by two symbols, i.e., '?' and ':'. As conditional
operator works on three operands, so it is also known as the
ternary operator. The behavior of the conditional operator is
similar to the 'if-else' statement as 'if-else' statement is also a
decision-making statement. Syntax:
Expression1? expression2: expression3;

#include <stdio.h>

int main() {

int number;

printf("Enter a number: ");

scanf("%d", &number);

number >= 0 ? printf("The number is positive.\n") : printf("The


number is negative.\n");

return 0; }

typedef

The typedef is a keyword used in C programming to provide some


meaningful names to the already existing variable in the C program. It
behaves similarly as we define the alias for the commands. In short, we can
say that this keyword is used to redefine the name of an already existing
variable.

Syntax: typedef <existing_name> <alias_name>

#include <stdio.h>
int main()
{
typedef unsigned int unit;
unit i,j;
i=10;
j=20;
printf("Value of i is :%d",i);
printf("\nValue of j is :%d",j);
return 0; }
Go to statement
The goto statement in C is used to transfer control to a labeled
statement within the same function. However, it is generally
considered a bad programming practice to use goto as it can
make the code harder to read and maintain.
#include <stdio.h>
int main() {
int number, factorial = 1;
printf("Enter a positive integer: ");
scanf("%d", &number);
if (number < 0) {
printf("Error: Factorial is not defined for negative numbers.\
n");
return 1; }
int i = 1;
factorial_calculation:
factorial *= i;
i++;
if (i <= number)
goto factorial_calculation;
printf("The factorial of %d is %d.\n", number, factorial);
return 0; }
PRIME NUMBER
1. #include<stdio.h>
2. int main(){
3. int n,i,m=0,flag=0;
4. printf("Enter the number to check prime:");
5. scanf("%d",&n);
6. m=n/2;
7. for(i=2;i<=m;i++)
8. {
9. if(n%i==0)
10. {
11. printf("Number is not prime");
12. flag=1;
13. break;
14. }
15. }
16. if(flag==0)
17. printf("Number is prime");
18. return 0;
19. }

PALINDROME
1. #include<stdio.h>
2. int main()
3. {
4. int n,r,sum=0,temp;
5. printf("enter the number=");
6. scanf("%d",&n);
7. temp=n;
8. while(n>0)
9. {
10. r=n%10;
11. sum=(sum*10)+r;
12. n=n/10;
13. }
14. if(temp==sum)
15. printf("palindrome number ");
16. else
17. printf("not palindrome");
18. return 0;
19. }
ARMSTRONG
1. #include<stdio.h>
2. int main()
3. {
4. int n,r,sum=0,temp;
5. printf("enter the number=");
6. scanf("%d",&n);
7. temp=n;
8. while(n>0)
9. {
10. r=n%10;
11. sum=sum+(r*r*r);
12. n=n/10;
13. }
14. if(temp==sum)
15. printf("armstrong number ");
16. else
17. printf("not armstrong number");
18. return 0;
19. }

Find max from array.


#include <stdio.h>

int findMax(int arr[], int size) {


int max = arr[0];

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


if (arr[i] > max) {
max = arr[i]; }
}

return max;
}

int main() {
int arr[] = {5, 9, 3, 7, 1, 6};
int size = sizeof(arr) / sizeof(arr[0]);

int max = findMax(arr, size);


printf("The maximum element in the array is: %d\n", max);

return 0;
}
profit/loss
#include<stdio.h>
int main()
{
float sp, cp, profit, loss;
printf("Enter the cost price of a product:");
scanf("%f",&cp);
printf("Enter the selling price of a product:");
scanf("%f",&sp);
if(sp>cp){
profit=sp-cp;
printf("Profit:%.2f",profit);
printf("Profit Percentage:%.2f",(profit/cp)*100);
}
else if(cp>sp){
loss=cp-sp;
printf("Loss:%.2f",loss);
printf("Loss Percentage:%.2f",(loss/cp)*100);
}
else{
printf("No Profit No Loss");
}
}

FIBONACCI
#include <stdio.h>
int main() {
int n, first = 0, second = 1, next;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (int i = 0; i < n; i++) {
if (i <= 1)
next = i;
else {
next = first + second;
first = second;
second = next;
}
printf("%d ", next);
}
return 0;
}

SWAP TWO NUMBER


#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int num1, num2;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
printf("Before swapping: num1 = %d, num2 = %d\n", num1,
num2);
swap(&num1, &num2);
printf("After swapping: num1 = %d, num2 = %d\n", num1,
num2);
return 0;
}

SELECTION SORT
#include <stdio.h>
void selectionSort(int arr[], int n) {
int i, j, minIndex, temp;
for (i = 0; i < n-1; i++) {
minIndex = i;
for (j = i+1; j < n; j++) {
if (arr[j] < arr[minIndex])
minIndex = j;
}
temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n"); }
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array: ");
printArray(arr, n);
selectionSort(arr, n);
printf("Sorted array: ");
printArray(arr, n);
return 0; }
POINTER
#include <stdio.h>
int main() {
int num = 10;
int *ptr;
ptr = &num;
printf("Value of num: %d\n", num);
printf("Address of num: %p\n", &num);
printf("Value stored in ptr: %p\n", ptr);
printf("Value pointed to by ptr: %d\n", *ptr);
return 0; }
BINARY SEARCH
#include <stdio.h>
int binarySearch(int arr[], int low, int high, int target) {
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == target)
return mid;
if (arr[mid] < target)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
int main() {
int arr[] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91};
int n = sizeof(arr) / sizeof(arr[0]);
int target = 23;
int result = binarySearch(arr, 0, n - 1, target);
if (result == -1)
printf("Element not found in the array.\n");
else
printf("Element found at index %d.\n", result);
return 0; }
STRUCTURE
#include <stdio.h>
struct Person {
char name[50];
int age;
float height; };
int main() {
struct Person person1;
strcpy(person1.name, "John");
person1.age = 25;
person1.height = 1.75;
printf("Name: %s\n", person1.name);
printf("Age: %d\n", person1.age);
printf("Height: %.2f\n", person1.height);
return 0; }

DECIMAL TO BINARY
#include <stdio.h>
void decimalToBinary(int decimal) {
int binary[32];
int i = 0;
if (decimal == 0) {
printf("Binary: 0\n");
return;
}
while (decimal > 0) {
binary[i] = decimal % 2;
decimal = decimal / 2;
i++;
}
printf("Binary: ");
for (int j = i - 1; j >= 0; j--) {
printf("%d", binary[j]); }
printf("\n"); }
int main() {
int decimal;
printf("Enter a decimal number: ");
scanf("%d", &decimal);
decimalToBinary(decimal);
return 0; }
UNION
#include <stdio.h>
union Data {
int intValue;
float floatValue;
char stringValue[20]; };
int main() {
union Data data;
data.intValue = 10;
printf("Integer value: %d\n", data.intValue);
data.floatValue = 3.14;
printf("Float value: %.2f\n", data.floatValue);
strcpy(data.stringValue, "Hello");
printf("String value: %s\n", data.stringValue);
printf("Size of the union: %lu bytes\n", sizeof(data));
return 0; }
FACTORIAL
#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
int result = factorial(number);
printf("The factorial of %d is: %d\n", number, result);
return 0; }
BUBBLE SORT
#include <stdio.h>
void bubbleSort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
// Swap arr[j] and arr[j+1]
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]); }
printf("\n");
return 0; }

You might also like