Cpds - Lab - Manual - Part1
Cpds - Lab - Manual - Part1
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();
}
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