Basic 10 C Programs
Basic 10 C Programs
Basic 10 C Programs
Report
On
Fundamentals of C
CS113AT
Under the
Guidance
of
Dr. Jayasimha S R
Assistant Professor
Department of MCA
1
Bachelor of Engineering
CERTIFICATE
S ANUPAMA KUMAR
Assistant Professor
Department of MCA,
RV College of Engineering®
2
INDEX PAGE
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.
Step 1: Start
Step 4: Add num1 and num2 and assign the result to sum.
sum←num1+num2
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
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:
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:
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:
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:
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
CONCLUSION
21
REFERENCES
1)https://www.programiz.com/dsa/algoritm
2) https://chat.openai.com/
3) https://www.programiz.com/c-
programming/online-compiler/
22