0% found this document useful (0 votes)
4 views18 pages

C Program Linux

The document contains multiple C programs demonstrating various programming concepts such as finding prime numbers, population growth comparison, data type ranges, array manipulation, and string operations. It also includes programs for calculating averages, inverting bits, and counting words in a sentence. Each program is structured with input prompts, processing logic, and output displays.

Uploaded by

Shashank Patil
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)
4 views18 pages

C Program Linux

The document contains multiple C programs demonstrating various programming concepts such as finding prime numbers, population growth comparison, data type ranges, array manipulation, and string operations. It also includes programs for calculating averages, inverting bits, and counting words in a sentence. Each program is structured with input prompts, processing logic, and output displays.

Uploaded by

Shashank Patil
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/ 18

C Program: First 100 Prime Numbers

#include <stdio.h>

int main() {

int count = 0;

int num = 2;

while (count < 100) {

int isPrime = 1;

for (int i = 2; i <= num / 2; i++) {

if (num % i == 0) {

isPrime = 0;

break;

if (isPrime) {

printf("%d ", num);

count++;

num++;

return 0;

}
Find when the population of CurvedLand
exceeds FlatLand population.
#include <stdio.h>

int main() {

float curved = 50000;

float flat = 100000;

int years = 0;

while (curved <= flat) {

curved = curved * 1.03; // 3% annual growth

flat = flat * 1.01; // 1% annual growth

years++;

printf(" CurvedLand population will exceed FlatLand in %d years.\n", years);

printf("CurvedLand: %.2f\n", curved);

printf("FlatLand : %.2f\n", flat);

return 0;

}
Range of numbers that can be stored in
different data types
#include <stdio.h>

#include <limits.h>

#include <float.h>

int main() {

// Integer types

printf("char: %d to %d\n", CHAR_MIN, CHAR_MAX);

printf("unsigned char: 0 to %u\n", UCHAR_MAX);

printf("short: %d to %d\n", SHRT_MIN, SHRT_MAX);

printf("unsigned short: 0 to %u\n", USHRT_MAX);

printf("int: %d to %d\n", INT_MIN, INT_MAX);

printf("unsigned int: 0 to %u\n", UINT_MAX);

printf("long: %ld to %ld\n", LONG_MIN, LONG_MAX);

printf("unsigned long: 0 to %lu\n", ULONG_MAX);

// Floating point types

printf("float: %e to %e\n", FLT_MIN, FLT_MAX);

printf("double: %e to %e\n", DBL_MIN, DBL_MAX);

return 0;

}
Insert and delete element from array.
#include <stdio.h>

int main() {

int arr[100], n, i, pos, val;

// Step 1: Read array

printf("Enter number of elements: ");

scanf("%d", &n);

printf("Enter %d elements:\n", n);

for (i = 0; i < n; i++) {

scanf("%d", &arr[i]);

// Step 2: Insert an element

printf("Enter position to insert (0-based index): ");

scanf("%d", &pos);

printf("Enter value to insert: ");

scanf("%d", &val);

if (pos < 0 || pos > n) {

printf(" Invalid insert position!\n");

} else {

for (i = n; i > pos; i--) {

arr[i] = arr[i - 1];

}
arr[pos] = val;

n++;

printf(" Value inserted.\n");

// Step 3: Delete an element

printf("Enter position to delete (0-based index): ");

scanf("%d", &pos);

if (pos < 0 || pos >= n) {

printf(" Invalid delete position!\n");

} else {

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

arr[i] = arr[i + 1];

n--;

printf(" Value deleted.\n");

// Step 4: Display final array

printf("Final array:\n");

for (i = 0; i < n; i++) {

printf("%d ", arr[i]);

return 0;

}
Find the hexadecimal equivalent, factorial or
reverse digits of a number provided on
command line.
#include <stdio.h>

#include <stdlib.h> // for atoi() - not needed here but kept for completeness

int factorial(int n) {

int fact = 1;

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

fact *= i;

return fact;

int reverseDigits(int n) {

int rev = 0;

while (n > 0) {

rev = rev * 10 + (n % 10);

n /= 10;

return rev;

int main() {

int num;

printf("Enter a number: ");


if (scanf("%d", &num) != 1) {

printf(" Invalid input. Please enter a valid integer.\n");

return 1;

printf("You entered: %d\n", num);

printf(" Hexadecimal: %X\n", num);

if (num >= 0 && num <= 12) {

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

} else {

printf(" Factorial too large to compute safely (max 12).\n");

printf(" Reversed digits: %d\n", reverseDigits(num));

return 0;

}
Word count
1) Without pointers (using array indexing)

CopyEdit

#include <stdio.h>

int wordCount(char str[]) {

int count = 0;

int i = 0;

int inWord = 0;

while (str[i] != '\0') {

// Check for space (only ' ' considered here)

if (str[i] == ' ' || str[i] == '\n' || str[i] == '\t') {

inWord = 0;

} else if (inWord == 0) {

inWord = 1;

count++;

i++;

return count;

int main() {

char input[1000];
printf("Enter a sentence:\n");

fgets(input, sizeof(input), stdin);

int count = wordCount(input);

printf("Word count: %d\n", count);

return 0;

2) With pointers (using pointer arithmetic)

CopyEdit

#include <stdio.h>

int wordCount(char *str) {

int count = 0;

int inWord = 0;

while (*str != '\0') {

if (*str == ' ' || *str == '\n' || *str == '\t') {

inWord = 0;

} else if (inWord == 0) {

inWord = 1;

count++;

str++;

return count;
}

int main() {

char input[1000];

printf("Enter a sentence:\n");

fgets(input, sizeof(input), stdin);

int count = wordCount(input);

printf("Word count: %d\n", count);

return 0;

Line fold
#include <stdio.h>

void lineFold(char *str, int width) {

int count = 0;

while (*str != '\0') {

if (*str == '\n') {

// Print newline and reset count

printf("\n");

count = 0;

} else {
printf("%c", *str);

count++;

if (count == width) {

printf("\n");

count = 0;

str++;

int main() {

char input[1000];

int foldWidth = 40; // desired max line length

printf("Enter a paragraph:\n");

fgets(input, sizeof(input), stdin);

printf("\nFolded text:\n");

lineFold(input, foldWidth);

return 0;

}
Sorting names
#include <stdio.h>

#include <string.h>

int main() {

int n;

printf("Enter number of names: ");

scanf("%d", &n);

getchar(); // consume newline after number input

char names[100][100]; // Up to 100 names, each up to 99 characters

char temp[100];

// Input names

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

printf("Enter name %d: ", i + 1);

fgets(names[i], sizeof(names[i]), stdin);

// Remove trailing newline if present

size_t len = strlen(names[i]);

if (names[i][len - 1] == '\n') {

names[i][len - 1] = '\0';

// Sort using bubble sort

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


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

if (strcmp(names[i], names[j]) > 0) {

strcpy(temp, names[i]);

strcpy(names[i], names[j]);

strcpy(names[j], temp);

// Output sorted names

printf("\nSorted names:\n");

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

printf("%s\n", names[i]);

return 0;

Concatenate strings
#include <stdio.h>

void concat(char *s1, char *s2) {

int i = 0;

// Move to the end of str1

while (s1[i] != '\0')

i++;
// Copy characters from str2 to str1

int j = 0;

while (s2[j] != '\0') {

s1[i] = s2[j];

i++;

j++;

// Null-terminate the concatenated string

s1[i] = '\0';

int main() {

char s1[50] = "Hello ";

char s2[] = "Geeks";

concat(s1, s2);

printf("%s", s1);

return 0;

}
Calculate the average of Maths and Science
marks of a class of students.
#include <stdio.h>

int main() {

int n;

printf("Enter number of students: ");

scanf("%d", &n);

float maths[n], science[n];

float totalMaths = 0, totalScience = 0;

// Input marks

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

printf("\nStudent %d:\n", i + 1);

printf("Enter Maths marks: ");

scanf("%f", &maths[i]);

printf("Enter Science marks: ");

scanf("%f", &science[i]);

totalMaths += maths[i];

totalScience += science[i];

// Print individual averages


printf("\n--- Individual Averages ---\n");

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

float avg = (maths[i] + science[i]) / 2;

printf("Student %d Average: %.2f\n", i + 1, avg);

// Class averages

float classAvgMaths = totalMaths / n;

float classAvgScience = totalScience / n;

float overallClassAvg = (classAvgMaths + classAvgScience) / 2;

printf("\n--- Class Averages ---\n");

printf("Maths Average: %.2f\n", classAvgMaths);

printf("Science Average: %.2f\n", classAvgScience);

printf("Overall Class Average: %.2f\n", overallClassAvg);

return 0;

}
Invert n bits from position p in number x
#include <stdio.h>

int invertBits(int x, int p, int n) {

int mask = ((1 << n) - 1) << (p - n + 1);

return x ^ mask;

int main() {

int x, p, n;

printf("Enter number (x): ");

scanf("%d", &x);

printf("Enter position (p): ");

scanf("%d", &p);

printf("Enter number of bits (n): ");

scanf("%d", &n);

int result = invertBits(x, p, n);

printf("Result after inverting %d bits from position %d in %d is: %d\n", n, p, x, result);

return 0;

You might also like