0% found this document useful (0 votes)
10 views8 pages

Codes c1? ?

The document contains a series of programming tasks and examples in C, including basic operations like printing, arithmetic calculations, and control structures. It also covers advanced topics such as recursion, searching algorithms (linear and binary), sorting (bubble sort), and file handling. Additionally, it discusses data structures like arrays, structures, and unions.

Uploaded by

itssaku21
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)
10 views8 pages

Codes c1? ?

The document contains a series of programming tasks and examples in C, including basic operations like printing, arithmetic calculations, and control structures. It also covers advanced topics such as recursion, searching algorithms (linear and binary), sorting (bubble sort), and file handling. Additionally, it discusses data structures like arrays, structures, and unions.

Uploaded by

itssaku21
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/ 8

1.

WAP to simply print “Hello World”


2. WAP to find the Sum of two numbers.
3. WAP to calculate the Simple interest and compound interest.
4. WAP to check whether a number entered by user is even or Odd. (if-else)
5. WAP to find the largest of three numbers. (if-else)
6. WAP to compute student grade using if-else ladder.
7. WAP to build a simple calculator using Switch case.
8. WAP using For loop (print 1 to 100 counting).
9. WAP to find the Sum of first 10 natural numbers using for loop.
10. WAP to print the table of a given number.
11. WAP using Do while loop.
12. WAP using While loop.
13. Write programs for Patterns.
14. WAP to find the Factorial of a number using loop.
15. WAP to print Fibonacci Series up to n terms entered by user.
16. WAP to check whether the number entered by user is prime or not.
17. WAP to check whether given year is leap year or not.
18. WAP to find the reverse of a number using while loop.
19. WAP to find whether a given number is palindrome or not.
20. WAP to find whether a given number is Armstrong number or not.
21. WAP for swapping of two numbers using temporary variable.
22. WAP for swapping of two numbers without using temporary variable.
23. Some programs using Functions
a) Simple program to see the use of function.
b) Simple Program using multiple parameter passing.
c) Calculate the sum of numbers using function parameter.
24. Call by value and call by reference
25. WAP to understand the use of Pointers.
26. Factorial using Recursion
27. Fibonacci using Recursion
28. WAP for linear search.
29. WAP for Binary Search
30. Some programs of Array
31. Matrix Multiplication
32. WAP for Bubble Sort
33. WAP to calculate the string length without using inbuilt function.
34. Structure and Union
35. File handling
WAP for Linear Search

#include <stdio.h>

int linearSearch(int arr[], int n, int target) {


int i;
for (i = 0; i< n; i++) {
if (arr[i] == target) {
return i; // Element found at index i
}
}
return -1; // Element not found
}

int main() {
int arr[] = {10, 2, 8, 5, 17};
int n = sizeof(arr) / sizeof(arr[0]);
int target = 8;
int result = linearSearch(arr, n, target);
if (result == -1) {
printf("Element not found in the array.\n");
} else {
printf("Element found at index: %d\n", result);
}
return 0;
}

WAP for Binary Search

#include <stdio.h>

int binarySearch(int arr[], int size, int targetVal);

int main() {
int myArray[] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19};
int myTarget = 15;
int size = sizeof(myArray) / sizeof(myArray[0]);

int result = binarySearch(myArray, size, myTarget);

if (result != -1) {
printf("Value %d found at index %d\n", myTarget, result);
} else {
printf("Target not found in array.\n");
}

return 0;
}

int binarySearch(int arr[], int size, int targetVal) {


int left = 0;
int right = size - 1;

while (left <= right) {


int mid = (left + right) / 2;

if (arr[mid] == targetVal) {
return mid;
}

if (arr[mid] < targetVal) {


left = mid + 1;
} else {
right = mid - 1;
}
}

return -1;
}

WAP to understand the differences between structure and Union

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

// declaring structure
struct struct_example {
int integer;
float decimal;
char name[20];
};

// declaring union

union union_example {
int integer;
float decimal;
char name[20];
};

void main()
{
// creating variable for structure
// and initializing values difference
// six
struct struct_example s = { 18, 38, "geeksforgeeks" };

// creating variable for union


// and initializing values
union union_example u = { 18, 38, "geeksforgeeks" };

printf("structure data:\n integer: %d\n"


"decimal: %.2f\n name: %s\n",
s.integer, s.decimal, s.name);
printf("\nunion data:\n integer: %d\n"
"decimal: %.2f\n name: %s\n",
u.integer, u.decimal, u.name);

// difference two and three


printf("\nsizeof structure : %d\n", sizeof(s));
printf("sizeof union : %d\n", sizeof(u));

// difference five
printf("\n Accessing all members at a time:");
s.integer = 183;
s.decimal = 90;
strcpy(s.name, "geeksforgeeks");

printf("structure data:\n integer: %d\n "


"decimal: %.2f\n name: %s\n",
s.integer, s.decimal, s.name);

u.integer = 183;
u.decimal = 90;
strcpy(u.name, "geeksforgeeks");

printf("\nunion data:\n integer: %d\n "


"decimal: %.2f\n name: %s\n",
u.integer, u.decimal, u.name);

printf("\n Accessing one member at time:");

printf("\nstructure data:");
s.integer = 240;
printf("\ninteger: %d", s.integer);
s.decimal = 120;
printf("\ndecimal: %f", s.decimal);

strcpy(s.name, "C programming");


printf("\nname: %s\n", s.name);

printf("\n union data:");


u.integer = 240;
printf("\ninteger: %d", u.integer);

u.decimal = 120;
printf("\ndecimal: %f", u.decimal);

strcpy(u.name, "C programming");


printf("\nname: %s\n", u.name);

// difference four
printf("\nAltering a member value:\n");
s.integer = 1218;
printf("structure data:\n integer: %d\n "
" decimal: %.2f\n name: %s\n",
s.integer, s.decimal, s.name);

u.integer = 1218;
printf("union data:\n integer: %d\n"
" decimal: %.2f\n name: %s\n",
u.integer, u.decimal, u.name);
}

WAP to calculate the string length without using inbuilt function.

#include <stdio.h>

// Function to calculate the length of a string


int string_length(char* str)
{
int len = 0;

// Iterate through the string until the null terminator


// is reached
while (*str != '\0') {
len++;
str++;
}
return len;
}

int main()
{
// Declare and initialize a character array
char str[] = "Hello, world!";
int length;

// Calculate the length of the string using the function


length = string_length(str);

// Print the length of the string


printf("The length of the string is: %d", length);

return 0;
}

WAP for Bubble Sort

#include <stdio.h>

void swap(int* arr, int i, int j) {


int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

void bubbleSort(int arr[], int n) {


for (int i = 0; i < n - 1; i++) {

// Last i elements are already in place, so the loop


// will only num n - i - 1 times
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1])
swap(arr, j, j + 1);
}
}
}

int main() {
int arr[] = { 6, 0, 3, 5 };
int n = sizeof(arr) / sizeof(arr[0]);

// Calling bubble sort on array arr


bubbleSort(arr, n);
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);

return 0;
}

WAP to Open a File, Write in it, And Close the File // File Handling

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

int main()
{

// Declare the file pointer


FILE* filePointer;

// Get the data to be written in file


char dataToBeWritten[50] = "GeeksforGeeks-A Computer "
"Science Portal for Geeks";

// Open the existing file GfgTest.c using fopen()


// in write mode using "w" attribute
filePointer = fopen("GfgTest.c", "w");

// Check if this filePointer is null


// which maybe if the file does not exist
if (filePointer == NULL) {
printf("GfgTest.c file failed to open.");
}
else {

printf("The file is now opened.\n");

// Write the dataToBeWritten into the file


if (strlen(dataToBeWritten) > 0) {

// writing in the file using fputs()


fputs(dataToBeWritten, filePointer);
fputs("\n", filePointer);
}

// Closing the file using fclose()


fclose(filePointer);
printf("Data successfully written in file "
"GfgTest.c\n");
printf("The file is now closed.");
}

return 0;
}

You might also like