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

Harshit Pandey

Uploaded by

noday48768
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)
4 views41 pages

Harshit Pandey

Uploaded by

noday48768
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/ 41

/*

Source code to convert temperature from Celsius to Fahrenheit

Name - HARSHIT PANDEY

Section - A

Roll No.- 2461373

Branch - C.S.E

*/

#include<stdio.h>

int main() {

float f, c;

printf("\nEnter the temp. in Celsius: ");

scanf("%f", &c);

f = c * 1.8 + 32;

printf("\nThe temperature in Fahrenheit is: %.2f", f);

return 0;

/*

OUTPUT:

Enter the temp. in Celsius: 26

The temperature in Fahrenheit is: 78.80

*/
/*

Source code to calculate Simple Interest and Compound Interest

Name - HARSHIT PANDEY

Section - A

Roll No.- 2461373

Branch - C.S.E

*/

#include<stdio.h>

#include<math.h>

int main() {

float p, r, t, si, A, ci;

printf("Enter the values of p, r, and t: ");

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

si = (p * r * t) / 100;

A = p * pow(1 + (r / 100), t);

ci = A - p;

printf("\nSimple interest: %f", si);

printf("\nCompound interest: %f", ci);

return 0;

/*

OUTPUT:

Enter the values of p, r, and t

500 2 1

Simple interest: 10.000000

Compound interest: 10.000000

*/
/*

Source code to calculate total salary

Name - HARSHIT PANDEY

Section - A

Roll No.- 2461373

Branch - C.S.E

*/

#include<stdio.h>

int main() {

float hra, ta, da, bas_sal, total_sal;

printf("Enter Basic Salary: ");

scanf("%f", &bas_sal);

hra = bas_sal * 3 / 100;

ta = bas_sal * 5 / 100;

da = bas_sal * 3 / 100;

total_sal = bas_sal + hra + ta + da;

printf("Total Salary is %f", total_sal);

return 0;

/*

OUTPUT:

Enter Basic Salary

200

Total Salary is 222.00

*/
/*

Source code to check if a number is even or odd

Name - HARSHIT PANDEY

Section - A

Roll No.- 2461373

Branch - C.S.E

*/

#include<stdio.h>

int main() {

int num;

printf("\nEnter the number: ");

scanf("%d", &num);

if (num % 2 == 0)

printf("\nThe number is even");

else

printf("\nThe number is odd");

return 0;

/*

OUTPUT:

Enter the number: 4

The number is even

Enter the number: 5

The number is odd

*/
/*

Source code to check if a number is positive, negative, or zero

Name - HARSHIT PANDEY

Section - A

Roll No.- 2461373

Branch - C.S.E

*/

#include<stdio.h>

int main() {

int num;

printf("\nEnter the number: ");

scanf("%d", &num);

if (num > 0)

printf("\nThe number is positive");

else if (num < 0)

printf("\nThe number is negative");

else

printf("\nThe number is zero");

return 0;

/*

OUTPUT:

Enter the number: 1

The number is positive

Enter the number: -2

The number is negative

Enter the number: 0

The number is zero

*/
/*

Source code to find the largest of three numbers

Name - HARSHIT PANDEY

Section - A

Roll No.- 2461373

Branch - C.S.E

*/

#include<stdio.h>

int main() {

int large, num1, num2, num3;

printf("Enter the values of num1, num2, num3: ");

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

large = num1;

if (num2 > large)

large = num2;

if (num3 > large)

large = num3;

printf("Largest number is %d", large);

return 0;

/*

OUTPUT:

Enter the values of num1, num2, num3

34 67 44

Largest number is 67

*/
/*

Source code to calculate electricity bill

Name - HARSHIT PANDEY

Section - A

Roll No.- 2461373

Branch - C.S.E

*/

#include<stdio.h>

int main() {

float unit, bill, rate;

printf("Enter the units: ");

scanf("%f", &unit);

if (unit > 2000)

rate = 7;

else if (unit > 1000)

rate = 6;

else

rate = 5;

bill = rate * unit;

printf("\nBILL: %f", bill);

return 0;

/*

OUTPUT:

Enter the units: 3000

BILL: 21000.000000

Enter the units: 2000

BILL: 12000.000000

Enter the units: 500

BILL: 2500.000000 */
/*

Source code to calculate grade based on marks

Name - HARSHIT PANDEY

Section - A

Roll No.- 2461373

Branch - C.S.E

*/

#include<stdio.h>

int main() {

float s1, s2, s3, s4, percent;

printf("Enter the marks for four subjects: ");

scanf("%f%f%f%f", &s1, &s2, &s3, &s4);

percent = (s1 + s2 + s3 + s4) * 100 / 400;

if (percent >= 85)

printf("Grade A");

else if (percent >= 70)

printf("Grade B");

else if (percent >= 55)

printf("Grade C");

else

printf("Grade D");

return 0;

/*

OUTPUT:

Enter the marks for four subjects

97 89 88 95

Grade A

Enter the marks for four subjects

33 23 27 20

Grade D

*/
/*

Source code to calculate shopkeeper bill

Name - HARSHIT PANDEY

Section - A

Roll No.- 2461373

Branch - C.S.E

*/

#include<stdio.h>

int main() {

int m_qty, bill;

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

scanf("%d", &m_qty);

bill = 5 * m_qty;

if (bill > 500)

bill = bill - bill * 10 / 100;

printf("Bill: %d", bill);

return 0;

/*

OUTPUT:

Enter the number of mangoes: 50

Bill: 250

Enter the number of mangoes: 200

Bill: 900

*/
/*

Source code to check if a year is a leap year

Name - HARSHIT PANDEY

Section - A

Roll No.- 2461373

Branch - C.S.E

*/

#include<stdio.h>

int main() {

int year;

printf("Enter a year to check if it is a leap year: ");

scanf("%d", &year);

if (year % 4 == 0)

printf("%d is a leap year.\n", year);

else

printf("%d is not a leap year.\n", year);

return 0;

/*

OUTPUT:

Enter a year to check if it is a leap year: 2020

2020 is a leap year.

Enter a year to check if it is a leap year: 1900

1900 is not a leap year.

*/
/*

Source code to calculate area of a triangle (with validation)

Name - HARSHIT PANDEY

Section - A

Roll No.- 2461373

Branch - C.S.E

*/

#include<stdio.h>

#include<math.h>

int main() {

int a, b, c;

float s, area;

printf("Enter the values of the sides of the triangle: ");

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

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

s = (float)(a + b + c) / 2;

area = sqrt(s * (s - a) * (s - b) * (s - c));

printf("\nThe area of the triangle is: %f", area);

} else

printf("\nThe triangle is not valid");

return 0;

/*

OUTPUT:

Enter the values of the sides of the triangle: 3 4 5

The area of the triangle is: 6.000000

Enter the values of the sides of the triangle: 1 1 5

The triangle is not valid

*/
/*

Source code to find roots of a quadratic equation

Name - HARSHIT PANDEY

Section - A

Roll No.- 2461373

Branch - C.S.E

*/

#include<stdio.h>

#include<math.h>

int main() {

float a, b, c, d, r1, r2;

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

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

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

if (d > 0) {

r1 = (-b + sqrt(d)) / (2 * a);

r2 = (-b - sqrt(d)) / (2 * a);

printf("Roots are real and unequal: %f and %f", r1, r2);

} else

printf("Roots are imaginary");

return 0;

/*

OUTPUT:

Enter the coefficients a, b, and c: 1 3 1

Roots are real and unequal: -0.381966 and -2.618034

Enter the coefficients a, b, and c: 2 3 4

Roots are imaginary

*/
/*

Source code to check character type

Name - HARSHIT PANDEY

Section - A

Roll No.- 2461373

Branch - C.S.E */

#include<stdio.h>

int main() {

char c;

printf("Enter a character: ");

scanf("%c", &c);

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

printf("Small case letter");

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

printf("Capital case letter");

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

printf("Digit");

else

printf("Special symbol");

return 0;

/* OUTPUT:

Enter a character: a

Small case letter

Enter a character: A

Capital case letter

Enter a character: 9

Digit

Enter a character: #

Special symbol */
/*

Source code to find the sum of even and odd numbers between two integers

Name - HARSHIT PANDEY

Section - A

Roll No.- 2461373

Branch - C.S.E

*/

#include<stdio.h>

int main() {

int x, y, i, sume = 0, sumo = 0;

printf("Enter the range (x and y): ");

scanf("%d%d", &x, &y);

for (i = x; i <= y; i++) {

if (i % 2 == 0)

sume += i;

else

sumo += i;

printf("Sum of even numbers: %d\n", sume);

printf("Sum of odd numbers: %d\n", sumo);

return 0;

/*

OUTPUT:

Enter the range (x and y): 1 10

Sum of even numbers: 30

Sum of odd numbers: 25

*/
/*

Source code to print squares of numbers

Name - HARSHIT PANDEY

Section - A

Roll No.- 2461373

Branch - C.S.E

*/

#include<stdio.h>

int main() {

int i, n, num;

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

scanf("%d", &n);

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

printf("Enter a number: ");

scanf("%d", &num);

printf("The square of %d is %d\n", num, num * num);

return 0;

/*

OUTPUT:

Enter the number of terms: 3

Enter a number: 4

The square of 4 is 16

Enter a number: 5

The square of 5 is 25

Enter a number: 6

The square of 6 is 36

*/
/*

Source code to print Fibonacci series

Name - HARSHIT PANDEY

Section - A

Roll No.- 2461373

Branch - C.S.E

*/

#include<stdio.h>

void main() {

int n, first = 0, second = 1, next, c;

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

scanf("%d", &n);

printf("First %d terms of Fibonacci series are: \n", n);

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

if (c <= 1)

next = c;

else {

next = first + second;

first = second;

second = next;

printf("%d ", next);

/*

OUTPUT:

Enter the number of terms: 5

First 5 terms of Fibonacci series are:

01123 */
/*

Source code to find sum of digits of a number

Name - HARSHIT PANDEY

Section - A

Roll No.- 2461373

Branch - C.S.E

*/

#include<stdio.h>

int main() {

int n, sum = 0, remainder;

printf("Enter an integer: ");

scanf("%d", &n);

while (n != 0) {

remainder = n % 10;

sum = sum + remainder;

n = n / 10;

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

return 0;

/*

OUTPUT:

Enter an integer: 123

Sum of digits: 6

*/
/*

Source code to check if a number is an Armstrong number

Name - HARSHIT PANDEY

Section - A

Roll No.- 2461373

Branch - C.S.E

*/

#include<stdio.h>

int main() {

int num, sum = 0, temp, rem;

printf("Enter a number: ");

scanf("%d", &num);

temp = num;

while (num != 0) {

rem = num % 10;

sum = sum + (rem * rem * rem);

num = num / 10;

if (temp == sum)

printf("%d is an Armstrong number.\n", temp);

else

printf("%d is not an Armstrong number.\n", temp);

return 0;

/*

OUTPUT:

Enter a number: 153

153 is an Armstrong number.

Enter a number: 123

123 is not an Armstrong number. */


/*

Source code to check if a number is a palindrome

Name - HARSHIT PANDEY

Section - A

Roll No.- 2461373

Branch - C.S.E

*/

#include<stdio.h>

int main() {

int num, temp, rem, rev = 0;

printf("Enter a number: ");

scanf("%d", &num);

temp = num;

while (num != 0) {

rem = num % 10;

rev = rev * 10 + rem;

num = num / 10;

if (rev == temp)

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

else

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

return 0;

/*

OUTPUT:

Enter a number: 121

121 is a palindrome.

Enter a number: 123

123 is not a palindrome. */


/*

Source code to find the sum of a series

Name - HARSHIT PANDEY

Section - A

Roll No.- 2461373

Branch - C.S.E

*/

#include<stdio.h>

int main() {

int i, n, sign = 1;

float sum = 0;

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

scanf("%d", &n);

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

sum += (1.0 / i) * sign;

sign *= -1;

printf("Sum of the series: %f\n", sum);

return 0;

/*

OUTPUT:

Enter the number of terms: 3

Sum of the series: 0.866667

*/
/*

Source code to print a specific pattern

Name - HARSHIT PANDEY

Section - A

Roll No.- 2461373

Branch - C.S.E.

*/

#include<stdio.h>

void main() {

int n, i, j, k;

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

scanf("%d", &n);

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

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

printf(" ");

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

printf("*");

printf("\n");

/*

OUTPUT:

Enter the number of rows: 5

*****

****

***

**

*/
/*

Source code to search an element in a 1-D array

Name - HARSHIT PANDEY

Section - A

Roll No.- 2461373

Branch - C.S.E.

*/

#include<stdio.h>

void main() {

int arr[50], n, key, i, found = 0;

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

scanf("%d", &n);

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

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

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

printf("Enter the element to search: ");

scanf("%d", &key);

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

if (arr[i] == key) {

printf("Element found at index %d\n", i);

found = 1;

break;

if (!found)

printf("Element not found\n");

/*

OUTPUT:

Enter the size of the array: 5

Enter 5 elements: 1 2 3 4 5

Enter the element to search: 3

Element found at index 2

*/
/*

Source code to find the maximum and minimum elements in a 1-D array

Name - HARSHIT PANDEY

Section - A

Roll No.- 2461373

Branch - C.S.E

*/

#include<stdio.h>

int main() {

int arr[50], n, i, max, min;

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

scanf("%d", &n);

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

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

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

max = min = arr[0];

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

if (arr[i] > max)

max = arr[i];

if (arr[i] < min)

min = arr[i];

printf("Maximum element: %d\n", max);

printf("Minimum element: %d\n", min);

return 0;

/*

OUTPUT:

Enter the size of the array: 5

Enter 5 elements:

10 20 5 15 25

Maximum element: 25

Minimum element: 5

*/
/*

Source code to perform sorting on a 1-D array

Name - HARSHIT PANDEY

Section - A

Roll No.- 2461373

Branch - C.S.E

*/

#include<stdio.h>

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

int i, j, temp;

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;

int main() {

int arr[50], n, i;

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

scanf("%d", &n);

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

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

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

sort(arr, n);

printf("Sorted array:\n");
for (i = 0; i < n; i++)

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

return 0;

/*

OUTPUT:

Enter the size of the array: 5

Enter 5 elements:

30 10 50 20 40

Sorted array:

10 20 30 40 50

*/
/*

Source code to add two 1-D arrays of unequal sizes

Name - HARSHIT PANDEY

Section - A

Roll No.- 2461373

Branch - C.S.E

*/

#include<stdio.h>

int main() {

int arr1[50] = {0}, arr2[50] = {0}, sum[50], i, n1, n2, large;

printf("Enter the size of the first and second arrays: ");

scanf("%d%d", &n1, &n2);

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

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

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

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

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

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

large = (n1 > n2) ? n1 : n2;

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

sum[i] = arr1[i] + arr2[i];

printf("Sum of arrays:\n");

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

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

return 0;

}
/*

OUTPUT:

Enter the size of the first and second arrays: 3 2

Enter elements of the first array:

10 20 30

Enter elements of the second array:

12

Sum of arrays:

11 22 30

*/
/*

Source code to print the upper and lower triangle of a matrix

Name - HARSHIT PANDEY

Section - A

Roll No.- 2461373

Branch - C.S.E

*/

#include<stdio.h>

#define SIZE 3

int main() {

int mat[SIZE][SIZE], i, j;

printf("Enter the elements of the matrix (%dx%d):\n", SIZE, SIZE);

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

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

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

printf("Lower triangle of the matrix:\n");

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

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

if (j <= i)

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

else

printf(" ");

printf("\n");

printf("Upper triangle of the matrix:\n");


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

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

if (j >= i)

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

else

printf(" ");

printf("\n");

return 0;

/*

OUTPUT:

Enter the elements of the matrix (3x3):

123

456

789

Lower triangle of the matrix:

45

789

Upper triangle of the matrix:

123

56

*/
/*

Source code to calculate the sum of first N integers using a function

Name - HARSHIT PANDEY

Section - A

Roll No.- 2461373

Branch - C.S.E

*/

#include<stdio.h>

int sum(int n) {

return (n * (n + 1)) / 2;

int main() {

int n, result;

printf("Enter a number: ");

scanf("%d", &n);

result = sum(n);

printf("Sum of first %d integers is %d\n", n, result);

return 0;

/*

OUTPUT:

Enter a number: 5

Sum of first 5 integers is 15

*/
/*

Source code to check if a number is prime using a function

Name - HARSHIT PANDEY

Section - A

Roll No.- 2461373

Branch - C.S.E

*/

#include<stdio.h>

int is_prime(int num) {

if (num <= 1)

return 0;

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

if (num % i == 0)

return 0;

return 1;

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

if (is_prime(num))

printf("%d is a prime number\n", num);

else

printf("%d is not a prime number\n", num);

return 0;

}
/*

OUTPUT:

Enter a number: 7

7 is a prime number

Enter a number: 4

4 is not a prime number

*/
/*

Source code to calculate the sum of digits using recursion

Name - HARSHIT PANDEY

Section - A

Roll No.- 2461373

Branch - C.S.E

*/

#include<stdio.h>

int sum_of_digits(int n) {

if (n == 0)

return 0;

return (n % 10) + sum_of_digits(n / 10);

int main() {

int num, result;

printf("Enter a number: ");

scanf("%d", &num);

result = sum_of_digits(num);

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

return 0;

/*

OUTPUT:

Enter a number: 123

Sum of digits: 6

*/
/*

Source code to calculate nCr using recursion

Name - HARSHIT PANDEY

Section - A

Roll No.- 2461373

Branch - C.S.E

*/

#include<stdio.h>

int factorial(int n) {

if (n <= 1)

return 1;

return n * factorial(n - 1);

int nCr(int n, int r) {

return factorial(n) / (factorial(r) * factorial(n - r));

int main() {

int n, r, result;

printf("Enter n: ");

scanf("%d", &n);

printf("Enter r: ");

scanf("%d", &r);

if (n < r) {

printf("Invalid input. n should be greater than or equal to r.\n");

return 0;

}
result = nCr(n, r);

printf("nCr = %d\n", result);

return 0;

/*

OUTPUT:

Enter n: 5

Enter r: 2

nCr = 10

*/

You might also like