Nothing Special   »   [go: up one dir, main page]

Basic 10 C Programs

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Experiential Learning

Report

On

Fundamentals of C

CS113AT

Submitted in Partial Fulfillment of the Requirement


for the I Semester

Bachelor of Engineering, MECHANICAL ENGINEERING


By

RVCE23BME127 ARAVIND PATIL

Under the

Guidance

of
Dr. Jayasimha S R
Assistant Professor
Department of MCA

RV College of Engineering®, Mysuru Road


RV Vidyanikethan Post, Bengaluru – 560059
January 2024

1
Bachelor of Engineering

CERTIFICATE

This is to certify that the assignment entitled “Fundamentals of C”


submitted in partial fulfillment Modern Fundamentals of C of I Semester BE is a
result of the bonafide work carried out by ARAVIND PATIL, RVCE23ME127 ,
during the Academic year 2023-24.

S ANUPAMA KUMAR
Assistant Professor
Department of MCA,
RV College of Engineering®

2
INDEX PAGE

SL.NO PARTICULARS PAGE


NO
1. INTRODUCTION

2. LITERATURE SURVEY

3. ALGORITHMS

4. FLOWCHART

5. CODE

6. METHODOLOGY

7. CONCLUSION

8. REFERENCES

3
INTRODUCTION

- FUNDAMENTALS OF PROGRAMMING: C IS A
PROCEDURAL PROGRAMMING LANGUAGE THAT
FOCUSES ON CORE PROGRAMMING CONCEPTS
SUCH AS VARIABLES, CONTROL STRUCTURES IN
C (LOOPS, CONDITIONS), FUNCTIONS, DATA
TYPES, AND BASIC ALGORITHMS. THESE BASIC
CONCEPTS IN C PROGRAMMING ARE ALSO
SHARED BY MANY OTHER
PROGRAMMING LANGUAGES.

LITREATURE SURVEY
THE C PROGRAMMING LANGUAGE HAS BEEN
AROUND FOR MANY YEARS AND IS A POPULAR
ANDSIGNIFICANT COMPUTER LANGUAGE. DENNIS
RITCHIE CREATED THE C PROGRAMMING LANGUAGE
AT BELL LABSIN THE EARLY 1970S WITH THE GOAL
OF CREATING A STRAIGHTFORWARD, EFFECTIVE,
AND PORTABLE LANGUAGE THATCOULD BE USED TO
CREATE SYSTEM SOFTWARE AND OTHER LOW-LEVEL
APPLICATIONS. MANY OF C’S STRENGTHS,SUCH AS
ITS LOW-LEVEL CAPABILITIES, EFFECTIVE MEMORY
MANAGEMENT, PORTABILITY, AND BROAD RANGEOF
APPLICATIONS, CAN BE CREDITED FOR ITS
POPULARITY AND LONGEVITY. OPERATING SYSTEMS,

4
EMBEDDEDDEVICES, APPLICATIONS FOR SCIENCE
AND ENGINEERING, VIDEO GAMES, AND MORE HAVE
ALL BEEN CREATEDUSING C. C++, JAVA, AND
PYTHON ARE JUST A FEW OF THE PROGRAMMING
LANGUAGES THAT C HAS IMPACTED,MAKING IT AN
IMPORTANT FIGURE IN THE HISTORY OF COMPUTER
SCIENCE. DESPITE ITS ADVANCED AGE, C ISSTILL A
VITAL AND RELEVANT LANGUAGE FOR MANY
DEVELOPERS, AND IT WILL PLAY A SIGNIFICANT
ROLE IN THECREATION OF SOFTWARE FOR MANY
YEARS TO COME

ALGORITHMS
An algorithm is a sequence of instructions that are carried out
in a predetermined sequence in order to solve a problem or
complete a work.

Algorithm : Add two numbers entered by the user

Step 1: Start

Step 2: Declare variables num1, num2 and sum.

Step 3: Read values num1 and num2.

Step 4: Add num1 and num2 and assign the result to sum.

sum←num1+num2

Step 5: Display sum

5
Step 6: Stop

FLOWCHART-
A FLOWCHART IS A TYPE OF DIAGRAM THAT REPRESENTS AN ALGORITHM,
WORKFLOW OR PROCESS. THIS DIAGRAMMATIC REPRESENTATION ILLUSTRATES A
SOLUTION MODEL TO A GIVEN PROBLEM.

6
7
CODE

1. Draw a flowchart to search a given number


using Binary search technique and implement the
same using C program
CODE:
#include<stdio.h>
int binarySearch(int arr[],int n,int target){
int low=0,high=n-1;
while(low<=high){
int mid=low+(high-low)/2;
if (arr[mid]==target)
return mid;
else if (arr[mid]<target)
low=mid+1;
else
high=mid-1;
}
return -1;
}
int main() {
int arr[]={1,2,3,4,5,6,7,8,9,10};
int n=sizeof(arr);
int target=5;
int result=binarySearch(arr,n,target);
if (result!=-1)
printf("element found at index %d\n",result);
else
printf("element not found in the array\n");
return 0;
}

OUTPUT

FLOWCHART

8
2. Write an algorithm for copying elements of one
Array to another Array and implement the same
using C program.

9
CODE:
#include <stdio.h>
void copyArray(int sourceArray[], int destinationArray[], int size) {
for (int i = 0; i < size; i++) {
destinationArray[i] = sourceArray[i];
}
}
int main() {
int sourceArray[] = {1, 2, 3, 4, 5};
int size = sizeof(sourceArray) / sizeof(sourceArray[0]);
int destinationArray[size];
copyArray(sourceArray, destinationArray, size);
printf("Source Array: ");
for (int i = 0; i < size; i++) {
printf("%d ", sourceArray[i]);
}
printf("\nDestination Array: ");
for (int i = 0; i < size; i++) {
printf("%d ", destinationArray[i]);
}
return 0;
}
OUTPUT:

ALGORITHM:
STEP 1: START
STEP 2: INITIALIZE arr1[] ={1, 2, 3, 4, 5}
STEP 3: CREATE arr2[] of size arr1[].
STEP 4: COPY elements of arr1[] to arr2[]
STEP 5: REPEAT STEP 6 UNTIL (i<arr1.length)
STEP 6: arr2[i] =arr1[i]
STEP 7: DISPLAY elements of arr1[].
STEP 8: REPEAT STEP 9 UNTIL (i<arr1.length)
STEP 9: PRINT arr1[i]
STEP 10: DISPLAY elements of arr2[].
STEP 11: REPEAT STEP 12 UNTIL (i<arr2.length)
STEP 12: PRINT arr2[i].
STEP 13: END

10
3(A). Write a program to check whether a given
number is Armstrong or not.
CODE:
#include <stdio.h>
int main() {
int num, originalNum, remainder, result = 0;
printf("Enter a three-digit integer: ");
scanf("%d", &num);
originalNum = num;
while (originalNum != 0) {
remainder = originalNum % 10;
result += remainder * remainder * remainder;
originalNum /= 10;
}
if (result == num)
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);
return 0;
}
OUTPUT:

3(B). Write a program to find the largest of 3


numbers using the ternary operator
CODE:
#include <stdio.h>
int main()
{
int n1 = 5, n2 = 10, n3 = 15, max;
max = (n1 > n2) ? (n1 > n3 ? n1 : n3) : (n2 > n3 ? n2 : n3);
printf("Largest number among %d, %d and %d is %d.",
n1, n2, n3, max);
return 0;
}
OUTPUT:

11
4. Write a C program to calculate electricity bill for
400 Units
the rate of electricity bill is as followed:
● For 1 to 50 units – 4.00 Rs/unit
● For 50 to 100 units – 4.50 Rs/unit
● For 100 to 150 units – 5.00 Rs/unit
● For 150 units and above – 6.00 Rs/unit
CODE:
#include <stdio.h>
int main()
{
int unit;
float amt, total_amt, sur_charge;
printf("Enter total units consumed: ");
scanf("%d", &unit);
if(unit <= 50)
{
amt = unit * 4.00;
}
else if(unit <= 100)
{
amt = 25 + ((unit-50) * 4.50);
}
else if(unit <= 150)
{
amt = 100 + ((unit-150) * 5.00);
}
else
{
amt = 220 + ((unit-250) * 6.00);
}
sur_charge = amt * 0.20;
total_amt = amt + sur_charge;
printf("Electricity Bill = Rs. %.2f", total_amt);
return 0;
}

12
OUTPUT:

5. Write a program to compute the N Fibonacci


numbers and prime numbers from 1 to N using for
and do-while loops respectively
CODE:
#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;
}
OUTPUT:

6. Write a program to read matrix A and B, and


compute product matrix C of A and B.

CODE:
#include <stdio.h>
void getMatrixElements(int matrix[][10], int row, int column) {
printf("\nEnter elements: \n");
13
for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j) {
printf("Enter a%d%d: ", i + 1, j + 1);
scanf("%d", &matrix[i][j]);
}
}
}
void multiplyMatrices(int first[][10],
int second[][10],
int result[][10],
int r1, int c1, int r2, int c2) {
for (int i = 0; i < r1; ++i) {
for (int j = 0; j < c2; ++j) {
result[i][j] = 0;
}
}
for (int i = 0; i < r1; ++i) {
for (int j = 0; j < c2; ++j) {
for (int k = 0; k < c1; ++k) {
result[i][j] += first[i][k] * second[k][j];
}
}
}
}
void display(int result[][10], int row, int column) {

printf("\nOutput Matrix:\n");
for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j) {
printf("%d ", result[i][j]);
if (j == column - 1)
printf("\n");
}
}
}
int main() {
int first[10][10], second[10][10], result[10][10], r1, c1, r2, c2;
printf("Enter rows and column for the first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for the second matrix: ");
scanf("%d %d", &r2, &c2);
while (c1 != r2) {
printf("Error! Enter rows and columns again.\n");

14
printf("Enter rows and columns for the first matrix: ");
scanf("%d%d", &r1, &c1);
printf("Enter rows and columns for the second matrix: ");
scanf("%d%d", &r2, &c2);
}
getMatrixElements(first, r1, c1);
getMatrixElements(second, r2, c2);
multiplyMatrices(first, second, result, r1, c1, r2, c2);
display(result, r1, c2);
return 0;
}

OUTPUT:

7. Using user defined functions write a C program


to sort the given list of N numbers in ascending
order using Bubble Sort Technique
15
CODE:
#include <stdio.h>
#define MAXSIZE 10
void main()
{
int array[MAXSIZE];
int i, j, num, temp;
printf("Enter the value of num \n");
scanf("%d", &num);
printf("Enter the elements one by one \n");
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
printf("Input array is \n");
for (i = 0; i < num; i++)
{
printf("%d\n", array[i]);
}
for (i = 0; i < num; i++)
{
for (j = 0; j < (num - i - 1); j++)
{
if (array[j] > array[j + 1])
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
printf("Sorted array is...\n");
for (i = 0; i < num; i++)
printf("%d\n", array[i]);
}

OUTPUT:

16
8. Using user defined functions, write a C
program to check whether a given string is
Palindrome or not.

CODE:
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = { "abbba" };
int l = 0;
int h = strlen(str) - 1;
while (h > l) {
if (str[l++] != str[h--]) {
printf("%s is not a palindrome\n", str);
return 0;
}
}
printf("%s is a palindrome\n", str);
return 0;
17
}
OUTPUT:

9. Write a C program using structures and


functions to perform addition of Complex numbers

CODE:
#include <stdio.h>
typedef struct complex {
float real;
float imag;
} complex;
complex add(complex n1, complex n2);
int main() {
complex n1, n2, result;
printf("For 1st complex number \n");
printf("Enter the real and imaginary parts: ");
scanf("%f %f", &n1.real, &n1.imag);
printf("\nFor 2nd complex number \n");
printf("Enter the real and imaginary parts: ");
scanf("%f %f", &n2.real, &n2.imag);
result = add(n1, n2);
printf("Sum = %.1f + %.1fi", result.real, result.imag);
return 0;
}
complex add(complex n1, complex n2) {
complex temp;
temp.real = n1.real + n2.real;
temp.imag = n1.imag + n2.imag;
return (temp);
}
OUTPUT:

18
10. Write a C program using pointers to add the
contents of a 1-D array and 2-D array
CODE:
#include <stdio.h>
void addArrays(int *ptr1D, int (*ptr2D)[3], int rows, int cols);
int main() {
int arr1D[3] = {1, 2, 3};
int arr2D[2][3] = {{4, 5, 6}, {7, 8, 9}};
int rows = 2;
int cols = 3;
printf("Original 2-D Array:\n");
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
printf("%d\t", arr2D[i][j]);
}
printf("\n");
}
addArrays(arr1D, arr2D, rows, cols);
printf("\nModified 2-D Array after adding 1-D array:\n");
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
printf("%d\t", arr2D[i][j]);
}
printf("\n");
}
return 0;
}
void addArrays(int *ptr1D, int (*ptr2D)[3], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; ++j) {
ptr2D[i][j] += *(ptr1D + j);
}

19
}
}

OUTPUT:

20
METHODOLOGY

When programs are developed to solve real-life


problems like inventory management, payroll
processing, student admissions, examination result
processing, etc. they tend to be huge and complex.
The approach to analyzing such complex problems,
planning for software development and controlling
the development process is called programming
methodology.

CONCLUSION

We have executed the given C programs


and got the output for each of them and it
is written .

21
REFERENCES

1)https://www.programiz.com/dsa/algoritm

2) https://chat.openai.com/

3) https://www.programiz.com/c-
programming/online-compiler/

22

You might also like