0% found this document useful (0 votes)
28 views

C programming

Program

Uploaded by

bboosttv
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)
28 views

C programming

Program

Uploaded by

bboosttv
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/ 29

// A PROGRAM TO CALL THE FUNCTION SWAP( ) BY

PASSING VALUES//

#include <stdio.h>

void swap(int *a, int *b) {

int C = *a;

*a = *b;

*b = C;

int main() {

int x, y;

printf("Enter two numbers:\n");

printf("x: ");

scanf("%d", &x);

printf("y: ");

scanf("%d", &y);

printf("Before swapping: x = %d, y = %d\n", x, y);

swap(&x, &y);

printf("After swapping: x = %d, y = %d\n", x, y);

return 0;

}
// A PROGRAM TO PRINT TRIANGLE OF NUMBERS//

#include <stdio.h>

int main() {

int rows;

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

scanf("%d", &rows);

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

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

printf("%d ", j);

printf("\n");

return 0;

}
// A PROGRAM TO FIND THE SURFACE AREA OF CUBE//

#include <stdio.h>

int main() {

float side, surfaceArea;

printf("Enter the length of the side of the cube: ");

scanf("%f", &side);

surfaceArea = 6 * side * side;

printf("The surface area of the cube is: %.2f\n", surfaceArea);

return 0;

}
// A PROGRAM TO FIND THE AREA OF ANY TRIANGLE//

#include <stdio.h>

int main() {

float base, height, area;

printf("Enter the base of the triangle: ");

scanf("%f", &base);

printf("Enter the height of the triangle: ");

scanf("%f", &height);

area = 0.5 * base * height;

printf("The area of the triangle is: %.2f\n", area);

return 0;

}
// A PROGRAM TO CONVERT TEMPERATURE FROM
DEGREE CENTIGRADE TO FAHRENHEIT//

#include <stdio.h>

int main() {

float celsius, fahrenheit;

printf("Enter temperature in degrees Celsius: ");

scanf("%f", &celsius);

fahrenheit = (celsius * 9 / 5) + 32;

printf("%.2f degrees Celsius is equal to %.2f degrees Fahrenheit.\n", celsius, fahrenheit);

return 0;

}
// A PROGRAM TO SWAP VALUES OF TWO VARIABLES
WITHOUT USING THIRD VARIABLE//

#include <stdio.h>

int main() {

int a, b;

printf("Enter two integers:\n");

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

printf("Before swapping: a = %d, b = %d\n", a, b);

a = a + b;

b = a - b;

a = a - b;

printf("After swapping: a = %d, b = %d\n", a, b);

return 0;

}
// A PROGRAM TO SWAP VALUES OF TWO VARIABLES
USING THIRD VARIABLE//

#include <stdio.h>

int main() {

int a, b, c;

printf("Enter two integers:\n");

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

printf("Before swapping: a = %d, b = %d\n", a, b);

c = a;

a = b;

b = c;

printf("After swapping: a = %d, b = %d\n", a, b);

return 0;

}
// A PROGRAM TO FIND SIMPLE INTEREST AND
COMPOUND INTEREST//

#include <stdio.h>

#include <math.h>

int main() {

float principal, rate, time, simpleInterest, compoundInterest, totalAmount;

printf("Enter the principal amount: ");

scanf("%f", &principal);

printf("Enter the rate of interest: ");

scanf("%f", &rate);

printf("Enter the time in years: ");

scanf("%f", &time);

simpleInterest = (principal * rate * time) / 100;

totalAmount = principal * pow((1 + rate / 100), time);

compoundInterest = totalAmount - principal;

printf("Simple Interest: %.2f\n", simpleInterest);

printf("Compound Interest: %.2f\n", compoundInterest);

return 0;

}
// A PROGRAM TO ACCEPT STUDENT ROLL NO., MARKS IN
THREE SUBJECTS AND CALCULATE TOTAL,AVERAGE AND
PRINT IT//

#include <stdio.h>

int main() {

int rollNumber;

float marks1, marks2, marks3, total, average;

printf("Enter student roll number: ");

scanf("%d", &rollNumber);

printf("Enter marks in three subjects:\n");

printf("Subject 1: ");

scanf("%f", &marks1);

printf("Subject 2: ");

scanf("%f", &marks2);

printf("Subject 3: ");

scanf("%f", &marks3);

total = marks1 + marks2 + marks3;

average = total / 3;

printf("\nRoll Number: %d\n", rollNumber);

printf("Total Marks: %.2f\n", total);

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

return 0;

}
// A PROGRAM TO FIND SUM AND AVERAGE OF THREE
REAL NUMBERS//

#include <stdio.h>

int main() {

float num1, num2, num3, sum, average;

printf("Enter three real numbers: ");

scanf("%f %f %f", &num1, &num2, &num3);

sum = num1 + num2 + num3;

average = sum / 3;

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

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

return 0;

}
// A PROGRAM TO DEMONSTRATE STUDENT STRUCTURE
TO READ & DISPLAY RECORDS OF N STUDENTS//
#include <stdio.h>

struct Student {

int id;

char name[50];

float marks;

};

int main() {

int n;

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

scanf("%d", &n);

struct Student students[n];

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

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

printf("ID: ");

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

printf("Name: ");

getchar();

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

printf("Marks: ");

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

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

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

printf("ID: %d, Name: %s, Marks: %.2f\n", students[i].id, students[i].name, students[i].marks);

return 0;

}
// A PROGRAM TO SWAP TWO NUMBERS USING
POINTERS//

#include <stdio.h>

void swap(int *a, int *b) {

int temp = *a;

*a = *b;

*b = temp;

int main() {

int num1, num2;

printf("Enter two numbers: ");

scanf("%d %d", &num1, &num2);

printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);

swap(&num1, &num2);

printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);

return 0;

}
// A PROGRAM TO READ STRING&FIND THE NUMBER OF
ALPHABETS,DIGITS,VOWELS,CONSONANTS,SPACES//
#include <stdio.h>

#include <ctype.h>

int main() {

char str[100];

int alphabets = 0, digits = 0, vowels = 0, consonants = 0, spaces = 0, specialChars = 0;

printf("Enter a string: ");

fgets(str, sizeof(str), stdin);

for (int i = 0; str[i] != '\0'; i++) {

char ch = str[i];

if (isalpha(ch)) {

alphabets++;

if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U' ||

ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {

vowels++;

} else {

consonants++; }

} else if (isdigit(ch)) {

digits++;

} else if (isspace(ch)) {

spaces++;

} else {

specialChars++; } }

printf("Alphabets: %d\n", alphabets);

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

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

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

printf("Spaces: %d\n", spaces);

printf("Special Characters: %d\n", specialChars);

return 0;}
// A PROGRAM TO READ ,DISPLAY &ADD TWO M×N
MATICES USING FUNCTIONS//
#include <stdio.h>

int main() {

int m, n;

printf("Enter number of rows (m): ");

scanf("%d", &m);

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

scanf("%d", &n);

int matrix1[m][n], matrix2[m][n], result[m][n];

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

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

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

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

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

}}

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

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

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

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

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

} }

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

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

result[i][j] = matrix1[i][j] + matrix2[i][j];

} }

printf("Resultant Matrix after Addition:\n");

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

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

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

printf("\n"); }

return 0;}
// A PROGRAM TO FIND THE FACTORIAL OF A NUMBER//

#include <stdio.h>

int main() {

int number;

unsigned long long factorial = 1;

printf("Enter a positive integer: ");

scanf("%d", &number);

if (number < 0) {

printf("Factorial is not defined for negative numbers.\n");

} else {

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

factorial *= i;

printf("Factorial of %d is: %llu\n", number, factorial);

return 0;

}
// A PROGRAM TO DEMONSTRATE STRING FUNCTIONS //
#include <stdio.h>

#include <string.h>

int main() {

char str1[100] = "Hello, ";

char str2[100] = "World!";

char str3[100];

char str4[100] = "Hello, World!";

char str5[100] = "World";

printf("Length of str1: %lu\n", strlen(str1));

strcpy(str3, str1);

printf("str3 after strcpy: %s\n", str3);

strcat(str1, str2);

printf("str1 after strcat: %s\n", str1);

int comparison = strcmp(str4, str1);

if (comparison == 0) {

printf("str4 is equal to str1\n");

} else if (comparison < 0) {

printf("str4 is less than str1\n");

} else {

printf("str4 is greater than str1\n");

char *substr = strstr(str4, str5);

if (substr) {

printf("Substring '%s' found in str4\n", str5);

} else { printf("Substring '%s' not found in str4\n", str5);

return 0; }
// A PROGRAM TO FIND THE LENGTH OF STRING
WITHOUT USING BUILT IN FUNCTION//

#include <stdio.h>

int main() {

char str[100];

int length = 0;

printf("Enter a string: ");

fgets(str, sizeof(str), stdin);

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

length++;

if (str[length - 1] == '\n') {

length--;

printf("Length of the string: %d\n", length);

return 0;

}
// A PROGRAM TO GENERATE FIBONACCI SERIES//

#include <stdio.h>

int main() {

int n, i;

unsigned long long first = 0, second = 1, next;

printf("Enter the number of terms in the Fibonacci series: ");

scanf("%d", &n);

printf("Fibonacci Series: ");

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

if (i <= 1) {

next = i;

} else {

next = first + second;

first = second;

second = next;

printf("%llu ", next);

printf("\n");

return 0;

}
// A PROGRAM TO PERFORMS ADD ITION AND
SUBTRACTION OF MATRICES//
#include <stdio.h>

void printMatrix(int matrix[MAX_SIZE][MAX_SIZE], int rows, int cols) {

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

for (int j = 0; j < cols; j++) {

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

printf("\n");

int main() {

int matrixA[MAX_SIZE][MAX_SIZE], matrixB[MAX_SIZE][MAX_SIZE];

int resultAdd[MAX_SIZE][MAX_SIZE], resultSub[MAX_SIZE][MAX_SIZE];

int rows, cols;

printf("Enter the number of rows and columns of the matrices: ");

scanf("%d %d", &rows, &cols);

printf("Enter elements of matrix A:\n");

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

for (int j = 0; j < cols; j++) {

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

printf("Enter elements of matrix B:\n");

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

for (int j = 0; j < cols; j++) {

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

} for (int i = 0; i < rows; i++) {


// A PROGRAM TO PERFORMS ADD ITION AND
SUBTRACTION OF MATRICES//
for (int j = 0; j < cols; j++) {

resultAdd[i][j] = matrixA[i][j] + matrixB[i][j];

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

for (int j = 0; j < cols; j++) {

resultSub[i][j] = matrixA[i][j] - matrixB[i][j];

printf("Result of A + B:\n");

printMatrix(resultAdd, rows, cols);

printf("Result of A - B:\n");

printMatrix(resultSub, rows, cols);

return 0;

}
// A PROGRAM TO REMOVE DUPLICATE ELEMENT IN A
SINGLE DIMENSIONAL ARRAY//

#include <stdio.h>

int main() {

int n, i, j, k;

int arr[100], unique[100];

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

scanf("%d", &n);

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

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

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

k = 0;

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

int isDuplicate = 0;

for (j = 0; j < k; j++) {

if (arr[i] == unique[j]) {

isDuplicate = 1;

break;}}

if (!isDuplicate) {

unique[k] = arr[i];

k++; }}

printf("Unique elements are:\n");

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

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

printf("\n");

return 0;

}
// A PROGRAM TO READ MARKS SCORED BY N STUDENTS
AND FIND THE AVERAGE OF MARKS//

#include <stdio.h>

int main() {

int n, i;

float marks[100], sum = 0.0, average;

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

scanf("%d", &n);

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

printf("Enter marks for student %d: ", i + 1);

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

sum += marks[i];

average = sum / n;

printf("The average marks scored by %d students is: %.2f\n", n, average);

return 0;

}
// A PROGRAM TO FIND THE ROOTS OF QUADRATIC
EQUATION//
#include <stdio.h>

#include <math.h>

int main() {

float a, b, c;

float discriminant, root1, root2, realPart, imaginaryPart;

printf("Enter coefficients a, b and c: ");

scanf("%f %f %f", &a, &b, &c);

discriminant = b * b - 4 * a * c;

if (discriminant > 0) {

root1 = (-b + sqrt(discriminant)) / (2 * a);

root2 = (-b - sqrt(discriminant)) / (2 * a);

printf("Roots are real and different.\n");

printf("Root 1 = %.2f\n", root1);

printf("Root 2 = %.2f\n", root2);

} else if (discriminant == 0) {

root1 = root2 = -b / (2 * a);

printf("Roots are real and the same.\n");

printf("Root 1 = Root 2 = %.2f\n", root1);

} else {

realPart = -b / (2 * a);

imaginaryPart = sqrt(-discriminant) / (2 * a);

printf("Roots are complex and different.\n");

printf("Root 1 = %.2f + %.2fi\n", realPart, imaginaryPart);

printf("Root 2 = %.2f - %.2fi\n", realPart, imaginaryPart);

return 0;

}
// A PROGRAM TO READ PERCENTAGE OF MARKS AND
TO DISPLAY APPROPRIATE MASSAGE//

#include <stdio.h>

int main() {

float percentage;

printf("Enter the percentage of marks: ");

scanf("%f", &percentage);

if (percentage >= 90) {

printf("Grade: A\nExcellent!\n");

} else if (percentage >= 80) {

printf("Grade: B\nVery Good!\n");

} else if (percentage >= 70) {

printf("Grade: C\nGood!\n");

} else if (percentage >= 60) {

printf("Grade: D\nSatisfactory!\n");

} else if (percentage >= 50) {

printf("Grade: E\nPass!\n");

} else if (percentage >= 0) {

printf("Grade: F\nFail!\n");

} else {

printf("Invalid percentage entered.\n");

return 0;

}
// A PROGRAM TO READ NUMBERS FROM KEYBOARD
CONTINUOUSLY TILL THE USER PRESS 999 AND FIND THE
SUM OF POSITIVE NUMBERS//

#include <stdio.h>

int main() {

int num;

int sum = 0;

printf("Enter numbers continuously (enter 999 to stop):\n");

while (1) {

scanf("%d", &num);

if (num == 999) {

break;

if (num > 0) { // Check if the number is positive

sum += num; // Add to the sum

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

return 0;

}
// A PROGRAM TO READ THE NUMBER AND FIND THE SUM
OF DIGITS , REVERSE THE NUMBER AND CHECK IT FOR
PALINDROME//

#include <stdio.h>

int main() {

int num, originalNum, reversedNum = 0, remainder, sum = 0;

printf("Enter an integer: ");

scanf("%d", &num);

originalNum = num;

while (num != 0) {

remainder = num % 10; // Get the last digit

sum += remainder; // Add to the sum

reversedNum = reversedNum * 10 + remainder;

num /= 10; }

if (originalNum == reversedNum) {

printf("%d is a palindrome.\n", originalNum);

} else {

printf("%d is not a palindrome.\n", originalNum);

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

return 0;

}
// A PROGRAM TO CHECK WHETHER THE NUMBER IS
PRIME OR NOT//

#include<stdio.h>

int main()
{
int n,i,m=0,flag=0;

printf("Enter the number to check prime:");


scanf("%d",&n);

m=n/2;
for(i=2;i<=m;i++)
{
if(n%i==0)
{
printf("Number is not prime");
flag=1;
break;
}
}
if(flag==0)
printf("Number is prime");
return 0;
}
// A PROGRAM TO READ THREE NUMBERS AND THE
BIGGEST OF THREE //

#include <stdio.h>

int main(){

int a, b, c;

printf("Enter a,b,c: ");

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

if (a > b && a > c) {

printf("a is Greater than b and c");

else if (b > a && b > c) {

printf("b is Greater than a and c");

else if (c > a && c > b) {

printf("c is Greater than a and b");

else {

printf("all are equal or any two values are equal");

return 0;

}
// A program to read radius of the circle to find area
and circumference//

#include<stdio.h>

void main()

float radius, area, cf;

printf("Enter Radius of Circle\n");

scanf("%f",&radius);

area=3.14*radius*radius;

printf("The area of Circle is %f",area);

cf=2*3.14*radius;

printf("\nThe Circumference of Circle is %f",cf);

return 0;

You might also like