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

Practical 1

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)
29 views12 pages

Practical 1

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/ 12

Q1: Write a program to check if a given number is

a prime number.

Answer:

#include <stdio.h>
void main() {
int n, i, isPrime = 1;
printf("Enter a number: ");
scanf("%d", &n);

for (i = 2; i <= n / 2; i++) {


if (n % i == 0) {
isPrime = 0;
break;
}
}
if (n <= 1) isPrime = 0;

if (isPrime)
printf("%d is a prime number.\n", n);
else
printf("%d is not a prime number.\n", n);
}
Q2: Write a program to calculate the sum of the
digits of a given number.

Answer:
// Program 2: Calculate the sum of digits of a number

#include <stdio.h>
void main() {
int n, sum = 0;
printf("Enter a number: ");
scanf("%d", &n);
while (n != 0) {
sum += n % 10;
n /= 10;
}
printf("Sum of digits: %d\n", sum);
}

Q3: Write a program to find the sum of all even


digits in a given number.

Answer:
// Program 3: Sum of even digits of a number

#include <stdio.h>
void main() {
int n, digit, sum = 0;
printf("Enter a number: ");
scanf("%d", &n);
while (n != 0) {
digit = n % 10;
if (digit % 2 == 0) sum += digit;
n /= 10;
}
printf("Sum of even digits: %d\n", sum);
}

Q4: Write a program to display the size of different


data types in C.

Answer:
// Program 4: Display size of different data types

#include <stdio.h>
void main() {
printf("Size of int: %d bytes\n", (int)sizeof(int));
printf("Size of float: %d bytes\n", (int)sizeof(float));
printf("Size of double: %d bytes\n", (int)sizeof(double));
printf("Size of char: %d bytes\n", (int)sizeof(char));
}

Q5: Write a program to print a right-angled


triangle pattern of asterisks.

Answer:
// Program 5: Print a pattern (e.g., triangle)
#include <stdio.h>
void main() {
int n, i, j;
printf("Enter the number of rows: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
for (j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
}

Q6: Write a program to calculate the factorial of a


number using recursion.

Answer:
// Program 6: Factorial for loop

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

printf("Fibonacci Series: %d, %d", t1, t2);


for (i = 3; i <= n; i++) {
nextTerm = t1 + t2;
printf(", %d", nextTerm);
t1 = t2;
t2 = nextTerm;
}
printf("\n");
}

Q7: Write a program to calculate the sum of 'n'


numbers stored in an array.

Answer:
// Program 7: Sum of n numbers using an array

#include <stdio.h>
void main() {
int n, i, sum = 0;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
sum += arr[i];
}
printf("Sum of elements: %d\n", sum);
}
Q8: Write a program to demonstrate the use of
structures by storing and displaying student
details.

Answer:
// Program 8: Using structures

#include <stdio.h>
struct Student {
char name[50];
int age;
float marks;
};
void main() {
struct Student s;
printf("Enter name: ");
scanf("%s", s.name);
printf("Enter age: ");
scanf("%d", &s.age);
printf("Enter marks: ");
scanf("%f", &s.marks);
printf("Name: %s, Age: %d, Marks: %.2f\n", s.name, s.age,
s.marks);
}

Q9: Write a program to demonstrate the use of


unions by storing and displaying data in different
formats.
Answer:
// Program 9: Basic union example

#include <stdio.h>
#include <string.h>
union Data {
int i;
float f;
char str[20];
};
void main() {
union Data data;
data.i = 10;
printf("Data as integer: %d\n", data.i);
data.f = 220.5;
printf("Data as float: %f\n", data.f);
strcpy(data.str, "Hello");
printf("Data as string: %s\n", data.str);
}

Q10: Write a program to reverse a given number.

Answer:
// Program 10: Reverse a number

#include <stdio.h>
void main() {
int n, reversed = 0;
printf("Enter a number: ");
scanf("%d", &n);
while (n != 0) {
reversed = reversed * 10 + n % 10;
n /= 10;
}
printf("Reversed number: %d\n", reversed);
}

Q11: Write a program to calculate x raised to the


power y.

Answer:
// Program 11: Calculate the power of a number

#include <stdio.h>
void main() {
int x, y, result = 1;
printf("Enter base (x): ");
scanf("%d", &x);
printf("Enter exponent (y): ");
scanf("%d", &y);
for (int i = 0; i < y; i++) {
result *= x;
}
printf("%d raised to the power %d is %d\n", x, y, result);
}
Q12: Write a program to find the GCD of two
numbers using loops.

Answer:
// Program 12: Find GCD of two numbers

#include <stdio.h>
void main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
while (a != b) {
if (a > b)
a -= b;
else
b -= a;
}
printf("GCD is %d\n", a);
}

Q13: Write a program to count the number of


vowels in a given string.

Answer:
// Program 13: Count vowels in a string

#include <stdio.h>
void main() {
char str[100];
int count = 0;
printf("Enter a string: ");
scanf("%s", str);
for (int i = 0; str[i] != '\0'; i++) {
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') {
count++;
}
}
printf("Number of vowels: %d\n", count);
}

Q14: Write a program to check if a given number is


an Armstrong number.

Answer:
// Program 14: Check if a number is an Armstrong
number

#include <stdio.h>
void main() {
int n, original, remainder, result = 0;
printf("Enter a number: ");
scanf("%d", &n);
original = n;
while (original != 0) {
remainder = original % 10;
result += remainder * remainder * remainder;
original /= 10;
}
if (result == n)
printf("%d is an Armstrong number\n", n);
else
printf("%d is not an Armstrong number\n", n);
}

Q15: Write a program to print the Fibonacci series


up to n terms.

Answer:
// Program 15: Print Fibonacci series

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

You might also like