0% found this document useful (0 votes)
22 views7 pages

Cpds - Lab - Manual - Part1

The document contains multiple C programming tasks including finding the sum of individual digits, generating Fibonacci sequences, identifying prime numbers, calculating factorials, finding roots of quadratic equations, determining the GCD of two integers, performing basic arithmetic operations using a switch statement, and finding the largest and smallest numbers in an array. Each task is accompanied by a code snippet that demonstrates the implementation. Additionally, there is a mention of matrix operations, specifically addition and multiplication, which are to be performed using functions.
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)
22 views7 pages

Cpds - Lab - Manual - Part1

The document contains multiple C programming tasks including finding the sum of individual digits, generating Fibonacci sequences, identifying prime numbers, calculating factorials, finding roots of quadratic equations, determining the GCD of two integers, performing basic arithmetic operations using a switch statement, and finding the largest and smallest numbers in an array. Each task is accompanied by a code snippet that demonstrates the implementation. Additionally, there is a mention of matrix operations, specifically addition and multiplication, which are to be performed using functions.
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
You are on page 1/ 7

1.Write a C program to find the sum of individual digits of a positive integer.

Void main()
{
Int n,sum=0,r;
Clrscr();
Printf(“enter a number”);
Scanf(“%d”,&n);
While(n>0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
Printf(“Sum of individual digits=%d”,sum);
Getch();
}

2. Fibonacci sequence is defined as follows: the first and second terms in the sequence are 0 and
Subsequent terms are found by adding the preceding two terms in the sequence.
Write a C program to generate the first n terms of the sequence.

Void main()
{
Int f1=0,f2=1,f3,n;
Clrscr();
Printf(“enter the limit of sequence”);
Scanf(“%d”,&n);
Printf(“%d\t%d\t”,f1,f2);
While(n-2>0)
{
F3=f1+f2;
F1=f2;
F2=f3;
Peintf(“%d\t”,f3);
}
Getch();
}

3.Write a C program to generate all the prime numbers between 1 and n, where n is a value
supplied by the user.

Void main()
{
Int n,i,j;
Clrscr();
Printf(“enter the value of n”);
Scanf(“%d”,&n);
Printf(“prime numbers between 1 and %d are\n”,n);
For(i=2;i<=n;i++)
{
For(j=2;j<I;j++)
{
If(i%j==0)
Break;
}
If(i==j)
Printf(“%d\n”,i);
}
Getch();
}

4 Write a C program to find the factorial of a given integer.


Void main()
{
Int n,fact=1;
Clrscr();
Printf(“enter the number to which factorial to be found”);
Scanf(“%d”,&n);
While(n>0)
{
Fact=fact*n;
N=n-1;
}
Printf(“factorial=%d\n”,fact);
Getch();
}

5.Write a C program to find the roots of a quadratic equation.


#include <math.h>
#include <stdio.h>
int main() {
double a, b, c, discriminant, root1, root2, realPart, imagPart;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf", &a, &b, &c);
discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("root1 = %.2lf and root2 = %.2lf", root1, root2);
}
else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
printf("root1 = root2 = %.2lf;", root1);
}
else {
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imagPart, realPart, imagPart);
}
return 0;
}

6. Write a C program to find the GCD (greatest common divisor) of two given integers.

#include <stdio.h>
#include <conio.h>
int main()
{
// declare the variables
int n1, n2, i, GCD_Num;
printf ( " Enter any two numbers: \n ");
scanf ( "%d %d", &n1, &n2);
// use for loop
for( i = 1; i <= n1 && i <= n2; ++i)
{
if (n1 % i ==0 && n2 % i == 0)
GCD_Num = i; /* if n1 and n2 is completely divisible by i, the divisible number will
be the GCD_Num */
}
// print the GCD of two numbers
printf (" GCD of two numbers %d and %d is %d.", n1, n2, GCD_Num);
return 0;
}

7. Write a C program, which takes two integer operands and one operator from the user, performs
the operation and then prints the result. (Consider the operators +,-,*, /, % and use Switch
Statement)

#include <stdio.h>
#include <conio.h>
void main()
{
int a, b, c;
char ch;
clrscr() ;
printf("Enter your operator(+, -, /, *, %)\n");
scanf("%c", &ch);
printf("Enter the values of a and b\n");
scanf("%d%d", &a, &b);

switch(ch)
{
case '+': c = a + b;
printf("addition of two numbers is %d", c);
break;
case '-': c = a - b;
printf("substraction of two numbers is %d", c);
break;
case '*': c = a * b;
printf("multiplication of two numbers is %d", c);
break;
case '/': c = a / b;
printf("remainder of two numbers is %d", c);
break;
case '%': c = a % b;
printf("quotient of two numbers is %d", c);
break;
default: printf("Invalid operator");
break;
}
getch();
}

8. Write a C program to find both the largest and smallest number in a list of integers.

#include <stdio.h>
#include <conio.h>

void main()
{
int a[10], i, n, small, large;
clrscr();
printf("Enter The Array Size:");
scanf("%d", &n);
printf("ENTER ELEMENTS OF ARRAY");
for (i = 0; i < n; i++) // read the elements of an array
scanf("%d", &a[i]);
small = a[0];
large = a[0];
for (i = 0; i < n; i++) // read the elements of an array
{
if (a[i] < small) // check the condition for minimum value
small = a[i];
if (a[i] > large) //check the condition for maximum value
large = a[i];
}
printf("largest value is:%d\n", large);
printf("smallest value is:%d\n", small);
getch();
}

9. Write a C program that uses functions to perform the following: i) Addition of Two Matrices
ii) Multiplication of Two Matrices

Copy from class notes

You might also like