KEMBAR78
Important C Programs Advanced | PDF | Queue (Abstract Data Type) | Computer Engineering
0% found this document useful (0 votes)
6 views6 pages

Important C Programs Advanced

The document contains a collection of important C programs covering various fundamental concepts such as printing 'Hello World', checking even or odd numbers, calculating factorials, and checking for prime numbers. It also includes implementations for Fibonacci series, string reversal, palindrome checking, linear search, bubble sort, file handling, recursion, stack and queue implementations, and singly linked list operations. Each program is presented with code snippets and brief descriptions of their functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

Important C Programs Advanced

The document contains a collection of important C programs covering various fundamental concepts such as printing 'Hello World', checking even or odd numbers, calculating factorials, and checking for prime numbers. It also includes implementations for Fibonacci series, string reversal, palindrome checking, linear search, bubble sort, file handling, recursion, stack and queue implementations, and singly linked list operations. Each program is presented with code snippets and brief descriptions of their functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Important C Programs (Advanced)

Hello World Program


#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
}

Check Even or Odd


#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if (num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);
return 0;
}

Factorial of a Number
#include <stdio.h>
int main() {
int n, i;
unsigned long long fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);
for(i = 1; i <= n; ++i) {
fact *= i;
}
printf("Factorial of %d = %llu", n, fact);
return 0;
}

Prime Number Check


#include <stdio.h>
int main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
for (i = 2; i <= n / 2; ++i) {
if (n % i == 0) {
flag = 1;
break;
}
}
if (n == 1)
printf("1 is neither prime nor composite.");
else {
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}
return 0;
}
Fibonacci Series
#include <stdio.h>
int main() {
int n, t1 = 0, t2 = 1, nextTerm, i;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (i = 1; i <= n; ++i) {
printf("%d, ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return 0;
}

Sum of Array Elements


#include <stdio.h>
int main() {
int n, i, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter elements: ");
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
sum += arr[i];
}
printf("Sum = %d", sum);
return 0;
}

Reverse a String
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
printf("Enter a string: ");
gets(str);
strrev(str);
printf("Reversed string: %s", str);
return 0;
}

Palindrome Check (String)


#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int i, len, flag = 0;
printf("Enter a string: ");
gets(str);
len = strlen(str);
for(i = 0; i < len/2; i++) {
if(str[i] != str[len-i-1]) {
flag = 1;
break;
}
}
if (flag == 0)
printf("String is a palindrome.");
else
printf("String is not a palindrome.");
return 0;
}

Linear Search
#include <stdio.h>
int main() {
int n, i, key;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter elements: ");
for(i = 0; i < n; i++) scanf("%d", &arr[i]);
printf("Enter element to search: ");
scanf("%d", &key);
for(i = 0; i < n; i++) {
if(arr[i] == key) {
printf("Element found at position %d", i+1);
return 0;
}
}
printf("Element not found.");
return 0;
}

Bubble Sort
#include <stdio.h>
int main() {
int n, i, j, temp;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter elements: ");
for(i = 0; i < n; i++) scanf("%d", &arr[i]);
for(i = 0; i < n-1; i++) {
for(j = 0; j < n-i-1; j++) {
if(arr[j] > arr[j+1]) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
printf("Sorted array: ");
for(i = 0; i < n; i++) printf("%d ", arr[i]);
return 0;
}

File Handling (Write & Read)


#include <stdio.h>
int main() {
FILE *fptr;
char data[100];
fptr = fopen("file.txt", "w");
if(fptr == NULL) {
printf("Error!");
return 1;
}
printf("Enter text: ");
gets(data);
fprintf(fptr, "%s", data);
fclose(fptr);

fptr = fopen("file.txt", "r");


fgets(data, 100, fptr);
printf("Data from file: %s", data);
fclose(fptr);
return 0;
}

Factorial using Recursion


#include <stdio.h>
int factorial(int n) {
if(n == 0)
return 1;
else
return n * factorial(n - 1);
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Factorial of %d is %d", num, factorial(num));
return 0;
}

Fibonacci using Recursion


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

Stack Implementation using Array


#include <stdio.h>
#define SIZE 5
int stack[SIZE], top = -1;
void push(int value) {
if(top == SIZE - 1)
printf("Stack Overflow\n");
else
stack[++top] = value;
}
void pop() {
if(top == -1)
printf("Stack Underflow\n");
else
printf("Popped: %d\n", stack[top--]);
}
void display() {
if(top == -1)
printf("Stack is empty\n");
else {
printf("Stack: ");
for(int i = 0; i <= top; i++)
printf("%d ", stack[i]);
printf("\n");
}
}
int main() {
push(10);
push(20);
push(30);
display();
pop();
display();
return 0;
}

Queue Implementation using Array


#include <stdio.h>
#define SIZE 5
int queue[SIZE], front = -1, rear = -1;
void enqueue(int value) {
if(rear == SIZE - 1)
printf("Queue Overflow\n");
else {
if(front == -1) front = 0;
queue[++rear] = value;
}
}
void dequeue() {
if(front == -1 || front > rear)
printf("Queue Underflow\n");
else
printf("Dequeued: %d\n", queue[front++]);
}
void display() {
if(front == -1 || front > rear)
printf("Queue is empty\n");
else {
printf("Queue: ");
for(int i = front; i <= rear; i++)
printf("%d ", queue[i]);
printf("\n");
}
}
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
display();
dequeue();
display();
return 0;
}

Singly Linked List (Insertion & Display)


#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* head = NULL;
void insert(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = head;
head = newNode;
}
void display() {
struct Node* temp = head;
while(temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
insert(10);
insert(20);
insert(30);
display();
return 0;
}

You might also like