Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
0 views

C PROGRAMS

The document contains multiple C programming examples demonstrating various concepts such as arithmetic operations, conditional statements, loops, functions, structures, and unions. Each example includes code snippets for tasks like calculating sums and averages, determining grades, generating Fibonacci series, and handling strings. The examples are structured to illustrate basic programming techniques and data handling in C.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

C PROGRAMS

The document contains multiple C programming examples demonstrating various concepts such as arithmetic operations, conditional statements, loops, functions, structures, and unions. Each example includes code snippets for tasks like calculating sums and averages, determining grades, generating Fibonacci series, and handling strings. The examples are structured to illustrate basic programming techniques and data handling in C.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 60

EX1,1

#include <stdio.h>

int main()

int num1,num2,num3,num4,num5,num6;

float sum,average;

printf("Enter six numbers:");

scanf("%d%d%d%d%d%d",&num1,&num2,&num3,&num4,&num5,&num6);

sum= (num1+num2+num3+num4+num5+num6);

average= (sum /6.0);

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

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

return 0;

}
EX1,2

#include<stdio.h>

int main()

float p, n, r, si;

printf("enter p,n,r values: ");

scanf("%f%f%f",&p,&n,&r);

si=((p*n*r)/100.0);

printf("%f\n",si);

return 0;

}
EX1,3

#include <stdio.h>

int main ()

float ricePricePerKg = 16.75, sugarPricePerKg = 15.0;

float riceQuantity, sugarQuantity, totalRicePrice, totalSugarPrice,totalPrice;

printf("Enter quantity of rice in kg:");

scanf("%f", &riceQuantity);

printf("Enter quantity of sugar in kg:");

scanf("%f", &sugarQuantity);

totalRicePrice = ricePricePerKg * riceQuantity;

totalSugarPrice = sugarPricePerKg * sugarQuantity;

totalPrice = totalRicePrice +totalSugarPrice;

printf("TOTAL PRICE FOR RICE: Rs.%.2f\n",totalRicePrice);

printf("Total price for sugar : Rs.%2f\n",totalSugarPrice);

printf("Overall total price: Rs.%2f\n", totalPrice);

return 0;

}
EX2,1

#include<stdio.h>

int main()

float basicPay, da, hra, specialPay, loan;

printf("Enter the basic pay:");

scanf("%f", &basicPay);

if (basicPay <10000)

da = 0.25 * basicPay;

hra = 0.15 * basicPay;

specialPay = 0.05 * basicPay;

loan = 500;

else if (basicPay >= 10000 && basicPay <=50000)

da = 0.35 * basicPay;

hra = 0.20 * basicPay;

specialPay = 0.10 * basicPay;

loan = 1000;

else

da = 0.50 * basicPay;

hra = 0.30 * basicPay;

specialPay = 0.20 * basicPay;

loan = 1500;

float totalSalary;

totalSalary = basicPay + da + hra +specialPay - loan;


printf("Basic Pay:%f\n",basicPay);

printf("HRA :%f\n",hra);

printf("special pay:%f\n",specialPay);

printf("Total Salary:%f\n",totalSalary);

return 0;

}
EX2,2

#include<stdio.h>

int main()

char ch;

printf("Enter a Character:");

scanf("%c", &ch);

if ((ch >= 'a' && ch <= 'z'))

printf("The character is lowercase \n");

else if ( ch >= 'A' && ch <= 'Z')

printf("The Character is uppercase \n");

else if (ch >= '0' && ch <= '9')

printf("The Character is digit \n");

else

printf("The character is special symbol \n");

return 0;

}
EX2,3

#include<stdio.h>

int main()

float hardness, carbonContent, tensileStrength;

printf("Enter hardness:");

scanf("%f", &hardness);

printf("Enter carbonContent:");

scanf("%f", &carbonContent);

printf("Enter tensile strength:");

scanf("%f", &tensileStrength);

if (hardness> 50 && carbonContent <0.7 && tensileStrength >5600)

printf("Grade A \n");

else if (hardness> 50 && carbonContent <0.7)

printf("Grade B \n");

else if (carbonContent <0.7 && tensileStrength >5600)

printf("Grade C \n");

else

printf("Grade D \n");

return 0;

}
EX3,1

#include <stdio.h>

int main() {

int i,n;

int t1=0,t2=1;

int nextTerm = t1 +t2;

printf("Enter the number of terms:");

scanf("%d", &n);

printf("Fibonacci series: %d, %d, ", t1, t2);

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

printf("%d,",nextTerm);

t1 = t2;

t2 = nextTerm;

nextTerm = t1+t2;

return 0 ;

}
EX3,1

#include <stdio.h>

#include <math.h>

int main()

int i, sum, num, count = 0;

printf("Amstrong number between 1 to 1000:\n");

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

num = i;

while (num!=0) {

num/=10;

count++;

num = i;

sum= pow(num%10, count)

+ pow((num % 100 - num % 10) /10, count)

+ pow((num % 1000 - num % 100) / 100, count);

if (sum==i) {

printf("%d",i);

count = 0;

return 0;

}
EX3,2

#include <stdio.h>

#include <conio.h>

int main()

int i, count=0, j;

printf("Prime numbers between 1 to 50 are:\n");

for(i=1; i<=50; i++)

for (j=2; j<i; j++)

if (i%j==0)

count++;

break;

if (count==0 && i!=1)

printf("%d\n",i);

count=0;

getch();

return 0;

}
EX3,3

#include <stdio.h>

int main() {

int n;

double sum = 0.0;

printf("Enter the number of terms: ");

scanf("%d", &n);

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

sum += 1.0 / (i * i * i);

printf("Sum of the series up to %d terms: %2f\n", n, sum);

return 0;

}
EX4,1

#include <stdio.h>

int main()

int rows = 6;

int mid = (rows+1)/2;

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

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

printf("*");

} printf("\n");

for (int i= mid-1; i >=1; i--)

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

print("*");

printf("\n");

return 0;

}
EX4,1

#include <stdio.h>

int main()

int rows = 6;

int mid = (rows+1)/2;

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

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

printf("*");

} printf("\n");

for (int i= mid-1; i >=1; i--)

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

print("*");

printf("\n");

return 0;

}
EX4,2

#include<stdio.h>

swap(int *, int *);

int main()

int a, b;

printf("Enter value of a & b:");

scanf("%d %d", &a, &b);

printf("\nBefore Swapping:\n");

printf("\na = %d\n\nb = %d\n", a, b);

swap(&a, &b);

printf("\nAfter swapping: \n");

printf("\na = %d\n\nb= %d",a, b);

return 0;

swap (int *x, int *y)

int temp;

temp = *x;

*x = *y;

*y = temp;

}
EX5,1

#include<stdio.h>

const int r=3,c=3;

int i,j;

void transpose(int m1[3][3], int m3[3][3])

puts("transpose : \n");

for( i =0;i<r;i++)

for( j=0;j<c;j++)

m3[i][j]=m1[j][i];

for( i =0;i<r;i++)

for(j=0;j<c;j++)

printf("%d ",m3[i][j]);

printf("\n");

puts("\n");

void sum(int m1[3][3], int m2[3][3], int m3[3][3])

{
puts("sum : \n");

for( i =0;i<r;i++)

for( j=0;j<c;j++)

m3[i][j]=m1[i][j] + m2[i][j];

for( i =0;i<r;i++)

for( j=0;j<c;j++)

printf("%d ",m3[i][j]);

printf("\n");

puts("\n");

void product(int m1[3][3], int m2[3][3], int m3[3][3])

puts("product : \n");

for( i =0;i<r;i++)

for( j=0;j<c;j++)

m3[i][j]=m1[i][j] * m2[i][j];
}

for( i =0;i<r;i++)

for( j=0;j<c;j++)

printf("%d ",m3[i][j]);

printf("\n");

puts("\n");

int main()

int m1[3][3]={{10,20,30},{40,50,60},{70,80,90}};

int m2[3][3]={{1,1,1},{2,2,2},{3,3,3}};

int m3[3][3];

sum(m1,m2,m3);

transpose(m1,m3);

product(m1,m2,m3);

return 0;

}
EX5,3

#include<stdio.h>

#include<stdbool.h>

bool is_palindrome(char str[], int len)

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

if(str[i] != str[len-1-i]){

return false;

return true;

int main() {

char str[] = "radar";

int len = 5;

if(is_palindrome(str, len)) {

printf("The string is a palindrome.\n");

} else {

printf("The string is not a palindrome.\n");

return 0;

}
EX6,1

#include <stdio.h>

struct population

int men;

int women;

};

struct state

char name[40];

struct population population_data;

};

int main()

struct state s;

printf("Enter state name: ");

scanf("%s", s.name);

printf("Enter population data:\n");

printf("Men: ");

scanf("%d", &s.population_data.men);

printf("Women: ");

scanf("%d", &s.population_data.women);

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

printf("Population Data:\n");

printf("Men: %d\n", s.population_data.men);


printf("Women: %d\n", s.population_data.women);

return 0;

}
EX6,2

#include <stdio.h>

struct book

char name[50];

char author[40];

int pages;

float cost;

};

int main()

struct book b;

struct book *bptr = &b;

printf("Enter the title of the book: ");

scanf("%s", bptr->name);

printf("Enter the name of the author: ");

scanf("%s", bptr->author);

printf("Enter the number of pages: ");

scanf("%d", &bptr->pages);

printf("Enter the cost: ");

scanf("%f", &bptr->cost);

printf("Book Details:\n");

printf("Title: %s\n", bptr->name);

printf("Author: %s\n", bptr->author);


printf("Number of Pages: %d\n", bptr->pages);

printf("Cost: %.2f\n", bptr->cost);

return 0;

}
EX6,3

#include <stdio.h>

struct student

int rno, tot;

int m[5];

float avg;

char name[40];

char grade[30];

} st[3];

int main()

int count =3;

int i, j;

for(i=0; i<count; i++)

printf("Enter student %d details\n",i+1);

printf("Enter name: ");

scanf("%s", st[i].name);

for(j=0; j <5; j++)

printf("Enter marks: ");

scanf("%d", &st[i].m[j]);

st[i].tot+st[i].m[j];

}
st[i].avg=st[i].tot/5;

if (st[i].avg>=75)

st[i].grade[0] = 'A';

else if (st[i].avg>=50 && st[i].avg <75)

st[i].grade[0] ='B';

else

{ st[i].grade[0]="C";

for (i=0;i<count; i++)

printf("\nName: %s\n", st[i].name);

printf("Marks: ");

for (j=0; j < 5; j++)

printf("%d\n", st[i].m[j]);
}

printf("Total: %d\n", st[i].tot);

printf("Average: %f\n", st[i].avg);

printf("Class: %c\n", st[i].grade[0]);

return 0;

}
EX6,4

#include <stdio.h>

#include <string.h>

union student

char name[50];

int marks;

char grade;

char address[100];

};

int main()

union student st;

printf("Enter student name: ");

scanf("%s", st.name);

printf("Enter student marks: ");

scanf("%d", &st.marks);

printf("Enter student grade: ");

scanf(" %c", &st.grade); // Note the space before %c to consume any newline character

printf("Enter student address: ");

scanf("%s", st.address);

printf("\nStudent Information:\n");

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

printf("Marks: %d\n", st.marks);


printf("Grade: %c\n", st.grade);

printf("Address: %s\n", st.address);

return 0;

}
EX7,1

#include <stdio.h>

#include <string.h>

int main()

char s[30];

printf("Enter the string: ");

gets(s);

char actual_str[30];

strcpy(actual_str, s);

strrev(s);

if (strcmpi(s, actual_str)==0)

printf("PALINDROME");

else

printf("NOT A PALNDROME");

return 0;

}
EX7,2

#include<stdio.h>

#include <string.h>

void display_names(int n, char names[][30])

int i=0;

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

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

void perform_sort(int n, char names[][30])

int i, j;

char temp[30];

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

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

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

{ strcpy(temp, names[j]);

strcpy(names[j], names[j+1]);

strcpy(names[j+1], temp);

int main()

char names[][30]={"John", "Alice", "Bob", "Charlie"};

int n = sizeof(names)/ sizeof(names[0]);

display_names(n, names);

perform_sort(n, names);

printf("\n");

display_names(n, names);

return 0;

}
EX7,3

#include <stdio.h>

#include <string.h>

int main()

char ch;

char s[30];

int i, count = 0;

printf("Enter input string: ");

gets(s);

printf("Enter the character to count: ");

ch = getchar();

for (i = 0; s[i] != '\0'; i++)

if (ch == s[i])

count++;

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

return 0;

}
EX8,1

#include <stdio.h>

void add(int *a, int *b, int *addition)

*addition = *a + *b;

void sub(int a, int b, int *substraction)

*substraction = a - b;

void mul(int *a, int *b, int *multiplication)

*multiplication = (*a) * (*b);

void div(int a, int *b, int *division)

if (*b != 0)

*division = a / (*b);

else

printf("Error: Division by zero!\n");

}
int main()

int a, b, result;

printf("Enter a: ");

scanf("%d", &a);

printf("Enter b: ");

scanf("%d", &b);

add(&a, &b, &result);

printf("Addition: %d\n", result);

sub(a, b, &result);

printf("Subtraction: %d\n", result);

mul(&a, &b, &result);

printf("Multiplication: %d\n", result);

div(a, &b, &result);

printf("Division: %d\n", result);

return 0;

}
EX8,2

#include <stdio.h>

#define MAX_SIZE 100

void print_array(int *arr, int n)

int i;

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

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

int main()

int src_arr[] = {10, -1, 100, 90, 87, 0, 15, 10, 20, 30};

int dest_arr[MAX_SIZE];

int *src_ptr = src_arr;

int *dest_ptr = dest_arr;

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

int *end_ptr = &src_arr[size - 1];

printf("Printing source array:\n");

print_array(src_arr, size);

while (src_ptr <= end_ptr)

*dest_ptr = *src_ptr;

src_ptr++;

dest_ptr++;
}

printf("\nPrinting destination array:\n");

print_array(dest_arr, size);

return 0;

}
EX8,3

#include <stdio.h>

#include <ctype.h>

int main()

int space_count = 0, vowels_count = 0, consonant_count = 0, digit_count = 0, special_count = 0;

int i;

char s[100], ch;

char *char_ptr;

printf("Enter string: ");

fgets(s, sizeof(s), stdin);

char_ptr = &s[0];

for (i = 0; *(char_ptr + i); i++)

ch = *(char_ptr + i);

if (ch >= 'a' && ch <= 'z')

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')

vowels_count++;

else

consonant_count++;

else if (ch >= '0' && ch <= '9')

digit_count++;

else if (ch == ' ')

{
space_count++;

else

special_count++;

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

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

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

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

printf("Special characters count: %d\n", special_count);

return 0;

}
EX9,1

#include <stdio.h>

int fact(int n);

int fact(int n)

if (n == 1)

return 1;

return n * fact(n - 1);

int main()

int n, res;

printf("Enter a number: ");

scanf("%d", &n);

res = fact(n);

printf("Factorial of %d: %d\n", n, res);

return 0;

}
EX9,2

#include <stdio.h>

int sum_nature(int n, int s);

int sum_nature(int n, int s)

if (n <= 0)

return s;

return sum_nature(n - 1, s + n);

int main()

int n, sum;

printf("Enter n: ");

scanf("%d", &n);

sum = sum_nature(n, 0);

printf("Sum: %d\n", sum);

return 0;

}
EX9,3

#include <stdio.h>

int generate_fibonacci_series(int c, int a, int b, int start, int end);

int generate_fibonacci_series(int c, int a, int b, int start, int end)

if (a >= start && a < end)

printf("%d ", a);

if (a > end)

return 0;

c = a + b;

generate_fibonacci_series(c, b, c, start, end);

int main()

int start, end;

printf("Enter start value: ");

scanf("%d", &start);

printf("Enter end value: ");

scanf("%d", &end);

generate_fibonacci_series(1, 0, 1, start, end);


return 0;

}
EX10,1

#include <stdio.h>

#include <stdlib.h>

int main(int argc, const char *argv[])

float a, b, res;

char op;

if (argc != 4)

printf("Incorrect usage\n");

return 0;

a = atof(argv[1]);

b = atof(argv[2]);

op = argv[3][0];

switch (op)

case '+':

res = a + b;

break;

case '-':

res = a - b;

break;

case '*':

res = a * b;

break;

case '/':
if (b != 0)

res = a / b;

else

printf("Error: Division by zero\n");

break;

default:

printf("Unknown operator\n");

break;

printf("Result = %f\n", res);

return 0;

}
EX10,2

#include <stdio.h>

#include <stdlib.h>

typedef struct {

float x;

float y;

} Values;

int main(int argc, char *argv[])

if (argc != 3)

printf("Incorrect usage!\n");

return 0;

Values myValues;

myValues.x = atof(argv[1]);

myValues.y = atof(argv[2]);

printf("Value 1: %.2f\n", myValues.x);

printf("Value 2: %.2f\n", myValues.y);

return 0;

}
EX10,3

#include <stdio.h>

int main(int argc, char *argv[])

char ch;

FILE *fptr;

if (argc != 2)

printf("Incorrect usage\n");

return 0;

fptr = fopen(argv[1], "r");

if (fptr == NULL)

printf("Cannot access file %s\n", argv[1]);

return 1;

printf("Contents of %s file:\n\n", argv[1]);

while ((ch = fgetc(fptr)) != EOF)

putchar(ch);

fclose(fptr);

return 0;
}
EX11,1

#include <stdio.h>

#include <stdlib.h>

typedef struct {

float x;

float y;

} Values;

int main(int argc, char *argv[])

if (argc != 3)

printf("Incorrect usage!\n");

return 0;

Values myValues;

myValues.x = atof(argv[1]);

myValues.y = atof(argv[2]);

printf("Value 1: %.2f\n", myValues.x);

printf("Value 2: %.2f\n", myValues.y);

return 0;

}
EX11,2

#include <stdio.h>

#define MAX_EMPLOYEES 100

typedef struct {

char name[100];

int id;

char position[100];

double salary;

} Employee;

int main()

Employee emps[MAX_EMPLOYEES];

int n, i;

char fname[] = "emp_details.txt";

printf("Enter the number of employees: ");

scanf("%d", &n);

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

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

printf("Name: ");

getchar(); // Clear newline from previous input

fgets(emps[i].name, sizeof(emps[i].name), stdin);

printf("ID: ");

scanf("%d", &emps[i].id);
printf("Position: ");

getchar(); // Clear newline from previous input

fgets(emps[i].position, sizeof(emps[i].position), stdin);

printf("Salary: ");

scanf("%lf", &emps[i].salary);

printf("\n");

FILE *fp = fopen(fname, "w");

if (fp == NULL)

printf("Error opening file!\n");

return 1;

fprintf(fp, "EMPLOYEE DETAILS:\n");

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

fprintf(fp, "Name: %s", emps[i].name);

fprintf(fp, "ID: %d\n", emps[i].id);

fprintf(fp, "Position: %s", emps[i].position);

fprintf(fp, "Salary: %.2f\n\n", emps[i].salary);

fclose(fp);

printf("\nEmployee details have been stored in %s\n", fname);

return 0;}
EX11,3

#include <stdio.h>

#define MAX_STUDENT 100

typedef struct {

char name[100];

int rollno;

int age;

int marks;

} Student;

int main()

Student sts[MAX_STUDENT];

int n, i;

char fname[] = "student_details.txt";

printf("Enter the number of students: ");

scanf("%d", &n);

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

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

printf("Name: ");

getchar(); // Clear newline from previous input

fgets(sts[i].name, sizeof(sts[i].name), stdin);

printf("Roll Number: ");

scanf("%d", &sts[i].rollno);
printf("Age: ");

scanf("%d", &sts[i].age);

printf("Marks: ");

scanf("%d", &sts[i].marks);

printf("\n");

FILE *fp = fopen(fname, "w");

if (fp == NULL)

printf("Error opening file!\n");

return 1;

fprintf(fp, "STUDENT DETAILS:\n");

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

fprintf(fp, "Name: %s", sts[i].name);

fprintf(fp, "Roll Number: %d\n", sts[i].rollno);

fprintf(fp, "Age: %d\n", sts[i].age);

fprintf(fp, "Marks: %d\n\n", sts[i].marks);

fclose(fp);

printf("\nStudent details have been stored in %s\n", fname);

return 0;

}
EX12,1

#include <stdio.h>

#include <stdlib.h>

int find_largest_element(int array[], int size)

int i, max = array[0];

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

if (array[i] > max)

max = array[i];

return max;

int main()

int *array;

int size, i, large_element;

printf("Enter the size of the array: ");

scanf("%d", &size);

array = (int*) malloc(size * sizeof(int));

printf("Enter the elements of the array:\n");

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

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

large_element = find_largest_element(array, size);

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

free(array);

return 0;

}
EX12,2

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node *next;

};

int main() {

struct Node *head = NULL;

struct Node *current = NULL;

int i, data, numNodes;

printf("Enter the number of nodes: ");

scanf("%d", &numNodes);

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

printf("Enter data for node %d: ", i + 1);

scanf("%d", &data);

struct Node *newNode = (struct Node*) malloc(sizeof(struct Node));

if (newNode == NULL) {

printf("Memory allocation failed!\n");

exit(1);

newNode->data = data;

newNode->next = NULL;

if (head == NULL) {
head = newNode;

current = newNode;

} else {

current->next = newNode;

current = newNode;

printf("Linked list: ");

current = head;

while (current != NULL) {

printf("%d ", current->data);

current = current->next;

current = head;

while (current != NULL) {

struct Node* temp = current;

current = current->next;

free(temp);

return 0;

}
EX12,3

#include <stdio.h>

#include <stdlib.h>

struct student {

char name[50];

int age;

float gpa;

};

int main() {

int n, i;

printf("Enter the number of students: ");

scanf("%d", &n);

struct student *students = (struct student*) malloc(n * sizeof(struct student));

if (students == NULL) {

printf("Memory allocation failed!\n");

exit(1);

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

printf("Enter details for student %d:\n", i + 1);

printf("Name: ");

scanf("%s", students[i].name);

printf("Age: ");

scanf("%d", &students[i].age);

printf("GPA: ");

scanf("%f", &students[i].gpa);

}
printf("Student details:\n");

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

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

printf("Name: %s\n", students[i].name);

printf("Age: %d\n", students[i].age);

printf("GPA: %.2f\n\n", students[i].gpa);

free(students);

return 0;

}
EX 5,2

#include<stdio.h>

int main()

int arr[]={164,34,25,12,22,11,90};

int n=7;

int temp;

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

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

if(arr[j]>arr[j+1]) {

int temp =arr[j];

arr[j] = arr[j+1];

arr[j+1] = temp;

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

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

return 0;

You might also like