Final C Imp
Final C Imp
#include <stdio.h>
int i, j, key;
key = array[i];
j = i - 1;
array[j + 1] = array[j];
j = j - 1; }
array[j + 1] = key;
int main() {
printf("\n");
insertionSort(array, size);
printf("\n");
return 0; }
FILE MODES
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.
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
#include <stdio.h>
int main() {
int number;
scanf("%d", &number);
return 0; }
typedef
#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. }
return max;
}
int main() {
int arr[] = {5, 9, 3, 7, 1, 6};
int size = sizeof(arr) / sizeof(arr[0]);
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;
}
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 = #
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; }