Practical-1
Practical-1
Practical-1
a prime number.
Answer:
#include <stdio.h>
void main() {
int n, i, isPrime = 1;
printf("Enter a number: ");
scanf("%d", &n);
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);
}
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);
}
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));
}
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");
}
}
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);
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);
}
#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);
}
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);
}
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);
}
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);
}
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);
}
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");
}