Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Page- 1

II Semester

Course 3: Problem Solving using C, LAB Manuals

List of Experiments

1. A. Write a program to calculate simple & compound interest

B. Write a C program to interchange two numbers.

2. Find the biggest of three numbers using C.

3. Write a c program to find the sum of individual digits of a positive integer.

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.

5. Write a c program to check whether a number is Armstrong or not.

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.

8. Write a program for concatenation of two strings.

9. Write a program for length of a string with and without String Handling functions

10. Write a Program to find GCD of Two numbers using Recursion

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.

QUESTIONS AND ANSWERS

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

Enter the first, second and third numbers


12
34
23
The largest number is: 34

2. Write a program to swap two numbers without using temporary variable.

#include <stdio.h>
#include <conio.h>
void main ()

{
int num 1, num2;
clrscr ();
printf("\n Enter the first number: ");
scanf("%d", &numl);

printf("\n Enter the second number: ");


scanf("%d", &num2);
Page- 3

num1=num1 + num 2;
num2 = num1-num 2;
num1=num1- num 2;

printf("\n The first number is: %d", num1);


printf("\n The second number is: %d", num2);
getch();

Output

Enter the first number: 3


Enter the second number: 5
The first number is 5
The second number is 3

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.

4. Write a program to generate Fibonacci series of a given number?

# 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

6. Write a program to print below N Prime Numbers?

#include <stdio.h>
#include <conio.h>
void main()
{

int i, j, k, n, count=0;
clrscr();
printf("Input N value");
scanf("%d",&n);

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


{
count = 0;
for(j=1;j<=i;j++)
Page- 5

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);

printf("\nEnter Second String:");


gets(str2);
while(str1[i]!='\0')
i++;
while(str2[j]!='\0')
{
str1[i]=str2[j];
j++;
i++;
}
str1[i]='\0';
printf("\nConcatenated String is %s", str1);
}

Output:
Enter First String: Hello
Enter Second String: India
Concatenated String is HelloIndia

9. Write a program to find the length of a string .

#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++;
}

printf("\n The length of the string is: %d", i);


getch();
}

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("Enter Aadhar Number");


scanf("%ld",&emp[i].aadharno);

printf("Enter employee Name");


scanf("%s", emp[i].title);

printf("Enter employee joined date");


scanf("%s",emp[i].jdate);

printf("Enter employee salary");


scanf("%ld",&emp[i].salary);

printf("Enter employee date of birth");


scanf("%s", &emp[i].dob);

printf("Enter employee gender");


scanf("%s", &emp[i].gender);

printf("Enter department name");


scanf("%s", &emp[i].dept);
}

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

emp[i].salary, emp[i].dob, emp[i].gender,emp[i].dept);


}

getch();
}

Output:

You might also like