0% found this document useful (0 votes)
29 views23 pages

C Prog Part A and B 27 Progs

Journal bca 1st sem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views23 pages

C Prog Part A and B 27 Progs

Journal bca 1st sem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

//1.

Write a program to read radius of a circle and to find and display area and circumference

#include<stdio.h>
void main()
{
int radius;
float area, circum;

printf("Enter the radius of circle \n");

scanf("%d",&radius);

area=3.14*radius*radius;

circum=2*3.14*radius;

printf("area of circle: %f \n",area);

printf("circumference of circle: %f \n",circum);

//2. Write a program to read Principal amount, Time and Rate and calculate Simple Interest.

#include <stdio.h>

void main() {

float principal, rate, time, interest;

// Take user input for principal, rate, and time


printf("Enter principal amount: ");
scanf("%f", &principal);

printf("Enter interest rate: ");


scanf("%f", &rate);

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


scanf("%f", &time);

// Calculate simple interest


interest = (principal * rate * time) / 100;

// Display the result


printf("Simple Interest is: %f\n", interest);

}
//3. C program to find the largest number among three numbers
#include <stdio.h>

int main()
{
int a = 10, b = 22, c = 9;
printf("The numbers a, b and c are: %d, %d, %d\n", a, b, c);

if (a >= b) {

if (a >= c)
printf("%d is the largest number.", a);
else
printf("%d is the largest number.", c);

}
else {

if (b >= c)
printf("%d is the largest number.", b);
else
printf("%d is the largest number.", c);

return 0;
}

//4. Write a program to function as a basic calculator; it should ask the user to input what
type of arithmetic operation he would like, and then ask for the numbers on which the
operation should be performed. The calculator should then give the output of the operation.
Use switch. Error message should be reported, if any attempt is made to divide by zero.

#include <stdio.h>

int main() {

char op;
int first, second;

printf("Enter an operator (+, -, *, /): ");


scanf("%c", &op);

printf("Enter two operands: ");


scanf("%d %d", &first, &second);

switch (op) {

case '+':
printf("%d + %d = %d", first, second, first + second);
break;
case '-':
printf("%d - %d = %d", first, second, first - second);
break;

case '*':
printf("%d * %d = %d", first, second, first * second);
break;

case '/':
if(second == 0){
printf("Error! second number cannot be zero");
}
else{
printf("%d / %d = %d", first, second, first / second);
}
break;

// operator doesn't match any case constant


default:
printf("Error! operator is not correct");

return 0;
}

//5. Write a program to find the roots of quadratic equation (Demonstration of else-if ladder)

#include<stdio.h>
#include<math.h>

void main () {
float a,b,c,r1,r2,d;

printf ("Enter the values of a b 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("The real roots = %f %f", r1, r2);
}
else if(d==0) {
r1 = -b/(2*a);
r2 = -b/(2*a);
printf("Roots are equal =%f %f", r1, r2);
}
else
printf("Roots are imaginary");

//6. Write a program to read marks scored by a student in n subjects and find and display the
average of all marks

#include<stdio.h>
#include<conio.h>

void main()
{
int i, n, mark, sum=0;
float avg;

printf("Enter number of subjects: ");


scanf("%d",&n);

printf("Enter Marks obtained in %d Subjects: \n",n);


for(i=0; i<n; i++)
{
scanf("%d", &mark);
sum = sum+mark;
}

avg = sum/n;

printf("\nAverage Marks = %f", avg);

//7. Write a program to demonstrate basic mathematical library functions defined in math.h

#include<stdio.h>
#include <math.h>

void main(){

printf("\n%f",ceil(3.6));
printf("\n%f",ceil(3.3));
printf("\n%f",floor(3.6));
printf("\n%f",floor(3.2));
printf("\n%f",sqrt(16));
printf("\n%f",sqrt(7));
printf("\n%f",pow(2,4));
printf("\n%f",pow(3,3));
printf("\n%d",abs(-12));

}
//8. Write a program to find HCF (GCD) of two numbers.

#include <stdio.h>

//function to return gcd of a and b


int gcd(int a, int b)
{
while (a > 0 && b > 0) {
if (a > b) {
a = a % b;
}
else {
b = b % a;
}
}
if (a == 0) {
return b;
}
return a;
}

// Main code
void main()
{
int a,b;

printf("Enter values for a and b:\n");


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

printf("GCD of %d and %d is %d ", a, b, gcd(a, b));

//9. Write a program that accepts a number n, and prints all prime numbers between 1 to n.

#include<stdio.h>

void main(){

int i, num, n, flag;

printf("Enter the range: ");


scanf("%d",&n);

printf("The prime numbers in between the range 1 to %d:\n",n);

for(num=2; num<=n; num++){

flag=1;

for(i=2;i<=num/2;i++){
if(num%i==0){
flag=0;
break;
}
}

if(flag==1)
printf("%d ",num);
}
}

//10. Write a program to read a number, find the sum of its digits, reverse the number and
check if it is a palindrome. Display appropriate message.

#include <stdio.h>

void main()
{
int num, temp, remainder, reverse = 0, sum=0;

printf("Enter an integer: \n");


scanf("%d", &num);

temp = num;
while (num > 0)
{
remainder = num % 10;
reverse = reverse * 10 + remainder;
num = num / 10;

sum = sum + remainder;


}

printf("Given number is = %d\n", temp);

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

printf("Its reverse is = %d\n", reverse);

if (temp == reverse)
printf("Number is a palindrome \n");
else
printf("Number is not a palindrome \n");
}
//11. Write a program to read numbers from keyboard continuously till the user presses 999
and to find the sum of only positive numbers. Display appropriate error messages.

#include<stdio.h>

void main(){

int n, sum=0;

printf("\nEnter 999 to exit\n");

while(1){
printf("\nEnter a number:");
scanf("%d",&n);

if(n==999){
printf("\nExiting the program");
break;
}
else if(n>=0){
sum=sum+n;
}
else{
printf("\nError: You entered negative Number");
}
}

printf("\nSum of positive numbers = %d",sum);

//12. Write a program to read percentage of marks and to display appropriate message
specifying class obtained. (demonstration of Switch Case statement)

#include<stdio.h>

void main(){

float percentage;
int temp;

printf("\nEnter your percentage: \n");


scanf("%f",&percentage);

temp = percentage / 10;

switch(temp){

case 10:
case 9:
case 8:
printf("\n Grade: A");
break;
case 7:
case 6:
printf("\n Grade: B");
break;
case 5:
case 4:
printf("\n Grade: C");
break;
case 3:
case 2:
case 1:
case 0:
printf("\n Grade: Fail");
break;
default:
printf("\nInvalid input");

}
}

//13. Write a program to read and concatenate two strings without using library function

#include<stdio.h>

int length(char s[]){


int i=0;

while(s[i]!='\0'){
i++;
}
return i;
}

void main(){

char s1[100], s2[100];


int i, s1_length;

printf("Enter two strings\n");


scanf("%s%s",&s1,&s2);

i=0;

s1_length = length(s1);
while(s2[i]!='\0'){

s1[s1_length+i]=s2[i];
i++;
}
s1[s1_length+i]='\0';
printf("\n concatenated string : %s",s1);

//14. Write a program to print sum of even numbers and sum of odd numbers from array of
integers which are to be inputed.

#include<stdio.h>

void main(){

int a[100], n, i, sum_even, sum_odd;

printf("Enter array size: ");


scanf("%d",&n);

sum_even = 0;
sum_odd = 0;

printf("\nEnter %d elements\n",n);
for(i=0;i<n;i++){
scanf("%d",&a[i]);

if(a[i]%2==0)
sum_even = sum_even + a[i];
else
sum_odd = sum_odd + a[i];
}

printf("\nSum of even numbers = %d",sum_even);


printf("\nSum of odd numbers = %d",sum_odd);
}

//15. Write a program to read a list of numbers, print it, then remove duplicate elements from
the list and print the modified list. Use single dimensional Array

#include<stdio.h>

void main(){

int a[100], n, i, j, k;

printf("Enter array size: ");


scanf("%d",&n);

printf("\nEnter %d elements\n",n);
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}

printf("\nArray elements are\n");


for(i=0;i<n;i++){
printf("%d ",a[i]);
}

for(i=0;i<n;i++){
for(j = i+1; j < n; j++){
if(a[i] == a[j]){
for(k = j; k < n; k++){
a[k] = a[k+1];
}
j--;
n--;
}
}
}

printf("\nArray elements after removing duplicates\n");


for(i=0;i<n;i++){
printf("%d ",a[i]);
}

}
PART B
//1. Write a program to swap(interchange) two numbers without using a temporary variable

#include <stdio.h>

int main() {
int a, b;

// Read two numbers from the user


printf("Enter first number (a): ");
scanf("%d", &a);
printf("Enter second number (b): ");
scanf("%d", &b);

// Swapping without a temporary variable


a = a + b;
b = a - b;
a = a - b;

// Display the result


printf("After swapping: \n");
printf("a = %d\n", a);
printf("b = %d\n", b);

return 0;
}

//2. Write a program to read a string and to find the number of alphabets, digits, vowels,
consonants, spaces and special characters using character functions

#include <stdio.h>
#include <ctype.h> // For character functions

int main() {
char str[100];
int i;
int alphabets = 0, digits = 0, vowels = 0, consonants = 0, spaces = 0, specialChars = 0;

// Read a string from the user


printf("Enter a string: ");
gets(str);

// Traverse the string and count different characters


for (i = 0; str[i] != '\0'; i++) {
if (isalpha(str[i])) {
alphabets++;
if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u' ||
str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] == 'O' || str[i] == 'U') {
vowels++;
} else {
consonants++;
}
} else if (isdigit(str[i])) {
digits++;
} else if (isspace(str[i])) {
spaces++;
} else {
specialChars++;
}
}

// Display the counts


printf("Alphabets: %d\n", alphabets);
printf("Vowels: %d\n", vowels);
printf("Consonants: %d\n", consonants);
printf("Digits: %d\n", digits);
printf("Spaces: %d\n", spaces);
printf("Special Characters: %d\n", specialChars);

return 0;
}

//3. Write a program to read a string, reverse it, concatenate the original string with its
reverse, and print length of the original string and the concatenated string using built in
functions

#include <stdio.h>
#include <string.h> // For string functions

int main() {

char str[100], revstr[100], catstr[100];


int lenstr, lencatstr;

// Read a string from the user


printf("Enter a string: ");
gets(str);

// Reverse the string


strcpy(revstr, str);
strrev(revstr);

// Concatenate the original string with its reverse


strcpy(catstr, str);
strcat(catstr, revstr);

// Find the length of the original string


lenstr = strlen(str);

// Find the length of the concatenated string


lencatstr = strlen(catstr);
// Display the results
printf("Original string: %s\n", str);
printf("Reversed string: %s\n", revstr);
printf("Concatenated string: %s\n", catstr);
printf("Length of original string: %d\n", lenstr);
printf("Length of concatenated string: %d\n", lencatstr);

return 0;
}

//4. Write a program to read a string and find the length of a string without using built in
function

#include <stdio.h>

int main() {
char str[100];
int length;

// Read a string from the user


printf("Enter a string: ");
gets(str);

// Calculate the length of the string


length = 0;
while (str[length] != '\0') {
length++;
}

// Display the length of the string


printf("Length of the string: %d\n", length);

return 0;
}

//5. Write a program to generate and display first n values in the Fibonacci sequence using
recursive function

#include <stdio.h>

// Function to calculate Fibonacci number using recursion


int fibonacci(int n) {
if (n == 1) {
return 0;
}else if (n == 2) {
return 1;
}
else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}

int main() {
int n, i;

// Read the number of Fibonacci values to generate


printf("Enter the number of Fibonacci values to generate: ");
scanf("%d", &n);

// Display the first n Fibonacci values


printf("First %d values in the Fibonacci sequence:\n", n);
for (i = 1; i <= n; i++) {
printf("%d ", fibonacci(i));
}
printf("\n");

return 0;
}

//6. Write a recursive function calculate factorial of a given integer, n.

#include <stdio.h>

// Recursive function to calculate factorial


int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}

int main() {
int number;

// Read an integer from the user


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

// Calculate and display the factorial


printf("Factorial of %d is %d\n", number, factorial(number));

return 0;
}
//7. Write a program to read elements in a square matrix, display it in the form of a matrix
and find and print its trace

#include <stdio.h>

int main() {
int n, i, j;

// Read the size of the square matrix


printf("Enter the size of the square matrix (n): ");
scanf("%d", &n);

int matrix[n][n];

// Read elements into the matrix


printf("Enter the elements of the matrix:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
}
}

// Display the matrix


printf("The matrix is:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}

// Calculate the trace of the matrix


int trace = 0;
for (i = 0; i < n; i++) {
trace += matrix[i][i];
}

// Print the trace


printf("The trace of the matrix is: %d\n", trace);

return 0;
}
//8. Write a program to read two matrices and perform addition and subtraction on them

#include <stdio.h>

int main() {

int rows, cols, i, j;


int matrix1[10][10], matrix2[10][10];
int sum[10][10], diff[10][10];

// Read the size of the matrices


printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);

// Read elements of the first matrix


printf("Enter elements of the first matrix:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
scanf("%d", &matrix1[i][j]);
}
}

// Read elements of the second matrix


printf("Enter elements of the second matrix:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
scanf("%d", &matrix2[i][j]);
}
}

// Perform addition and subtraction


for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
diff[i][j] = matrix1[i][j] - matrix2[i][j];
}
}

// Display the sum of the matrices


printf("Sum of the matrices:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
printf("%d ", sum[i][j]);
}
printf("\n");
}

// Display the difference of the matrices


printf("Difference of the matrices:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
printf("%d ", diff[i][j]);
}
printf("\n");
}

return 0;
}

//9. Write a program to read, display and multiply two m x n matrices using functions.

#include <stdio.h>

// Function prototypes
void readMatrix(int matrix[][10], int rows, int cols);
void displayMatrix(int matrix[][10], int rows, int cols);
void multiplyMatrices(int matrix1[][10], int matrix2[][10], int result[][10], int rows1, int cols1, int
rows2, int cols2);

int main() {
int rows1, cols1, rows2, cols2;

// Read the dimensions of the first matrix


printf("Enter the number of rows and columns of the first matrix: ");
scanf("%d %d", &rows1, &cols1);

// Read the dimensions of the second matrix


printf("Enter the number of rows and columns of the second matrix: ");
scanf("%d %d", &rows2, &cols2);

// Check if matrix multiplication is possible


if (cols1 != rows2) {
printf("Matrix multiplication is not possible.\n");
return 1;
}

int matrix1[10][10], matrix2[10][10], result[10][10] = {0};

// Read the elements of the first matrix


printf("Enter elements of the first matrix:\n");
readMatrix(matrix1, rows1, cols1);

// Read the elements of the second matrix


printf("Enter elements of the second matrix:\n");
readMatrix(matrix2, rows2, cols2);

// Multiply the matrices


multiplyMatrices(matrix1, matrix2, result, rows1, cols1, rows2, cols2);

// Display the matrices and the result


printf("First matrix:\n");
displayMatrix(matrix1, rows1, cols1);

printf("Second matrix:\n");
displayMatrix(matrix2, rows2, cols2);

printf("Resultant matrix after multiplication:\n");


displayMatrix(result, rows1, cols2);

return 0;
}

// Function to read a matrix


void readMatrix(int matrix[][10], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
}
}
}

// Function to display a matrix


void displayMatrix(int matrix[][10], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}

// Function to multiply two matrices


void multiplyMatrices(int matrix1[][10], int matrix2[][10], int result[][10], int rows1, int cols1, int
rows2, int cols2) {
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
for (int k = 0; k < cols1; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
}

//10. Write a program to check and display if a number is prime by defining isprime( )
function

#include <stdio.h>

// Function to check if a number is prime


int isPrime(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 number;

// Read a number from the user


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

// Check if the number is prime and display the result


if (isPrime(number)) {
printf("%d is a prime number.\n", number);
} else {
printf("%d is not a prime number.\n", number);
}

return 0;
}

//11. Write a program using functions that takes in three arguments - a start temperature (in
Celsius), an end temperature (in Celsius) and a step size. Print out a table that goes from the
start temperature to the end temperature, in steps of the step size; converting each Celsius to
Fahrenheit.

#include <stdio.h>

// Function prototypes
void printTemperatureTable(int start, int end, int step);
float celsiusToFahrenheit(float celsius);

int main() {
int startTemp, endTemp, stepSize;

// Read the start temperature, end temperature, and step size from the user
printf("Enter the start temperature (in Celsius): ");
scanf("%d", &startTemp);
printf("Enter the end temperature (in Celsius): ");
scanf("%d", &endTemp);
printf("Enter the step size: ");
scanf("%d", &stepSize);

// Print the temperature table


printTemperatureTable(startTemp, endTemp, stepSize);
return 0;
}

// Function to print the temperature table


void printTemperatureTable(int start, int end, int step) {
printf("Celsius\tFahrenheit\n");
for (int celsius = start; celsius <= end; celsius += step) {
printf("%d\t%.2f\n", celsius, celsiusToFahrenheit(celsius));
}
}

// Function to convert Celsius to Fahrenheit


float celsiusToFahrenheit(float celsius) {
return (celsius * 9.0 / 5.0) + 32;
}

//12. Write a C program to create array of structure which stores Roll No, Name and Average
marks of students. Accept n students data and print it in proper format.

#include <stdio.h>

// Define a structure to store student details


struct Student {
int rollNo;
char name[50];
float avgMarks;
};

int main() {

// Declare an array of structures to store student data


struct Student students[100];

int n;

// Read the number of students


printf("Enter the number of students: ");
scanf("%d", &n);

// Read data for each student


for (int i = 0; i < n; i++) {
printf("Enter details for student %d:\n", i + 1);

printf("Roll No: ");


scanf("%d", &students[i].rollNo);

printf("Name: ");
scanf("%s", &students[i].name);

printf("Average Marks: ");


scanf("%f", &students[i].avgMarks);
}

// Display student data


printf("\nStudent Details:\n");
printf("Roll No\tName\t\tAverage Marks\n");
printf("----------------------------------------\n");
for (int i = 0; i < n; i++) {
printf("%d\t%s\t\t%.2f\n", students[i].rollNo, students[i].name, students[i].avgMarks);
}

return 0;
}

//13. Write a C program to illustrate difference between structure and union by defining
emp_no ,emp_name, salary as members and display the size of the defined structure

#include <stdio.h>
#include <string.h>

// Define a structure to store employee details


struct EmployeeStruct {
int emp_no;
char emp_name[50];
float salary;
};

// Define a union to store employee details


union EmployeeUnion {
int emp_no;
char emp_name[50];
float salary;
};

int main() {
// Declare variables of structure and union
struct EmployeeStruct empStruct;
union EmployeeUnion empUnion;

// Assign values to structure members


empStruct.emp_no = 1;
strcpy(empStruct.emp_name, "John Doe");
empStruct.salary = 50000.0;

// Assign values to union members


empUnion.emp_no = 2;
strcpy(empUnion.emp_name, "Jane Doe");
empUnion.salary = 60000.0;

// Display the sizes of structure and union


printf("Size of Employee structure: %lu bytes\n", sizeof(empStruct));
printf("Size of Employee union: %lu bytes\n", sizeof(empUnion));

// Display the members of structure


printf("\nStructure Employee Details:\n");
printf("Employee Number: %d\n", empStruct.emp_no);
printf("Employee Name: %s\n", empStruct.emp_name);
printf("Employee Salary: %.2f\n", empStruct.salary);

// Display the members of union


printf("\nUnion Employee Details:\n");
printf("Employee Number: %d\n", empUnion.emp_no);
printf("Employee Name: %s\n", empUnion.emp_name);
printf("Employee Salary: %.2f\n", empUnion.salary);

return 0;
}

You might also like