You are on page 1of 13

1. Find out the perfect number using c program.

Definition of perfect number


Perfect number is a positive number which sum of all positive divisors excluding that number is equal to that
number. For example 6 is perfect number since divisor of 6 are 1, 2 and 3.  Sum of its divisor is 1 + 2+ 3 =6.
6 is the smallest perfect number.
Next perfect number is 28 since 1+ 2 + 4 + 7 + 14 = 28
Some more perfect numbers: 496, 8128...
 C program to check perfect number
#include<stdio.h>
#include<conio.h>
void  main()
{
int n,i=1,sum=0;
clrscr();
printf("Enter a number:");
scanf("%d",&n);
while(i<n)
{
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("%d is a perfect number",i);
else
printf("%d is not a perfect number",i);
getch();
}
Sample output:
Enter a number: 6
6 is a perfect number.
2. Write a C program to check whether a number is Armstrong or not.
Armstrong number
A positive integer is called an Armstrong number if the sum of cubes of individual digit is equal to that
number itself. For example:
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.
121 is not equal to 1*1*1+2*2*2+1*1*1 // 121 is not an Armstrong number.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,rem,asn=0,temp;
clrscr();
printf("Enter a number:");
scanf("%d",&n);
temp=n;
while(n!=0)
{
rem=n%10;
asn=asn+(rem*rem*rem);
n=n/10;
}
if(temp==asn)
printf("%d is an Armstrong number",temp);
else
1
printf("%d is not an Armstrong number",temp);
getch();
}
Sample Output:
Enter a number:
153
153 is an Armstrong number

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


#include<stdio.h>
#include<conio.h>
void main()
{
long int n,k=1,sum=0;
clrscr();
printf("Enter the number whose digits are to be added:");
scanf("%ld",&n);
while(n!=0)
{
k=n%10;
sum=sum+k;
k=n/10;
n=k;
}
printf("Sum of the digits:%ld",sum);
getch();
}

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.

5. Write a C program to generate the first ‘n’ terms of the sequence.


Sum of 1 + 2 + ….  + n series in c programming language
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
int sum=0;
clrscr();
printf("Enter the n i.e. max values of series: ");
scanf("%d",&n);
sum = (n * (n + 1)) / 2;
printf("Sum of the series: ");
for(i =1;i <= n;i++){
if (i!=n)
printf("%d + ",i);
else
printf("%d = %d ",i,sum);
}
getch();
}
Sample output:
Enter the n i.e. max values of series: 5
Sum of the series: 1 + 2 + 3 + 4 + 5 = 15
6. Write a C program to generate all the prime numbers between 1 and n, where n is a value supplied by the user.
#include<stdio.h>
#include<conio.h>
void main()
{
Int num,i=1,j,count;
clrscr();
printf("Enter Num value To Print Prime Numbers between 1 and Num:");
scanf("%d",&num);
printf("Prime Numbers upto %d:\n",num);
while(i<=num)
{
count=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
count++;
}
3
if(count= =2)
printf("%d ",i);
i++;
}
printf("\n\n");
getch ();
}

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

Enter the number of rows and columns of second matrix


2
2
Enter the elements of second matrix
5
6
2
1
Product of entered matrices:
9 8
23 13

9. Write a program to perform various string operations


#include<stdio.h>
#include<conio.h>
void main()
{
char arr[30],s1[10],s2[10],s3[10];
int opt,i=0,j,len=0;
clrscr();
printf("Enter any option\n\n");
printf("1: Find out length of the string\n");
printf("2: Concatenate of the two string\n");
printf("3:Reverse of the string\n");
printf("4:Copy of the string\n");
printf("Enter u r choice:\n");
scanf("%d",&opt);
switch(opt)
{
case 1:
{
7
printf("Enter any string: \n");
scanf("%s",arr);
for(i=0;arr[i]!='\0';i++);
printf("The length of the string is:%d",i);
break;
}
case 2:
{
printf(" String Concatenation \n");
printf("\nEnter a First string::\n");
scanf("%s", s1);
printf("\nEnter a Second string:");
scanf("%s",s2);
for(i=0;s1[i]!='\0';i++)
{
s3[i]=s1[i];
}
s3[i]='\0';
for(j=0;j<=i;j++)
{
s3[i+j]=s2[j];
}
printf("The Concatenated string is:%s",s3);
break;
}
case 3:
{
printf(" Reverse the string ");
printf("\nEnter a string:");
scanf("%s",s1);
while(s1[i]!='\0')
{
len=len+1;
i++ ;
}
for(i=len-1;i>=0;i--)
{
printf("%c",s1[i]);
}
break;
}
case 4:
{
printf(" String copying \n");
printf("Enter a 1st string:");
scanf("%s",s1);
printf("Enter a 2nd string:");
scanf("%s",s2);
while(s2[i]!='\0')
{
s1[i]=s2[i];
i++;
}
8
s1[i]='\0';
printf("%s",s1);
break;
}
default:
{
printf("r u entered inavlid choice");
}
}
getch();
}

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

for (c = 0; c < n; c++)


scanf("%d", &a[c]);
printf("Enter a search element:\n");
scanf("%d", &search);
for (c = 0; c < n; c++)
{
if (a[c] == search) /* if required element found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
printf("%d is not present in array.\n", search);
getch();
}
Sample Output:
Enter the number of elements in array
5
Enter 5 numbers
5
6
4
2
9
Enter the number to search
4
4 is present at location 3.
11. Write a C program to sort a given list of integers in ascending order.
9
#include <stdio.h>
void main()
{
int i, j, a, n, number[30];
clrscr();
printf("Enter the value of N \n");
scanf("%d", &n);
printf("Enter the numbers \n");
for (i = 0; i < n; ++i)
scanf("%d", &number[i]);
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (number[i] > number[j])
{
a = number[i];
number[i] = number[j];
number[j] = a;
}
}
}
printf("The numbers arranged in ascending order are given below \n");
for (i = 0; i < n; ++i)
printf("%d\n", number[i]);
}
Sample Output:
Enter the value of N
6
Enter the numbers
3
78
90
456
780
200
The numbers arranged in ascending order are given below
3
78
90
200
456
780

12. C program for Towers of Hanoi using Recursion *


#include <stdio.h>
void towers(int, char, char, char);
int main()
{
int num;
clrscr();
printf("Enter the number of disks : ");
scanf("%d", &num);
printf("The sequence of moves involved in the Tower of Hanoi are :\n");
towers(num, 'A', 'C', 'B');

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

13. C Program to Check Palindrome Number


#include <stdio.h>
int main()
{
int n, reverse=0,rem,temp;
clrscr();
printf("Enter an integer: ");
scanf("%d", &n);
temp=n;
while(temp!=0)
{
rem=temp%10;
reverse=reverse*10+rem;
temp=temp/10;
}
if(reverse==n)
printf("%d is a palindrome.",n);
else
printf("%d is not a palindrome.",n);
return 0;
}

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

15. C program for using files(copying)


#include <stdio.h>
#include <stdlib.h> 
int main()
{
char ch, source_file[20], target_file[20];
FILE *source, *target; 
clrscr();
printf("Enter name of file to copy\n");
gets(source_file); 
source = fopen(source_file, "r"); 
if( source == NULL )
{
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);

printf("Enter name of target file\n");
gets(target_file); 
target = fopen(target_file, "w"); 
if( target == NULL )
{
fclose(source);
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);

while( ( ch = fgetc(source) ) != EOF )
fputc(ch, target); 
12
printf("File copied successfully.\n"); 
fclose(source);
fclose(target); 
return 0;
}

Sample Output:
Enter name of file to copy
Factorial.c
Enter name of target file
Factorial-copy.c
File copied successfully.

13

You might also like