0% found this document useful (0 votes)
16 views13 pages

Practical 3

Uploaded by

rishiikumar2546
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views13 pages

Practical 3

Uploaded by

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

JIGYASA UNIVERSITY, DEHRADUN - 11

C Programming Practical

Name - RISHI KUMAR

ENROLLMENT NO. – 2024BTH001

COURSE – B. Tech. CS (First Semester)

SUBMITTED TO – Mr. SACHIN JAIN


Q1:
Write a C program to calculate the sum of two
numbers.

Answer:
#include <stdio.h>

int main() {
int a = 5, b = 3;
int sum = a + b;
printf("Sum: %d", sum);
return 0;
}

Q2:
Write a C program to print the multiplication table of
7.

Answer:
#include <stdio.h>

int main() {
int num = 7;
for (int i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", num, i, num * i);
}
return 0;
}

Q3:
Write a C program to display all prime numbers
between 1 and 100.

Answer:
#include <stdio.h>

int main() {
int i, j, flag;
for (i = 2; i <= 100; i++) {
flag = 0;
for (j = 2; j <= i/2; j++) {
if (i % j == 0) {
flag = 1;
break;
}
}
if (flag == 0) {
printf("%d ", i);
}
}
return 0;
}

Q4:
Write a C program to find the largest of three
numbers using a function.

Answer:
#include <stdio.h>

int largest(int a, int b, int c) {


if (a > b && a > c) return a;
if (b > a && b > c) return b;
return c;
}

int main() {
int a = 12, b = 25, c = 19;
int result = largest(a, b, c);
printf("Largest number: %d", result);
return 0;
}

Q5:
Write a C program to reverse a number.
Answer:
#include <stdio.h>

int main() {
int num = 12345, reverse = 0;
while (num != 0) {
reverse = reverse * 10 + num % 10;
num /= 10;
}
printf("Reversed number: %d", reverse);
return 0;
}

Q6:
Write a C program to print the following pattern:
*

**

***

****

*****

Answer:
#include <stdio.h>

int main() {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
return 0;
}

Q7:
Write a C program using structures to store and
display the name and marks of a student.

Answer:
#include <stdio.h>

struct Student {
char name[50];
int marks;
};

int main() {
struct Student student1;
// You can manually assign values as no input method is
allowed
// student1.marks = 85; // Example values for marks
// strcpy(student1.name, "John Doe"); // Manually
assigning a name

printf("Student name: %s\n", student1.name);


printf("Marks: %d", student1.marks);
return 0;
}

Q8:
Write a C program to demonstrate a union and
display its size.

Answer:
#include <stdio.h>

union Data {
int i;
float f;
char str[20];
};

int main() {
union Data data;
printf("Size of union: %zu bytes\n", sizeof(data));
return 0;
}

Q9:
Write a C program to check if a number is even or
odd.

Answer:
#include <stdio.h>

int main() {
int num = 4;
if (num % 2 == 0) {
printf("Even");
} else {
printf("Odd");
}
return 0;
}

Q10:
Write a C program to calculate the factorial of 5 using
a loop.

Answer:
#include <stdio.h>

int main() {
int num = 5, fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
printf("Factorial: %d", fact);
return 0;
}

Q11:
Write a C program to display the Fibonacci series up
to 10 terms.

Answer:
#include <stdio.h>

int main() {
int n = 10, t1 = 0, t2 = 1, nextTerm;
printf("Fibonacci Series: ");
for (int i = 1; i <= n; i++) {
printf("%d ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return 0;
}

Q12:
Write a C program to calculate the sum of digits of the
number 12345.

Answer:
#include <stdio.h>

int main() {
int num = 12345, sum = 0;
while (num != 0) {
sum += num % 10;
num /= 10;
}
printf("Sum of digits: %d", sum);
return 0;
}

Q13:
Write a C program to check if the number 121 is a
palindrome.

Answer:
#include <stdio.h>

int main() {
int num = 121, reversed = 0, original = num;
while (num != 0) {
reversed = reversed * 10 + num % 10;
num /= 10;
}
if (original == reversed) {
printf("Palindrome");
} else {
printf("Not Palindrome");
}
return 0;
}

Q14:
Write a C program to find the area of a circle using a
function.

Answer:
#include <stdio.h>

float areaOfCircle(float radius) {


return 3.14 * radius * radius;
}

int main() {
float radius = 5;
printf("Area: %.2f", areaOfCircle(radius));
return 0;
}

Q15:
Write a C program to find the minimum number in an
array.

Answer:
#include <stdio.h>

int main() {
int arr[] = {3, 7, 1, 9, 2};
int min = arr[0];
for (int i = 1; i < 5; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
printf("Minimum number: %d", min);
return 0;
}

You might also like