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

Yashveer C Programming file 1

The document contains a practical file for problem solving using C programming, detailing 20 different programs. Each program includes a brief description, the source code, and sample output. Topics covered include basic arithmetic operations, conditional statements, loops, arrays, and functions.

Uploaded by

syashvir69
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Yashveer C Programming file 1

The document contains a practical file for problem solving using C programming, detailing 20 different programs. Each program includes a brief description, the source code, and sample output. Topics covered include basic arithmetic operations, conditional statements, loops, arrays, and functions.

Uploaded by

syashvir69
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

wa n t S in g h M anage

al
B echnical Campus me
aja T Khandari Farm, Agra - n
R

t
282002 (U.P.)

SESSION 2023-24
PRACTICAL FILE
PROBLEM SOLVING USING C

Submitted To:- Submitted By :-


Mr. K. K Goyal Sir Yashveer Singh
Class-MCA 1ST SEM
. Program No.-1

Write a program to add 2 numbers .

#include <stdio.h>

void main() {
int a = 5; // Initialized value for a
int b = 2;
int n;

n = a + b;

printf(" The sum of two number a and b is : %d", n);

return 0;
}
OUTPUT

The sum of two number a and b is : 7


Program No.-2

Write a program to check the number is odd or not.

#include <stdio.h>

int main() {
int number;

printf("Enter an integer: ");


scanf("%d", &number);

if (number % 2 == 1) {
printf("%d is an odd number.\n", number);
} else {
printf("%d is not an odd number.\n", number);
}

return 0;
}
OUTPUT

For input : number=4

Enter an integer: 4
4 is not an odd number.
Program No.-3

Write a program to check the number is positive or not

#include <stdio.h>

int main() {
int number;

printf("Enter an integer: ");


scanf("%d", &number);

if (number > 0) {
printf("%d is a positive number.\n", number);
} else if (number == 0) {
printf("The entered number is zero.\n");
} else {
printf("%d is not a positive number.\n", number);
}

return 0;
}
OUTPUT

For input : number = 5

Enter an integer: 5
5 is a positive number.
Program No.-4
Write a program to print first 10 odd numbers in reverse order.

#include <stdio.h>

int main() {
int count = 0;
int i = 1;

printf("First 10 odd numbers in reverse order:\n");


for (i = 19; count < 10; i -= 2) {
printf("%d\n", i);
count++;
}

return 0;
}
OUTPUT
First 10 odd numbers in reverse order:
19
17
15
13
11
9
7
5
3
1
Program No.-5

Write a program to check whether the year is leap year or not using
function

#include <stdio.h>

int isLeapYear(int year)


{
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
return 1;
}
else {
return 0; // Not a leap year
}
}
int main() {
int year;

printf("Enter a year: ");


scanf("%d", &year);

if (isLeapYear(year)) {
printf("%d is a leap year.\n", year);
} else {
printf("%d is not a leap year.\n", year);
}
return 0;
}
OUTPUT
For input : year = 2028

Enter a year: 2028


2028 is a leap year.
Program No.-6

Write a program to check the number is even or not

#include <stdio.h>

int main() {
int number;

// Input the number


printf("Enter an integer: ");
scanf("%d", &number);

// Check if the number is even


if (number % 2 == 0) {
printf("%d is an even number.\n", number);
} else {
printf("%d is not an even number.\n", number);
}

return 0;
}
OUTPUT

For input : number = 6

Enter an integer: 6
6 is an even number
Program No.-7

Write a program print number from 1 to 10 .

#include <stdio.h>

int main() {
// Print numbers from 1 to 10
for (int i = 1; i <= 10; i++) {
printf("%d ", i);
}

return 0;
}
OUTPUT

1 2 3 4 5 6 7 8 9 10
Program No.-8

Program to print elements of an array in reverse order.

#include <stdio.h>

void printReverseArray(int arr[], int size) {


for (int i = size - 1; i >= 0; i--) {
printf("%d ", arr[i]);
}
printf("\n");
}

int main() {
// Example usage:
int myArray[] = {1, 2, 3, 4, 5};
int size = sizeof(myArray) / sizeof(myArray[0]);

printf("Original array: ");


for (int i = 0; i < size; i++) {
printf("%d ", myArray[i]);
}
printf("\n");

printf("Array in reverse order: ");


printReverseArray(myArray, size);

return 0;
}

OUTPUT

Original array: 1 2 3 4 5
Array in reverse order: 5 4 3 2 1
Program No.-9
Write a program to calculate SI

#include <stdio.h>

float calculateSimpleInterest(float principal, float rate, int time)


{
float simpleInterest = (principal * rate * time) / 100;
return simpleInterest;
}
int main() {
float principal, rate;
int time;

printf("Enter principal amount: ");


scanf("%f", &principal);

printf("Enter rate of interest: ");


scanf("%f", &rate);

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


scanf("%d", &time);

float simpleInterest = calculateSimpleInterest(principal, rate, time);

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

return 0;
}}

OUTPUT
For the input as : principal amount=10000 , interest rate = 10%, time=20 years

Enter principal amount: 10000


Enter rate of interest: 10
Enter time period (in years): 20
Simple Interest: 20000.00
Program No.-10

Write a program to find the average of array elements

#include <stdio.h>

float calculateArrayAverage(int arr[], int size)


{
int sum = 0;
for (int i = 0; i < size; i++)
{
sum += arr[i];
}
return (float)sum / size;
}
int main() {
int size;

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


scanf("%d", &size);

int myArray[size];
printf("Enter %d elements:\n", size);
for (int i = 0; i < size; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &myArray[i]);
}

float average = calculateArrayAverage(myArray, size);


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

return 0;
}
OUTPUT
For The Array A={ 2 , 4 , 6, 8 }

Enter the size of the array: 4


Enter 4 elements:
Element 1: 2
Element 2: 4
Element 3: 6
Element 4: 8
Average of array elements: 5.00
Program No.-11

Write a program to accepts the element of an array through keyboard

#include <stdio.h>

int main()
{
int size;

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


scanf("%d", &size);

int myArray[size];

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


for (int i = 0; i < size; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &myArray[i]);
}

printf("Entered array elements:\n");


for (int i = 0; i < size; i++) {
printf("%d ", myArray[i]);
}
printf("\n");

return 0;
}
OUTPUT

For input : elements = 1,2,3,4,5

Enter the size of the array: 5


Enter 5 elements:
Element 1: 1
Element 2: 2
Element 3: 3
Element 4: 4
Element 5: 5
Entered array elements:
12345
Program No.-12

Write a program to print following output.


1
23
456
7 8 9 10

#include <stdio.h>

int main() {
int rows, number = 1;

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


for (int j = 1; j <= i; j++) {
printf("%d ", number);
number++;
}
printf("\n");
}

return 0;
}
OUTPUT

1
23
456
7 8 9 10
Program No.-13

Write a program to print table from 2 and 4

#include <stdio.h>

int main() {
// Print multiplication tables for 2 and 4
for (int i = 2; i <= 2; i += 2) {
printf("Multiplication table for %d:\n", i);
for (int j = 1; j <= 10; j++) {
printf("%d x %d = %d\n", i, j, i * j);
}
printf("\n");
}

return 0;
}
OUTPUT

Multiplication table for 2:


2x1=2
2x2=4
2x3=6
2x4=8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
Program No.-14

Write a program to calculate the sum of array elements

#include <stdio.h>

int main() {
int size;

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


scanf("%d", &size);

int myArray[size];

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


for (int i = 0; i < size; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &myArray[i]);
}
int sum = 0;
for (int i = 0; i < size; i++) {
sum += myArray[i];
}
printf("Sum of array elements: %d\n", sum);

return 0;
}
OUTPUT

For Array : A={2. 4, 6, 8 ,10}

Enter the size of the array: 5


Enter 5 elements:
Element 1: 2
Element 2: 4
Element 3: 6
Element 4: 8
Element 5: 10
Sum of array elements: 30
Program No.-15

Write a program using for statement to print first Seven number in


descending and ascending order.

#include <stdio.h>

int main() {

printf("Descending Order:\n");
for (int i = 7; i >= 1; i--) {
printf("%d ", i);
}
printf("\n");

printf("Ascending Order:\n");
for (int i = 1; i <= 7; i++) {
printf("%d ", i);
}
printf("\n");

return 0;
}
OUTPUT

Descending Order:
7654321
Ascending Order:
1234567
Program No.-16

WAP to accept the elements from keyboard and print matrix.

#include <stdio.h>

int main() {
int arr[2][2];

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


for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
printf("Element at position [%d][%d]: ", i + 1, j + 1);
scanf("%d", &arr[i][j]);
}
}
printf("Matrix:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
printf("%d\t", arr[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT
For input : matrix A elements = {1, 2,3,4 }

Enter the elements of the matrix:


Element at position [1][1]: 1
Element at position [1][2]: 2
Element at position [2][1]: 3
Element at position [2][2]: 4
Matrix:
1 2
3 4
Program No.-17

Write a program to calculate the sum of 5 digit number.

#include <stdio.h>

int main()
{
int number, digit, sum = 0;

printf("Enter a 5-digit number: ");


scanf("%d", &number);

if (number < 10000 || number > 99999)


{
printf("Please enter a valid 5-digit number.\n");
return 1; // Exit the program with an error code
}
while (number > 0) {
digit = number % 10;
sum += digit;
number /= 10;
}
printf("Sum of digits: %d\n", sum);

return 0;
}
OUTPUT

For Input : number = 12345

Enter a 5-digit number: 12345


Sum of digits: 15
Program No.-18

Write a program to calculate SI 3 times.

#include <stdio.h>

int main() {
int p, r, t;
int C = 0;
float SI;

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


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

while (C < 3) {
SI = (p * r * t) / 100.0;
printf("Simple Interest: %f\n", SI);
C++;
}
return 0;
}

OUTPUT

For input : principal=1000 , rate= 5% and time= 2 years

Enter the values of p, r, and t: 1000


5
2
Simple Interest: 100.000000
Simple Interest: 100.000000
Simple Interest: 100.000000
Program No.-19

Write a program to print and check the condition for the following.
if % is greater than equal to be 60% then print Ist division.
if it is less than to 60% and above or equal to 40% then print 2nd
division.
if it is less than 40 % then print failed devision .

#include <stdio.h>

void main() {
int p;

// Input percentage
printf("Write percentage: ");
scanf("%d", &p);

if (p >= 60) {
printf("First division\n");
} else if (p >= 40) {
printf("Second division\n");
} else {
printf("Failed division\n");
}

return 0;
}
OUTPUT

For input : percentage = 65%

Write percentage: 65
First division
Program No.-20

Write a program to print first 50 even number.

#include <stdio.h>

int main() {
// Print the first 50 even numbers
for (int i = 2; i <= 100; i += 2) {
printf("%d ", i);
}

return 0;
}
OUTPUT

2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42
44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80
82 84 86 88 90 92 94 96 98 100
Program No.-21

Write a program to interchange the value of two variables.

#include <stdio.h>

void main() {
int a = 5, b = 10, temp;

// Display values of a and b before interchange


printf("Values of a and b before interchange: %d %d\n", a, b);

// Interchange the values using a temporary variable


temp = a;
a = b;
b = temp;

// Display values of a and b after interchange


printf("Values of a and b after interchange: %d %d\n", a, b);

return 0;
}
OUTPUT

Values of a and b before interchange: 5 10


Values of a and b after interchange: 10 5
Program No.-22

Write a program to find the greatest value amongst 3.

#include <stdio.h>

int main() {
int num1, num2, num3;

// Input three numbers


printf("Enter three numbers separated by space: ");
scanf("%d %d %d", &num1, &num2, &num3);

// Find the greatest value


if (num1 >= num2 && num1 >= num3) {
printf("%d is the greatest number.\n", num1);
} else if (num2 >= num1 && num2 >= num3) {
printf("%d is the greatest number.\n", num2);
} else {
printf("%d is the greatest number.\n", num3);
}

return 0;
}
OUTPUT

For input : number = 10 , 1 , 45 ;

Enter three numbers separated by space: 10 1 45


45 is the greatest number.
. Program No.-23

Write a program to write the content into a file .BU

#include <stdio.h>

int main() {
FILE *filePointer;
char content[100];

// Open the file in write mode


filePointer = fopen("output.txt", "w");

// Check if the file is successfully opened


if (filePointer == NULL) {
printf("Error opening the file.\n");
return 1;
}

// Input content from the user


printf("Enter content to write to the file:\n");
fgets(content, sizeof(content), stdin);

// Write content to the file


fprintf(filePointer, "%s", content);

// Close the file


fclose(filePointer);

printf("Content written to the file successfully.\n");

return 0;
}
. Program No.-24

Write a program the to read the content from a file

#include <stdio.h>

int main() {
FILE *filePointer;
char content[100];

// Open the file in read mode


filePointer = fopen("input.txt", "r");

// Check if the file is successfully opened


if (filePointer == NULL) {
printf("Error opening the file.\n");
return 1;
}

// Read content from the file


printf("Content of the file:\n");
while (fgets(content, sizeof(content), filePointer) !=
NULL) {
printf("%s", content);
}

// Close the file


fclose(filePointer);

return 0;
}
. Program No.-25

Write a program to copy the content from one file to


another .

#include <stdio.h>

int main() {
FILE *sourceFile, *destinationFile;
char content[100];

// Open the source file in read mode


sourceFile = fopen("source.txt", "r");

// Check if the source file is successfully opened


if (sourceFile == NULL) {
printf("Error opening the source file.\n");
return 1;
}

// Open the destination file in write mode


destinationFile = fopen("destination.txt", "w");

// Check if the destination file is successfully opened


if (destinationFile == NULL) {
printf("Error opening the destination file.\n");
fclose(sourceFile); // Close the source file before
exiting
return 1;
}

// Copy content from the source file to the destination file


while (fgets(content, sizeof(content), sourceFile) !=
NULL) {
fprintf(destinationFile, "%s", content);
}

// Close both files


fclose(sourceFile);
fclose(destinationFile);

printf("Content copied from source.txt to destination.txt


successfully.\n");

return 0;
}
PROGRAM NO.-26

WRITE A C PROGRAM TO PERFORM ADDITION USING BITWISE OPERATORS

#include <stdio.h>

int add(int a, int b) {


while (b != 0) {
// Carry contains common set bits of a and b
int carry = a & b;

// Sum of bits of a and b where at least one of the bits is not set
a = a ^ b;

// Carry is shifted by one so that adding it to a gives the required sum


b = carry << 1;
}
return a;
}

int main() {
int num1, num2;

printf("Enter first number: ");


scanf("%d", &num1);

printf("Enter second number: ");


scanf("%d", &num2);

int result = add(num1, num2);

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

return 0;
}

OUTPUT

Enter first number: 5


Enter second number: 7
Sum: 12

file:///C/Users/yashv/Downloads/New%20folder/5.txt[16-02-2024 16:36:50]
PROGRAM NO.-27

WRITE A C PROGRAM TO COMPARE TWO


STRINGS

#include <stdio.h>

int compareStrings(const char *str1, const char *str2) {


while (*str1 != '\0' && *str2 != '\0') {
if (*str1 != *str2) {
return 0; // Strings are not equal
}
str1++;
str2++;
}

// Check if both strings have reached the end


return (*str1 == '\0' && *str2 == '\0');
}

int main() {
char str1[100], str2[100];

// Get input strings from the user


printf("Enter the first string: ");
scanf("%s", str1);

printf("Enter the second string: ");


scanf("%s", str2);

// Compare strings and display the result


if (compareStrings(str1, str2)) {
printf("The strings are equal.\n");
} else {
printf("The strings are not equal.\n");
}

return 0;
}

OUTPUT

Enter the first string: hello


Enter the second string: hello
The strings are equal.

file:///C/Users/yashv/Downloads/New%20folder/10.txt[16-02-2024 16:37:28]
PROGRAM NO.-28

WRITE A C PROGRAM TO DISPLAY THE ASCII VALUE OF A GIVEN CHRACTER WITH


OUTPUT
#include <stdio.h>

int main() {
char inputChar;

// Get character input from the user


printf("Enter a character: ");
scanf("%c", &inputChar);

// Display ASCII value


printf("ASCII value of %c is %d\n", inputChar, inputChar);

return 0;
}

OUTPUT

Enter a character: A
ASCII value of A is 65

file:///C/Users/yashv/Downloads/New%20folder/1.txt[16-02-2024 16:34:56]
PROGRAM NO.- 29

WRITE A C PROGRAM TO PERFORM ADDITION USING BITWISE OPERATORS

#include <stdio.h>

int add(int a, int b) {


while (b != 0) {
// Carry contains common set bits of a and b
int carry = a & b;

// Sum of bits of a and b where at least one of the bits is not set
a = a ^ b;

// Carry is shifted by one so that adding it to a gives the required sum


b = carry << 1;
}
return a;
}

int main() {
int num1, num2;

printf("Enter first number: ");


scanf("%d", &num1);

printf("Enter second number: ");


scanf("%d", &num2);

int result = add(num1, num2);

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

return 0;
}

OUTPUT

Enter first number: 5


Enter second number: 7
Sum: 12

file:///C/Users/yashv/Downloads/New%20folder/5.txt[16-02-2024 16:36:50]

You might also like