Find Out The Perfect Number Using C Program
Find Out The Perfect Number Using C Program
Find Out The Perfect Number Using C Program
Sample output:
Enter the number whose digits are to be added: 4582
Sum of the digits: 19
4. A Fibonacci sequence is defined as follows: the first and second terms in the sequence are 0 and 1.
Subsequent terms are found by adding the preceding two terms in the sequence.
Definition of Fibonacci numbers:
We assume first two Fibonacci are 0 and 1
A series of numbers in which each sequent number is sum of its two previous numbers is known as Fibonacci series
and each numbers are called Fibonacci numbers.
Example of Fibonacci series:
0 , 1 ,1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55
#include<stdio.h>
#include<conio.h>
void main()
{
int k,r;
long int prv=0,pre=1,nn;
clrscr();
printf("Enter the number range:");
scanf("%d",&r);
printf("FIBONACCI SERIES: ");
printf("%ld%ld",prv,pre);
for(k=2;k<r;k++)
{
2
nn=prv+pre;
prv=pre;
pre=nn;
printf("%ld",pre);
}
getch();
}
Sample Output:
Enter the number range: 15
FIBONACCI SERIES: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377.
Sample Output:
Enter Num value To Print Prime Numbers between 1 and Num:
20
Prime Numbers upto 20
2 3 5 7 11 13 17 19
7. 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[25], i, large, small, n;
clrscr();
printf("Enter the size of array(max 25)\n");
scanf("%d", &n);
printf("Enter any %d integer array elements\n",n);
for(i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
large = a[0];
small = a[0];
for(i = 1; i < n ; i++)
{
if(a[i] > large)
{
large = a[i];
}
if(a[i] < small)
{
small = a[i];
}
}
printf("The largest element from the given array is:%d \nThe smallest element from the given array is:%d", large,
small);
getch();
}
Sample Output:
Enter the size of array(max 25)
5
Enter any 5 integers array elements
10 2 3 1 5
The largest element from the given array is 10
The smallest element from the given array is 1
4
8 a) C program for addition of two matrices using arrays.
#include <stdio.h>
#include<conio.h>
void main()
{
int m,n, p,q,c, d, first[10][10], second[10][10], sum[10][10];
clrscr();
printf("Enter the number of rows and columns of first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");
for (c = 0; c < m; c++)
{
for (d = 0; d < n; d++)
{
scanf("%d", &first[c][d]);
}
}
printf("Enter the number of rows and columns of second matrix\n");
scanf("%d%d", &p, &q);
printf("Enter the elements of second matrix\n");
for (c = 0; c < p; c++)
{
for (d = 0 ; d < q; d++)
{
scanf("%d", &second[c][d]);
}
}
printf("Sum of entered matrices:-\n");
for (c = 0; c < m; c++)
{
for (d = 0 ; d < n; d++)
{
sum[c][d] = first[c][d] + second[c][d];
printf("%d\t", sum[c][d]);
}
printf("\n");
}
getch();
}
Sample Output:
Enter the number of rows and columns of first matrix
2
2
Enter the elements of first matrix
1
2
3
4
Enter the number of rows and columns of second matrix
2
2
5
Enter the elements of second matrix
5
6
2
1
Sum of entered matrices:
6 8
5 5
b) Multiplication two matrices.
#include <stdio.h>
#include<conio.h>
void main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
clrscr();
printf("Enter the number of rows and columns of first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");
for (c = 0; c < m; c++)
{
for (d = 0; d < n; d++)
{
scanf("%d", &first[c][d]);
}
}
printf("Enter the number of rows and columns of second matrix\n");
scanf("%d%d", &p, &q);
if (n != p)
printf("Matrices with entered orders can't be multiplied with each other.\n");
else
{
printf("Enter the elements of second matrix\n");
for (c = 0; c < p; c++)
{
for (d = 0; d < q; d++)
{
scanf("%d", &second[c][d]);
}
}
for (c = 0; c < m; c++)
{
for (d = 0; d < q; d++)
{
for (k = 0; k < p; k++)
{
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
6
printf("Product of entered matrices:-\n");
for (c = 0; c < m; c++)
{
for (d = 0; d < q; d++)
{
printf("%d\t", multiply[c][d]);
printf("\n");
}
}
}
getch();
}
Sample Output:
Enter the number of rows and columns of first matrix
2
2
Enter the elements of first matrix
1
2
3
4
10. Write C program that implements searching of given item in given list.
Linear search c program
#include <stdio.h>
#include<conio.h>
void main()
{
int a[100],search,c,n;
clrscr();
printf("\nEnter the number of elements into an array:");
scanf("%d",&n);
printf("Enter any %d Elemnts:\n",n);
10
return 0;
}
void towers(int num, char frompeg, char topeg, char auxpeg)
if (num == 1)
{
printf("\n Move disk 1 from peg %c to peg %c", frompeg, topeg);
return;
}
towers(num - 1, frompeg, auxpeg, topeg);
printf("\n Move disk %d from peg %c to peg %c", num, frompeg, topeg);
towers(num - 1, auxpeg, topeg, frompeg);
}
Sample Output:
Enter the number of disks : 3
The sequence of moves involved in the Tower of Hanoi are :
Move disk 1 from peg A to peg C
Move disk 2 from peg A to peg B
Move disk 1 from peg C to peg B
Move disk 3 from peg A to peg C
Move disk 1 from peg B to peg A
Move disk 2 from peg B to peg C
Move disk 1 from peg A to peg C
Sample Output:
Enter an integer: 12321
12321 is a palindrome.
Enter an integer: 52145
52145 is not a palindrome.
14. C program for Swapping of two numbers using pointers.
#include <stdio.h>
11
int main()
{
int x, y, *a, *b, temp;
clrscr();
printf("Enter the value of x and y:\n");
scanf("%d%d", &x, &y);
printf("Before Swapping\nx = %d\ny = %d\n", x, y);
a = &x;
b = &y;
temp = *b;
*b = *a;
*a = temp;
printf("After Swapping\nx = %d\ny = %d\n", x, y);
return 0;
}
Sample Output:
Enter the value of x and y:
10
20
Before Swapping
x =10
y = 20
After Swapping
x =20
y = 10
Sample Output:
Enter name of file to copy
Factorial.c
Enter name of target file
Factorial-copy.c
File copied successfully.
13