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

Project@C

The document contains a series of programming questions and solutions related to basic C programming concepts, including temperature conversion, sorting numbers, displaying odd numbers, generating mathematical tables, and printing patterns. It also covers topics like Fibonacci sequence, checking for palindromes, computing grades, and performing matrix operations. Each question is accompanied by a complete C code implementation demonstrating the solution.

Uploaded by

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

Project@C

The document contains a series of programming questions and solutions related to basic C programming concepts, including temperature conversion, sorting numbers, displaying odd numbers, generating mathematical tables, and printing patterns. It also covers topics like Fibonacci sequence, checking for palindromes, computing grades, and performing matrix operations. Each question is accompanied by a complete C code implementation demonstrating the solution.

Uploaded by

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

Name: - Anant A Kore

Class- BCA 1 Roll: - 17 UNIT-II


Q 1. Converting degrees Celsius to Fahrenheit and vice versa?
#include <stdio.h>
int main() {
float temperatur, convert;
int choice;
printf("Temperatur Conversion\n");
printf("1. Celsius to Fahrenheit\n");
printf("2. Fahrenheit to Celsius\n");
printf("Enter your choice (1 or 2): ");
scanf("%d", &choice);
switch (choice){
case 1:
printf("Enter temperatur in Celsius: ");
scanf("%f", &temperatur);
convert = (temperatur * 9 / 5) + 32;
printf("%.2f Celsius = %.2f Fahrenheit\n", temperatur, convert);
break;
case 2:
printf("Enter temperatur in Fahrenheit: ");
scanf("%f", &temperatur);
convert = (temperatur - 32) * 5 / 9;
printf("%.2f Fahrenheit = %.2f Celsius\n", temperatur, convert);
break;
default:
printf("Invalid choice.\n");
break;}
return 0;
}
Output:
BCA 1 Roll: - 17

Q 2. Display three input numbers in sorted (non-decreasing) order?


#include <stdio.h>
int main() {
int a, b, c, temp;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a > b) {
temp = a;
a = b;
b = temp;
}
if (a > c) {
temp = a;
a = c;
c = temp;
}
if (b > c) {
temp = b;
b = c;
c = temp;
}
printf("Sorted numbers: %d, %d, %d", a, b, c);
return 0;
}
Output:
BCA 1 Roll: - 17

Q 3. Given an input positive integer number, display odd numbers


from in therange [1,n]?
#include <stdio.h>
int main() {
int n, i;
printf("Enter a positive integer: ");
scanf("%d", &n);

printf("Odd numbers from 1 to %d are:\n", n);


for (i = 1; i <= n; i += 2) {
printf("%d ", i);
}
printf("\n");
return 0;
}

Output:
BCA 1 Roll: - 17

Q 4. Display first mathematical tables, each table up to 10 rows?


Generalise this todisplay first n (> 0) mathematical tables up to
m (m > 0) rows?
#include <stdio.h>
int main() {
int n, i, j;
printf("Enter the number of tables to display : ");
scanf("%d", &n);

for (i = 1; i <= 10; i++) {


for (j = 1; j <= n; j++) {
printf("%d\t", i * j);
}
printf("\n");
}
return 0;
}

Output:
BCA 1 Roll: - 17

Q 5. Display following patterns

1. #include<stdio.h> 2. #include<stdio.h>
int main(){ int main(){
int n; int n;
printf("Enter n: "); printf("Enter n: ");
scanf("%d",&n); scanf("%d",&n);
for(int i=1; i<=n ; i++){ for(int i=1 ; i<=n ; i++){
for(int j=1 ; j<=i ; j++){ for(int k=1 ; k<=n-i ; k++){
printf("$ "); printf(" ");
} }
printf("\n"); for(int j=1 ; j<=i ; j++){
} printf("$ ");
return 0; }
} printf("\n");
Output: }
return 0;}
3.#include<stdio.h>
int main(){ 4.#include<stdio.h>

int n; int main(){

printf("Enter n: "); int n;

scanf("%d",&n); printf("Enter n: ");

for(int i=1 ; i<=n ; i++){ scanf("%d",&n);


for(int j=1 ; j<=n-i+1 ; j++){ for(int i=n ; i>=1 ; i--) {
printf("%d ", j); for(int k=0 ; k<n-i ;k++){
} printf(" ");
printf("\n"); }
} for(int j=1 ; j<=i ; j++){
return 0; printf("%d ", j);
} }
printf("\n");
Output:
}
return 0;
}
Output:
BCA 1 Roll: - 17

Q 6. Print following patterns

1. #include <stdio.h>
int main(){
int n; Output:

printf("Enter n: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++){
if (i == 1 || i == n || j == 1 || j == n)
printf("* ");
else
printf(" ");
}
printf("\n");
}
return 0;
}
2. #include <stdio.h> 3. #include <stdio.h>
int main() { int main() {
int n; int n;
printf("Enter n: "); printf("Enter n: ");
scanf("%d",&n); scanf("%d",&n);
for (int i = 1; i <= n; i++) { for (int i = 0; i < n; i++) {
for(int k=1 ; k<=n-i ; k++){ for (int j = 0; j < n; j++) {
printf(" "); if (i==0 || i==n-1 || j==0 || j==n-1 || i==j ||
} j==n-i-1){
for (int j = 1; j <= i; j++) { printf("* ");
printf("%d", j); } else {
} printf(" ");
for (int j = i - 1; j >= 1; j--) { }
printf("%d", j); }
} printf("\n");
printf("\n"); }
} return 0;
return 0; }
}
Output:
Output:
4. #include <stdio.h> Output:
int main() {
int n;
printf("Enter n: ");
scanf("%d",&n);
for (int i = 1; i <= n; i++) {
for (int j = i; j < n; j++) {
printf(" ");
}
for (int j = 1; j <= (2*i-1); j++) {
printf("*");
}
printf("\n");
}
for (int i = n-1; i >= 1; i--) {
for (int j = n; j > i; j--) {
printf(" ");
}
for (int j = 1; j <= (2*i-1); j++) {
printf("*");
}
printf("\n");
}
return 0;
}
BCA 1 Roll: - 17

Q 7. Display the first n (n > 0) terms of the fibonacci sequence?

#include <stdio.h>
int main() {
int n, t1 = 0, t2 = 1, nextTerm;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Sequence: ");
for (int i = 1; i <= n; ++i) {
printf("%d ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return 0;
}

Output:
UNIT-III
Q 1. Extract digits of an integer number (left to right and right to
left)?
#include <stdio.h> right to left:
int main() { #include <stdio.h>
int num, temp, length = 0; int main() {
printf ("Enter an integer: "); int num;
scanf ("%d", &num);
temp = num; printf("Enter an integer: ");
while (temp != 0) { scanf("%d", &num);
length++;
temp /= 10; printf("Digits from right to left: ");
} while (num != 0) {
int digits[length]; printf("%d ", num % 10);
temp = num; num /= 10;
for (int i = length - 1; i >= 0; i--) { }
digits[i] = temp % 10; return 0;
temp /= 10; }
} Output:
printf ("Digits from left to right: ");
for (int i = 0; i < length; i++) {
printf ("%d ", digits[i]);
}
return 0;

}
Output:
BCA 1 Roll: - 17

Q 2. Check if a given positive integer number is a palindrome or


not?
#include <stdio.h>
int main() {
int num, reversedNum = 0, remainder, originalNum;
printf("Enter an integer: ");
scanf("%d", &num);
originalNum = num;
while (num != 0) {
remainder = num % 10;
reversedNum = reversedNum * 10 + remainder;
num /= 10;
}
if (originalNum == reversedNum) {
printf("%d is a palindrome.\n", originalNum);
} else {
printf("%d is not a palindrome.\n", originalNum);
}
return 0;
}Output:
BCA 1 Roll: - 17

Q 3. Compute character grade from the marks (0 ≤ marks ≤ 100) of


a subject. Grading Scheme: 80-100 : A, 60 - 79: B, 50 - 59: C, 40-49:
D, 0-39: F? Solve this using both else-if ladder and switch case?
#include <stdio.h>
int main() {
int marks;
char grade;
printf("Enter the marks (0-100): ");
scanf("%d", &marks);
if (marks >= 80 && marks <= 100) {
grade = 'A';
} else if (marks >= 60 && marks <= 79) {
grade = 'B';
} else if (marks >= 50 && marks <= 59) {
grade = 'C';
} else if (marks >= 40 && marks <= 49) {
grade = 'D';
} else if (marks >= 0 && marks <= 39) {
grade = 'F';
} else {
printf("Invalid marks entered.\n");
return 1;
}
printf("The grade is: %c\n", grade);
return 0;
}Output:
BCA 1 Roll: - 17

Q 7. Compute prime factors of a positive integer number?


#include <stdio.h>
int main() {
int num, i;
printf("Enter a positive integer: ");
scanf("%d", &num);
printf("Prime factors of %d are: ", num);
while (num % 2 == 0) {
printf("2 ");
num /= 2;
}
for (i = 3; i * i <= num; i += 2) {
while (num % i == 0) {
printf("%d ", i);
num /= i;
}
}
if (num > 2) {
printf("%d", num);
}
printf("\n");
return 0;
}Output:
BCA 1 Roll: - 17

Q 9. Check if a given positive integer number is a perfect number or


not?
#include <stdio.h>
int main() {
int num, sum = 0;

printf("Enter a positive integer: ");


scanf("%d", &num);

for (int i = 1; i <= num / 2; i++) {


if (num % i == 0) {
sum += i;
}
}
if (sum == num) {
printf("%d is a perfect number.\n", num);
} else {
printf("%d is not a perfect number.\n", num);
}
return 0;
}Output:
UNIT IV
Q 1. Design a modularized algorithm/program to compute a
maximum of 8 numbers?
#include <stdio.h>
int findMax(int numbers[], int size) {
int max = numbers[0];
for (int i = 1; i < size; i++) {
if (numbers[i] > max) {
max = numbers[i];
}
}
return max;
}
int main() {
int numbers[8], size;
printf("Enter the number of elements (1 to 8): ");
scanf("%d", &size);
if (size > 0 && size <= 8) {
printf("Enter %d numbers: ", size);
for (int i = 0; i < size; i++) {
scanf("%d", &numbers[i]);
}
printf("The maximum number is: %d\n", findMax(numbers, size));
} else {
printf("Invalid number of elements.\n");
}
return 0;
}
Output:
BCA 1 Roll: - 17

Q 2. Implement your own string length and string reversal


functions?
String Length Function:
#include <stdio.h>
int my_strlen(const char *str) {
int length = 0;
while (str[length] != '\0') {
length++;
}
return length;
}
int main() {
char str[100];

printf("Enter a string: ");


fgets(str, sizeof(str), stdin);
str[my_strlen(str) - 1] = '\0';

int length = my_strlen(str);


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

return 0;
}
Output:
String Reversal Function:
#include <stdio.h>
int my_strlen(const char *str) {
int length = 0;
while (str[length] != '\0') length++;
return length;
}
void my_strrev(char *str) {
int start = 0, end = my_strlen(str) - 1;
while (start < end) {
char temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
}
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
str[my_strlen(str) - 1] = '\0';
printf("Original string: %s\n", str);
my_strrev(str);
printf("Reversed string: %s\n", str);
return 0;
}
Output:
BCA 1 Roll: - 17

Q 3. Design algorithm/program to perform matrix operations


addition, subtractionand transpose?
#include <stdio.h>
void readMatrix(int rows, int cols, int matrix[rows][cols]) {
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
scanf("%d", &matrix[i][j]);
}
void printMatrix(int rows, int cols, int matrix[rows][cols]) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++)
printf("%d ", matrix[i][j]);
printf("\n");
}
}
void addMatrices(int rows, int cols, int m1[rows][cols], int m2[rows][cols], int
result[rows][cols]) {
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
result[i][j] = m1[i][j] + m2[i][j];
}
void subtractMatrices(int rows, int cols, int m1[rows][cols], int m2[rows][cols], int
result[rows][cols]) {
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
result[i][j] = m1[i][j] - m2[i][j];
}
void transposeMatrix(int rows, int cols, int matrix[rows][cols], int result[cols][rows]) {
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
result[j][i] = matrix[i][j];
}
int main() {
int rows, cols;
printf("Enter the number of rows and columns: ");
scanf("%d%d", &rows, &cols);

int matrix1[rows][cols], matrix2[rows][cols];


int result[rows][cols], transpose[cols][rows];

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


readMatrix(rows, cols, matrix1);
printf("Enter elements of second matrix:\n");
readMatrix(rows, cols, matrix2);

addMatrices(rows, cols, matrix1, matrix2, result);


printf("Addition:\n");
printMatrix(rows, cols, result);

subtractMatrices(rows, cols, matrix1, matrix2, result);


printf("Subtraction:\n");
printMatrix(rows, cols, result);

transposeMatrix(rows, cols, matrix1, transpose);


printf("Transpose of first matrix:\n");
printMatrix(cols, rows, transpose);

return 0;
}
Output:

You might also like