0% found this document useful (0 votes)
5 views27 pages

C Programming Lab Assignments Solutions

The document contains a series of programming assignments in C, including tasks such as calculating the sum of a series, printing multiplication tables, generating Fibonacci series, checking for prime numbers, and finding GCD and LCM. Each assignment is accompanied by a code solution and explanations of the logic used. The document serves as a practical guide for students in a Computer Science course.

Uploaded by

Hansini
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)
5 views27 pages

C Programming Lab Assignments Solutions

The document contains a series of programming assignments in C, including tasks such as calculating the sum of a series, printing multiplication tables, generating Fibonacci series, checking for prime numbers, and finding GCD and LCM. Each assignment is accompanied by a code solution and explanations of the logic used. The document serves as a practical guide for students in a Computer Science course.

Uploaded by

Hansini
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

NAME – HANSINI

CSE CORE
LAB ASSIGNMENT – 03
25BECSE44

1
Q1. Write a program to add first seven terms of the following series
using a for loop:

1/1!+2/2!+3/3!+…….

Ans: #include <stdio.h>

int main()

int i, j, fact;

float sum = 0.0;

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

fact = 1;

for(j = 1; j <= i; j++)

fact = fact * j;

sum = sum + (float)i / fact;

printf("Sum of first 7 terms = %.4f\n", sum);

return 0;

2
3
[Link] a program to print the multiplication table of the number
entered by the user. Tl ble should get displayed in the following form

10 * 1 = 10

10*2=20

Ans:#include <stdio.h>

int main()

int n, i;

printf("Enter a number: ");

scanf("%d", &n);

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

printf("%d * %d = %d\n", n, i, n * i);

return 0;

4
5
Ans:#include <stdio.h>

int main()

int n = 5;

int i, j, space, num;

for(i = 0; i < n; i++)

for(space = 0; space < n - i - 1; space++)

printf(" ");

num = 1;

for(j = 0; j <= i; j++) {

printf("%4d", num);

num = num * (i - j) / (j + 1);

printf("\n");

return 0;

6
}

7
4. Write a Program to print Armstrong Series between 1 to 1000

#include <stdio.h>

#include <math.h>

int main()

int num, original, remainder, n, count;

printf("Armstrong numbers between 1 and 1000 are:\n");

for(num = 1; num <= 1000; num++)

original = num;

n = num;

count = 0;

while(n != 0)

n = n / 10;

count++;

n = num;

int sum =0;

while(n != 0)

8
{

remainder = n % 10;

sum += pow(remainder, count);

n = n / 10;

if(sum == original)

printf("%d ", original);

return 0;

9
5. Write a program to print The Fibonacci series (0, 1, 1, 2, 3, 5, 8, 13,
21, 38,...)

#include <stdio.h>

int main()

int n, i;

int a = 0, b = 1, c;

printf("Enter number of terms: ");

scanf("%d", &n);

printf("Fibonacci Series: ");

for(i = 1; i <= n; i++)

printf("%d ", a);

c = a + b;

a = b;

b = c;

return 0;

10
11
6. Implement a C program that checks if a given number is prime or not
using a loop and conditional statements

Ans: #include <stdio.h>

int main()

int n, i, flag = 0;

printf("Enter a number: ");

scanf("%d", &n);

if (n <= 1)

printf("%d is not a prime number.\n", n);

return 0;

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

if(n % i == 0) {

flag = 1;

break;

if(flag == 0)

12
printf("%d is a prime number.\n", n);

else

printf("%d is not a prime number.\n", n);

return 0;

13
7. Write a program to enter the numbers till the user wants and at the
end it should display

the count of positive, negative and zeros entered.

Ans:#include <stdio.h>

int main()

int num;

int positive = 0, negative = 0, zero = 0;

char choice;

do

printf("Enter a number: ");

scanf("%d", &num);

if (num > 0)

positive++;

else if (num < 0)

negative++;

else

zero++;

printf("Do you want to enter another number? (y/n): ");

14
scanf(" %c", &choice);

while (choice == 'y' || choice == 'Y');

printf("\nCount of positive numbers: %d", positive);

printf("\nCount of negative numbers: %d", negative);

printf("\nCount of zeros: %d\n", zero);

return 0;

15
8. Write a program that uses a loop to find the sum of all numbers from
1 to 100 but

terminates when the sum exceeds 1000.

Ans:#include <stdio.h>

int main()

int sum = 0;

int i;

for(i = 1; i <= 100; i++)

sum += i;

if(sum > 1000)

printf("Sum exceeded 1000 after adding %d\n", i);

break;

printf("Final sum: %d\n", sum);

16
return 0;

17
9. Write a program to find GCD (greatest common divisor or HCF) and
LCM (least common

multiple) of two numbers.

Ans:#include <stdio.h>

int gcd(int a, int b)

while (b != 0)

int temp = b;

b = a % b;

a = temp;

return a;

int lcm(int a, int b, int gcd_value)

return (a * b) / gcd_value;

int main()

18
{

int num1, num2;

int gcd_value, lcm_value;

printf("Enter two numbers: ");

scanf("%d %d", &num1, &num2);

gcd_value = gcd(num1, num2);

lcm_value = lcm(num1, num2, gcd_value);

printf("GCD (HCF) of %d and %d is: %d\n", num1, num2, gcd_value);

printf("LCM of %d and %d is: %d\n", num1, num2, lcm_value);

return 0;

19
10. Write a program to check whether a number is Palindrome or not.

Ans:#include <stdio.h>

int main()

int num, original, reversed = 0, remainder;

printf("Enter a number: ");

scanf("%d", &num);

original = num;

while (num != 0)

remainder = num % 10;

reversed = reversed * 10 + remainder;

num = num / 10;

if (original == reversed)

printf("%d is a palindrome.\n", original);

else

printf("%d is not a palindrome.\n", original);

return 0;

20
}

21
Ans: #include <stdio.h>

int main()

int i, j;

for(i = 1; i <= 5; i += 2)

for(j = 1; j <= i; j++)

printf("%d ", j);

printf("\n");

for(i = 3; i >= 1; i -= 2)

22
{

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

printf("%d ", j);

printf("\n");

return 0;

23
Ans: #include <stdio.h>

int main()

int i, j;

for(i = 1; i <= 5; i++)

for(j = 1; j <= i; j++)

printf("%d ", j);

printf("\n");

return 0;

24
25
Ans:#include <stdio.h>

int main()

int i, j, space;

int n = 5;

for(i = 1; i <= n; i++)

for(space = 1; space <= n-i; space++)

printf(" ");

for(j = 1; j <= 2*i-1; j++)

printf("*");

printf("\n");

for(i = n-1; i >= 1; i--)

26
for(space = 1; space <= n-i; space++)

printf(" ");

for(j = 1; j <= 2*i-1; j++)

printf("*");

printf("\n");

return 0;

27

You might also like