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

PROGRAM 1

PROGRAM

Uploaded by

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

PROGRAM 1

PROGRAM

Uploaded by

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

ST.

ANDREWS INSTITUTE
OF TECHNOLOGY & MANAGEMENT

Gurugram Delhi (NCR)


Approved by AICTE, Govt. of India, New Delhi
Affiliated to Maharshi Dayanand University
+
‘A ’ Grade State University, accredited by NAAC

Session: 2024 – 2025

Computer Fundamentals & Problem Solving using C

Course Code: 23BCA401DS02

Semester: 1st

Bachelor of Computer Applications

Submitted By
Name: RIYA PANDEY
Roll No.: 249144

DEPARTMENT OF COMPUTER APPLICATIONS

Page | 1
PROGRAM 1

Write a C program to demonstrate the use of ‘printf()’ and ‘scanf()’


functions. Prompt the user to enter their name and age, then display
the entered information.

#include <stdio.h>
int main() {
char name[50];
int age;
printf("Enter your name: ");
scanf("%49s", name);
printf("Enter your age: ");
scanf("%d", &age);
printf("Hello, %s! You are %d years old.\n", name, age);
return 0;
}

Page | 2
Page | 3
PROGRAM 2

. Write a C program to define and use symbolic constants.


Define symbolic constants for PI and the radius of a circle,
then calculate and display the area of the circle.

#include <stdio.h>
#define PI 3.14
void main() {
float radius, area;
printf("Enter the radius : ");
scanf("%f", &radius);
area = PI * radius * radius;
printf("The area of the circle is %f", area);
}

Page | 4
Page | 5
PROGRAM 3

Write a C program to demonstrate the use of various operators and


expressions. Perform addition, subtraction, multiplication, and
division on two numbers entered by the user and display the results.

#include <stdio.h>
int main() {
float num1, num2, sum, difference, product, quotient;
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
sum = num1 + num2;
difference = num1 - num2;
product = num1 * num2;
quotient = num1 / num2;
printf("Addition: %f\n", sum);
printf("Subtraction: %f\n", difference);
printf("Multiplication: %f\n", product);
printf("Division: %f\n", quotient);
return 0;
}

Page | 6
Page | 7
PROGRAM 4

4. Write a C program to implement decision making using if-else


statements. Check if a number entered by the user is positive,
negative, or zero and display the result.

#include <stdio.h>
int main() {
float number;
printf("Enter a number: ");
scanf("%f", &number);
if (number > 0) {
printf("The number is positive.\n");
} else if (number < 0) {
printf("The number is negative.\n");
} else {
printf("The number is zero.\n");
}
return 0;
}

Page | 8
Page | 9
PROGRAM 5

5. Write a C program to implement a nested if


statement.

#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age >= 18) {
printf("You are eligible to vote.\n");
if (age >= 65) {
printf("You are also a senior citizen.\n");
} else {
printf("You are not a senior citizen.\n");
}
} else {
printf("You are not eligible to vote.\n");
}
return 0;

Page | 10
}

Page | 11
PROGRAM 6

6. Write a C program to demonstrate the use of switch statement.


Simulate a simple calculator to perform addition, subtraction,
multiplication, and division based on user input.
#include <stdio.h>
int main() {
char operator;
double num1, num2, result;
printf("Enter an operator (+, -, *, /): ");
scanf(" %c", &operator);
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
switch (operator) {
case '+':
result = num1 + num2;
printf("%.2lf + %.2lf = %.2lf\n", num1, num2, result);
break;
case '-':
result = num1 - num2;
printf("%.2lf - %.2lf = %.2lf\n", num1, num2, result);

Page | 12
break;
case '*':
result = num1 * num2;
printf("%.2lf * %.2lf = %.2lf\n", num1, num2, result);
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
printf("%.2lf / %.2lf = %.2lf\n", num1, num2, result);
} else {
printf("Error: Division by zero is not allowed.\n");
}
break;
default:
printf("Error: Invalid operator.\n");
break;
}
return 0;
}

Page | 13
Page | 14
PROGRAM 7

7. Write a C program to implement looping using a while loop.


#include <stdio.h>

int main() {
int number;
int sum = 0;
printf("Enter positive numbers to sum them up (enter a negative
number to stop):\n");
while (1) {
printf("Enter a number: ");
scanf("%d", &number);
if (number < 0) {
break;
}
sum += number;
}
printf("The total sum of the entered positive numbers is: %d\n",
sum);
return 0;
}

Page | 15
Page | 16
PROGRAM 8

8. Write a C program to implement looping using a do-while loop.

#include <stdio.h>
int main() {
int number;
int sum = 0;
do {
printf("Enter a number (enter 0 to stop): ");
scanf("%d", &number);
sum += number;
} while (number != 0);
printf("The total sum of the entered numbers is: %d\n", sum -
number);
return 0;
}

Page | 17
Page | 18
PROGRAM 9
9. Write a C program to implement looping using for loop

#include <stdio.h>
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
if (n <= 0) {
printf("Please enter a positive integer.\n");
} else {
printf("The first %d natural numbers are:\n", n);
for (int i = 1; i <= n; ++i) {
printf("%d ", i);
}
printf("\n");
return 0;
}
}

Page | 19
Page | 20
PROGRAM 10

10.Write a C program to demonstrate the use of break and continue


statements. Print numbers from 1 to 10, but skip the number 5 using
continue and stop the loop when it reaches 8 using break.

#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
continue;
}
if (i == 8) {
break;
}
printf("%d\n", i);
}
return 0;
}
Page | 21
Page | 22
PROGRAM 11

11. Write a C program to define a user defined function to calculate


the factorial of a number, entered by the user and display the result.

#include <stdio.h>
unsigned long long factorial(int n);
int main() {
int number;
printf("Enter a positive integer: ");
scanf("%d", &number);
if (number < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
printf("Factorial of %d = %llu\n", number, factorial(number));
}
return 0;
}
unsigned long long factorial(int n) {
if (n == 0) {
return 1;
} else {
Page | 23
return n * factorial(n - 1);
}
}

Page | 24
Page | 25
PROGRAM 12
12. Write a C program to demonstrate the use of arrays. Read 10
integers from the user, store them in an array to find the largest
number.
#include <stdio.h>
int main() {
int numbers[10];
int largest;
printf("Enter 10 integers:\n");
for (int i = 0; i < 10; i++) {
printf("Number %d: ", i + 1);
scanf("%d", &numbers[i]);
}
largest = numbers[0];
for (int i = 1; i < 10; i++) {
if (numbers[i] > largest) {
largest = numbers[i];
}
}
printf("The largest number is: %d\n", largest);
return 0;
}

Page | 26
Page | 27
PROGRAM 13

13. Write a C program toread a string from the user and then
display its length, and convert it to uppercase and lowercase.

#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char str[100];
int length;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = '\0';
length = strlen(str);
printf("Length of the string: %d\n", length);
printf("Uppercase: ");
for (int i = 0; i < length; i++) {
putchar(toupper(str[i]));
}
printf("\n");
printf("Lowercase: ");

Page | 28
for (int i = 0; i < length; i++) {
putchar(tolower(str[i]));
}
printf("\n");
return 0;
}

Page | 29
Page | 30
PROGRAM 14

14.Write a C program to demonstrate the use of pointers. Declare an


integer variable, assign a value to it, and then use a pointer to
display its value and address.

#include <stdio.h>
int main() {
int num;
int *ptr;
num = 42;
ptr = &num;
printf("Value of num: %d\n", *ptr);
printf("Address of num: %p\n", (void*)&num);
printf("Address stored in ptr: %p\n", (void*)ptr);
return 0;
}

Page | 31
Page | 32
PROGRAM 15
15.Write a C program to pass arrays to a function. Read 5
integers into an array, pass the array to a function, and return
sum of the elements of the array.

#include <stdio.h>
int sumArray(int arr[], int size);
int main() {
int numbers[5];
int total;
printf("Enter 5 integers:\n");
for (int i = 0; i < 5; i++) {
printf("Number %d: ", i + 1);
scanf("%d", &numbers[i]);
}
total = sumArray(numbers, 5);
printf("The sum of the elements in the array is: %d\n", total);
return 0;
}
int sumArray(int arr[], int size) {
int sum = 0;
for (int i = 0; i < size; i++) {
sum += arr[i];
}
return sum;
}

Page | 33
Page | 34

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy