C LAB Manual
C LAB Manual
II Semester
List of Experiments
4. A Fibonacci sequence is defined as follows: the first and second terms in the sequenceare O and 1. Subsequent
terms are found by adding the preceding two terms in the sequence.
6. Write a c program to generate all the prime numbers between 1 and n, where n is avalue supplied by the user.
7. Write a c program that uses functions to perform the following: Addition of twomatrices. Multiplication of two
matrices.
9. Write a program for length of a string with and without String Handling functions
11. Write a c program to read data of 10 employees with a structure of 1.employee id2.aadar no, 3.title, 4.joined
date, 5.salary, 6.date of birth, 7.gender, 8.department.
1(A). Write a program that calculates the Simple Interest and Compound Interest. The Principal,
Amount, Rate of Interest and Time are entered through the keyboard.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float p, r, t, a, si, ci;
clrscr();
printf("Enter Principle=");
scanf("%f",&p);
printf("Enter Rate=");
scanf("%f",&r);
printf("Enter Time=");
scanf("%f",&t);
Page- 2
si=(p*r*t)/100;
printf("Simple Interest-%f", si);
a = p*(pow((1+r/100), t));
ci = a - p;
printf("\nCompound Interest-%f",ci);
}
Output:
Enter Principle=100
Enter Rate=10
Enter Time-3
Simple Interest-30.000000
Compound Interest-33.100010
1(B). Write a program to find the largest of three numbers using the Ternary operator?
#include <stdio.h>
#include <conio.h>
void main ()
{
int num1, num2, num3, large;
clrscr();
printf("Enter the first, second and third numbers");
scanf("%d%d%d", &num1, &num2, &num3);
large =num1 > num2 ? (num1> num3 ? num1: num3): (num2>num3 ? num2: num3);
printf ("\n The largest number is: %d", large);
getch ();
}
Output
#include <stdio.h>
#include <conio.h>
void main ()
{
int num 1, num2;
clrscr ();
printf("\n Enter the first number: ");
scanf("%d", &numl);
num1=num1 + num 2;
num2 = num1-num 2;
num1=num1- num 2;
Output
3. Write a program to enter a number and then calculate the sum of its digits?
#include <stdio.h>
#include <conio.h>
void main()
{
int num, r, sum = 0;
clrscr();
printf("\n Enter the number: ");
scanf("%d", &num);
while (num != 0)
{
r = num%10;
sum = sum+r;
num = num/10;
}
printf("\n The sum of digits = %d", sum);
getch();
Output:
Enter the number : 123
The sum of digits =6.
# include <stdio.h>
#include <conio.h>
void main()
int a, b, c, n, i;
clrscr();
printf("Enter n value");
{
scanf ("%d",&n);
a = 0; b = 1;
printf("%d\t%d", a, b);
i=2;
while (i<=n)
{
Page- 4
c = a + b;
printf("\t%d", c);
a = b;
b = c;
i++;
}
Output:
Enter n value 10
011235 8 13 21 34
5. Write a program to find whether the given number is an armstrong number or not.
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
int num, sum=0, r, n;
clrscr();
printf("\n Enter the number: ");
scanf("%d", &num);
n=num;
while (n>0)
{
r=n%10;
sum+= pow (r, 3);
n=n/10;
}
if (sum==num)
printf("\n%d is an armstrong number", num);
else
printf("\n%d is not an armstrong number", num);
getch();
}
Output:
Enter the number: 153
153 is an armstrong number
#include <stdio.h>
#include <conio.h>
void main()
{
int i, j, k, n, count=0;
clrscr();
printf("Input N value");
scanf("%d",&n);
if (i % j ==0)
count++;
}
if (count = = 2)
printf("%d\t", i);
}
getch();
}
Output
Enter N value 10
2357
7. Write a c program that uses functions to perform the following: Addition of twomatrices.
Multiplication of two matrices.
for answer Refer your notebook
8. Write a program for concatenated two strings(Without using String handling functions).
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[25],str2[25];
int i=0,j=0;
clrscr();
printf("\nEnter First String:");
gets(str1);
Output:
Enter First String: Hello
Enter Second String: India
Concatenated String is HelloIndia
#include <stdio.h>
#include <conio.h>
void main ()
Page- 6
char str[100], i = 0;
clrscr();
printf("\n Enter the string:");
gets(str);
while(str[i] != '\0')
{
i++;
}
Output
Enter the string : HELLO
The length of the string is: 5
10. Write a C Program to find GCD of given two Numbers using Recursion .
#include <stdio.h>
#include<conio.h>
int ged(int, int);
int main()
{
int a, b, result;
clrscr();
printf("Enter the two numbers to find their GCD: ");
scanf("%d%d", &a, &b);
result = gcd(a, b);
printf("The GCD of %d and %d is: %d", a, b, result);
}
int gcd(int a, int b)
{ while (a != b)
{
if (a > b)
{
return gcd(a - b, b);
}
else
{
return gcd(a, b - a);
}
}
return a;
}
OUTPUT:
Enter the two numbers to find their GCD: 100 70
The GCD of 100 and 70 is: 10.
Page- 7
11. Write a c program to read data of 10 employees with a structure of 1. employee id, 2. aadar no, 3.
title, 4. joined date, 5. salary, 6. date of birth, 7. gender, 8. department.
#include<stdio.h>
#include<conio.h>
struct employee {
int empid;
long aadharno;
char title[30];
char jdate[20];
long salary;
char dob[20];
char gender[8];
char dept[30];
};
void main()
{
int n,i;
struct employee emp[10];
clrscr();
printf("Enter no.of emplyee details ");
scanf("%d",&n);
printf("Enter employee details\n");
for(i=0;i<n;i++)
{
printf("Enter employee id");
scanf("%d",&emp[i].empid);
printf("Empid\tAadharNo\tTitle\tJdate\tSalary\tDob\tGender\tDeptname\n");
for(i=0;i<n;i++)
{
printf("%d\t%ld\t%s\t%s\t%d\t%s\t%s\t%s\n",emp[i].empid,emp[i].aadharno, emp[i].title,emp[i]jdate,
Page- 8
getch();
}
Output: