[go: up one dir, main page]

0% found this document useful (0 votes)
176 views24 pages

1 General

The document provides code snippets for 30+ C programs that cover a wide range of concepts including: checking number types (perfect, prime, palindrome), operations (addition, subtraction, multiplication), conversions (binary, octal, hexadecimal), strings, arrays, files, and more. For each concept, 1-3 code examples are given to demonstrate how to write C programs to perform the corresponding tasks.

Uploaded by

dhananjaykr3781
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
176 views24 pages

1 General

The document provides code snippets for 30+ C programs that cover a wide range of concepts including: checking number types (perfect, prime, palindrome), operations (addition, subtraction, multiplication), conversions (binary, octal, hexadecimal), strings, arrays, files, and more. For each concept, 1-3 code examples are given to demonstrate how to write C programs to perform the corresponding tasks.

Uploaded by

dhananjaykr3781
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

1General

1. Write a c program to check given number is perfect number or not.


2. Write a c program to check given number is Armstrong number or not.
3. Write a c program to check given number is prime number or not.
4. Write a c program to reverse any number.
5. Write a c program to check given number is strong number or not.
6. Write a c program to find out sum of digit of given number.
7. Write a c program to check given number is palindrome number or not.
8. Write a c program to check given string is palindrome number or not.
9. Write a c program to find out power of number.
10. Write a c program to add two numbers without using addition operator.
11. Write a c program to subtract two numbers without using subtraction operator.
12. Write a c program to find largest among three numbers using binary minus operator.
13. Write a c program to find largest among three numbers using conditional operator
14. Write a c program to find out generic root of any number.
15. Write a c program to solve quadratic equation.
16. Write a c program which passes one dimension array to function.
17. Write a c program which passes two dimension array to function.
18. Write a c program which passes structure to function.
19. Write a c program for Floyd’s triangle.
20. Write a c program to print Pascal triangle.
21. Write a c program to generate multiplication table.
22. Write a c program to get factorial of given number.
23. Write a c program to find out prime factor of given number.
24. Write a c program to find out NCR factor of given number.
25. Write a c program to print Fibonacci series of given range.
26. Write a c program to print ASCII value of all characters.
27. Write a c program to check given year is leap year or not.
28. Write a c program which takes password from user.
29. Write a scanf function in c which accept sentence from user.
30. Write a scanf function in c which accept paragraph from user.
31. Write a c program to print the all prime numbers between 1 to 300.
L.C.M and H.C.F.

1. Write a c program to find out L.C.M. of two numbers.


2. Write a c program to find out H.C.F. of two numbers.
3. Write a c program to find out G.C.D. of two numbers.
Swapping

1. Write a c program to swap two numbers.


2. Write a c program to swap two numbers without using third variable.
3. Write a c program for swapping of two arrays.
4. Write a c program for swapping of two string.
Conversion

1. Write a c program to convert decimal number to hexadecimal number.


2. Write a c program to convert decimal number to octal number.
3. Write a c program to convert octal number to decimal number.
4. Write a c program to convert octal number to hexadecimal number.
5. Write a c program to convert hexadecimal number to decimal number.
6. Write a c program to convert hexadecimal number to octal number.
7. Write a c program to convert binary number to decimal number.
8. Write a c program to convert binary number to hexadecimal number.
9. Write a c program to convert binary number to octal number.
10. Write a c program to convert decimal number to binary number.
11. Write a c program to convert hexadecimal number to binary number.
12. Write a c program to convert octal number to binary number.
13. Write a c program to convert farehnite to centigrade.
14. Write a c program to convert centigrade to farehnite.
String

1. Write a c program to convert the string from upper case to lower case.
2. Write a c program to convert the string from lower case to upper case.
3. Write a c program to delete the all consonants from given string.
4. Write a c program to count the different types of characters in given string.
5. Write a c program to sort the characters of a string.
6. Write a c program for concatenation two strings without using string.h header file.
7. Write a c program to find the length of a string using pointer.
8. Write a c program which prints initial of any name.
9. Write a c program to print the string from given character.
Matrix

1. Write a c program for addition of two matrices.


2. Write a c program for subtraction of two matrices
3. Write a c program for multiplication of two matrices.
4. Write a c program to find out sum of diagonal element of a matrix.
5. Write a c program to find out transport of a matrix.
File

1. Write a c program to open a file and write some text and close its.
2. Write a c program to delete a file.
3. Write a c program to copy a file from one location to other location.
4. Write a c program to copy a data of file to other file.
5. Write a c program which display source code as a output.
6. Write a c program which writes string in the file.
7. Write a c program which reads string from file.
8. Write a c program which writes array in the file.
9. Write a c program which concatenate two file and write it third file.
10. Write a c program to find out size of any file.
11. Write a c program to know type of file.
12. Write a c program to know permission of any file.
13. Write a c program to know last date of modification of any file.
14. Write a c program to find size and drive of any file.
Complex number

1. Write a c program for addition and subtraction of two complex numbers.


2. Write a c program for multiplication of two complex numbers.
3. Write a c program for division two complex numbers.
Series

1. Write a c program to find out the sum of series 1 + 2 + …. + n.


2. Write a c program to find out the sum of series 1^2 + 2^2 + …. + n^2.
3. Write a c program to find out the sum of series 1^3 + 2^3 + …. + n^3.
4. Write a c program to find out the sum of given A.P.
5. Write a c program to find out the sum of given G.P.
6. Write a c program to find out the sum of given H.P.
7. Write a c program to find out the sum of series 1 + 2 + 4 + 8 … to infinity.
Array

1. Write a c program to find out largest element of an array.


2. Write a c program to find out second largest element of an unsorted array.
3. Write a c program to find out second smallest element of an unsorted array.
4. Write a c program which deletes the duplicate element of an array.
5. Write a c program for delete an element at desired position in an array.
6. Write a c program for insert an element at desired position in an array.
Sorting

1. Write a c program for bubble sort.


2. Write a c program for insertion sort.
3. Write a c program for selection sort.
4. Write a c program for quick sort.
5. Write a c program for heap sort.
6. Write a c program for merge sort.
7. Write a c program for shell sort.
Recursion

1. Write a c program to find factorial of a number using recursion.


2. Write a c program to find GCD of a two numbers using recursion.
3. Write a c program to find out sum digits of a number using recursion.
4. Write a c program to find power of a number using function recursion.
5. Write a c program to reverse any number using recursion.
Size of data type

1. Write a c program to find the size of int without using sizeof operator.
2. Write a c program to find the size of double without using sizeof operator.
3. Write a c program to find the size of structure without using sizeof operator.
4. Write a c program to find the size of union without using sizeof operator.
Using pointer

1. Write a c program for concatenation two string using pointer.


Searching

1. Write a c program for linear search.


2. Write a c program for binary search.
3. Write a c program for binary search using recursion.
Area and volume

1. Write a c program to find the area of circle.


2. Write a c program to find the area of any triangle.
3. Write a c program to find the area of rectangle.
4. Write a c program to find the area of equilateral triangle.
5. Write a c program to find the area of trapezium.
6. Write a c program to find the area of right angle triangle.
7. Write a c program to find the volume and surface area of cube.
8. Write a c program to find the surface area and volume of a cone.
9. Write a c program to find the perimeter of a circle, rectangle and triangle.

C program with very large numbers


1. Write a c program to find factorial of 100 or very large numbers
2. Write a c program to multiply the two very large number (larger the long int)
3. Write a c program for division of large number (larger than long int)

1
#include<stdio.h>
int main(){
  int n,i=1,sum=0;
  printf("\nEnter a number:-");
  scanf("%d",&n);
  while(i<n){
      if(n%i==0)
           sum=sum+i;
          i++;
  }
  if(sum==n)
      printf("\nThe no %d is a perfect number",i);
  else
      printf("\nThe no %d is not a perfect
number",i);
  return 0;
}
2.
#include<stdio.h>
int main(){
int num,r,sum=0,temp;
printf("\nEnter a number:-");
scanf("%d",&num);
temp=num;
while(num!=0){
r=num%10;
num=num/10;
sum=sum+(r*r*r);
}
if(sum==temp)
printf("\nThe number %d is an armstrong
number",temp);
else
printf("\nThe number %d is not an armstrong
number",temp);
return 0;
}

3.
#include<stdio.h>
int main(){
    int num,i,count=0;
    printf("\nEnter a number:");
    scanf("%d",&num);
    for(i=2;i<=num/2;i++){
        if(num%i==0){
         count++;
            break;
        }
    }
   if(count==0)
        printf("%d is a prime number",num);
   else
      printf("%d is not a prime number",num);
   return 0;
}
4-

#include<stdio.h>
int main(){
int num,out;
scanf("%d",&num);
out=rev(num);
printf("%d",out);
return 0;
}

int rev(int num){
static sum,r;
if(num){
r=num%10;
sum=sum*10+r;
rev(num/10);
}
else return 0;
return sum;
}

5.

#include<stdio.h>
int main(){
  int num,i,f,r,sum=0,temp;
  printf("\nEnter a number");
  scanf("%d",&num);
  temp=num;
  while(num){
      i=1,f=1;
      r=num%10;
      while(i<=r){
      f=f*i;
          i++;
      }
      sum=sum+f;
      num=num/10;
  }
  if(sum==temp)
      printf("%d is a strong number",temp);
  else
      printf("%d is not a strong number",temp);
  return 0;
}

6./

#include<stdio.h>
int main(){
  int num,sum=0,r;
  printf("\nEnter a number:");
  scanf("%d",&num);
  while(num){
      r=num%10;
      num=num/10;
      sum=sum+r;
  }
  printf("sum=%d",sum);
  return 0;
}

7.

#include<stdio.h>/*check the given number is


palindrome number or not using c program*/
int main(){
int num,r,sum=0,temp;
printf("\nEnter a number:");
scanf("%d",&num);
temp=num;
while(num){
r=num%10;
num=num/10;
sum=sum*10+r;
}
if(temp==sum)
printf("\n%d is a palindrome",temp);
else
printf("\n%d is not a palindrome",temp);
return 0;
}
8,

#include<string.h>
#include<stdio.h>
int main(){
  char *str,*rev;
  int i,j;
  printf("\nEnter a string:");
  scanf("%s",str);
  for(i=strlen(str)-1,j=0;i>=0;i--,j++)
      rev[j]=str[i];
      rev[j]='\0';
  if(strcmp(rev,str))
      printf("\nThe string is not a palindrome");
  else
      printf("\nThe string is a palindrome");
  return 0;
}

9.

#include<stdio.h>
int main(){
  int pow,num,i=1;
  long int sum=1;
  printf("\nEnter a number: ");
  scanf("%d",&num);
  printf("\nEnter power: ");
  scanf("%d",&pow);
  while(i<=pow){
            sum=sum*num;
            i++;
  }
  printf("\n%d to the power %d is: %ld",num,pow,sum);
  return 0;
}

10.

11.

12.
#include<stdio.h>
int main(){
    int a,b,c;
    printf("\nEnter 3 numbers: ");
    scanf("%d %d %d",&a,&b,&c);
    if(a-b>0 && a-c>0)
         printf("\nGreatest is a :%d",a);
    else
         if(b-c>0)
             printf("\nGreatest is b :%d",b);
         else
             printf("\nGreatest is c :%d",c);
    return 0;
}

13.

#include<stdio.h>
int main(){
  int a,b,c,big;
  printf("\nEnter 3 numbers:");
  scanf("%d %d %d",&a,&b,&c);
  big=(a>b&&a>c?a:b>c?b:c);
  printf("\nThe biggest number is:%d",big);
  return 0;
}

14.

#include<stdio.h>
int main(){
long int num,sum,r;
printf("\nEnter a number:-");
scanf("%ld",&num);
while(num>10){
sum=0;
while(num){
r=num%10;
num=num/10;
sum+=r;
}
if(sum>10)
num=sum;
else
break;
}
printf("\nSum of the digits in single digit is:
%ld",sum);
return 0;
}
15.

16.

#include <stdio.h>
#define N 5
void fstore1D(int a[], int a_size);
void fretrieve1D(int a[], int a_size);
void fedit1D(int a[], int a_size);
int main(){
int a[N];
printf("Input data into the matrix:\n");
fstore1D(a, N);
fretrieve1D(a, N);
fedit1D(a, N);
fretrieve1D(a, N);
return 0;
}

void fstore1D(int a[], int n){
int i;
for ( i = 0; i < n; ++i )
scanf("%d", &a[i]);
}

void fretrieve1D(int a[], int n){
int i;
for ( i = 0; i < n; ++i )
printf("%6d ", a[i]);
printf("\n");
}

void fedit1D(int a[], int n){
int i, q;
for ( i = 0; i < n; ++i ){
printf("Prev. data: %d\nEnter 1 to edit 0 to
skip.", a[i]);
scanf("%d", &q);
if ( q == 1 ){
printf("Enter new value: ");
scanf("%d", &a[i]);
}
}
}

17.

#include <stdio.h>
#define M 3
#define N 5
void fstore2D(int a[][N]);
void fretrieve2D(int a[][N]);
int main(){
  int a[M][N];
  printf("Input data in matrix (%d X %d)\n", M, N);
  fstore2D(a);
  fretrieve2D(a);
  return 0;
}
void fstore2D(int a[][N]){
    int i, j;
    for (i = 0; i < M; ++i){
    for (j = 0; j < N; ++j)
         scanf("%d", &a[i][j]);
    }
}
void fretrieve2D(int a[][N]){
   int i, j;
   for ( i = 0; i < M; ++i ){
       for ( j = 0; j < N; ++j)
            printf("%6d ", a[i][j]);
       printf("\n");
   }
}

18.
19.

#include<stdio.h>
void main(){
  int i,j,r,k=1;
  printf("\nEnter the range:");
  scanf("%d",&r);
  printf("\nFLOYD'S TRIANGLE\n\n");
  for(i=1;i<=r;i++){
      for(j=1;j<=i;j++,k++)
           printf(" %d",k);
      printf("\n");
  }
  return 0;
}

20.

#include<stdio.h>
int main(){
  int line,i,j,k;
  printf("Enter the no. of lines");
  scanf("%d",&line);
  for(i=1;i<=line;i++){
      for(j=1;j<=line-i;j++)
           printf(" ");
      for(k=1;k<i;k++)
      printf("%d",k);
      for(k=i;k>=1;k--)
      printf("%d",k);
      printf("\n");
  }
  return 0;
}

21.

#include<stdio.h>
int main(){
  int r,i,j,k;
  printf("\nEnter the number range:-");
  scanf("%d",&r);
  for(i=1;i<=r;i++){
      for(j=1;j<=10;j++)
           printf(" %d*%d=%d",i,j,i*j);
      printf("\n");
  }
  return 0;
}

22.

#include<stdio.h>
int main(){
  int i=1,f=1,num;
  printf("\nEnter a number:");
  scanf("%d",&num);
  while(i<=num){
      f=f*i;
      i++;
  }
  printf("\nFactorial of %d is:%d",num,f);
  return 0;
}

23.

#include<stdio.h>
int main(){
  int num,i=1,j,k;
  printf("\nEnter a number:");
  scanf("%d",&num);
  while(i<=num){
      k=0;
      if(num%i==0){
         j=1;
          while(j<=i){
            if(i%j==0)
                 k++;
             j++;
          }
          if(k==2)
             printf("\n%d is a prime factor",i);
      }
      i++;
   }
   return 0;
}

24.

#include<stdio.h>
int main(){
  int n,r,ncr;
  printf("Enter any two numbers->");
  scanf("%d %d",&n,&r);
  ncr=fact(n)/(fact(r)*fact(n-r));
  printf("The NCR factor of %d and %d is
%d",n,r,ncr);
  return 0;
}
 int fact(int n){
  int i=1;
  while(n!=0){
      i=i*n;
      n--;
  }
  return i;
 }

25.

#include<stdio.h>
int main(){
  int n,r,ncr;
  printf("Enter any two numbers->");
  scanf("%d %d",&n,&r);
  ncr=fact(n)/(fact(r)*fact(n-r));
  printf("The NCR factor of %d and %d is
%d",n,r,ncr);
  return 0;
}
 int fact(int n){
  int i=1;
  while(n!=0){
      i=i*n;
      n--;
  }
  return i;
 }

26.

#include<stdio.h>
void main(){
  int i;
  for(i=0;i<=255;i++){
            printf("%d -> %c ",i,i);
  }
  return 0;
}

27.

28.

29.

30.

31.

#include <math.h>
#include <stdio.h>
main()
{
  int i, j;
  i = 1;
  while ( i < 300 )
  {
            j = 2;
            while ( j < sqrt(i) )
            {
                        if ( i % j == 0 )
                                    break;
                        else
                        {
                                    ++j;
                                    continue;
                        }
            }
   if ( j > sqrt(i) )
            printf("%d\t", i);
    ++i;
  }
  return 0;
}

1.

#include<stdio.h>
int main(){
  int n1,n2,x,y;
  printf("\nEnter two numbers:");
  scanf("%d %d",&n1,&n2);
  x=n1,y=n2;
  while(n1!=n2){
      if(n1>n2)
           n1=n1-n2;
      else
      n2=n2-n1;
  }
  printf("L.C.M=%d",x*y/n1);
  return 0;
}

2.

#include<stdio.h>
int main(){
int n1,n2;
printf("\nEnter two numbers:");
scanf("%d %d",&n1,&n2);
while(n1!=n2){
if(n1>=n2-1)
n1=n1-n2;
else
n2=n2-n1;
}
printf("\nGCD=%d",n1);
return 0;
}
1.

#include<stdio.h>
int main(){
    int a,b;
    printf("\nEnter two numbers:");
    scanf("%d %d",&a,&b);
    printf("\nBefore swapping a=%d b=%d",a,b);
    a=a^b;
    b=b^a;
    a=a^b;
    printf("\nAfter swapping a=%d b=%d",a,b);
    return 0;
}

OR
Swapping of two number
#include<stdio.h>
int main(){
    int a=5,b=10;
//process one
    a=b+a;
    b=a-b;
    a=a-b;
    printf("a= %d  b=  %d",a,b);

//process two
    a=5;
    b=10;
    a=a+b-(b=a);
    printf("\na= %d  b=  %d",a,b);
//process three
    a=5;
    b=10;
    a=a^b;
    b=a^b;
    a=b^a;
    printf("\na= %d  b=  %d",a,b);
   
//process four
    a=5;
    b=10;
    a=b-~a-1;
    b=a+~b+1;
    a=a+~b+1;
    printf("\na= %d  b=  %d",a,b);
   
//process five
    a=5,
    b=10;
    a=b+a,b=a-b,a=a-b;
    printf("\na= %d  b=  %d",a,b);
    getch();

2.

#include<stdio.h>
int main(){
    int a,b;
    printf("\nEnter two numbers:");
    scanf("%d %d",&a,&b);
    printf("\nBefore swapping a=%d b=%d",a,b);
    a=a^b;
    b=b^a;
    a=a^b;
    printf("\nAfter swapping a=%d b=%d",a,b);
    return 0;
}

OR
Swapping of two number
#include<stdio.h>
int main(){
    int a=5,b=10;
//process one
    a=b+a;
    b=a-b;
    a=a-b;
    printf("a= %d  b=  %d",a,b);

//process two
    a=5;
    b=10;
    a=a+b-(b=a);
    printf("\na= %d  b=  %d",a,b);
//process three
    a=5;
    b=10;
    a=a^b;
    b=a^b;
    a=b^a;
    printf("\na= %d  b=  %d",a,b);
   
//process four
    a=5;
    b=10;
    a=b-~a-1;
    b=a+~b+1;
    a=a+~b+1;
    printf("\na= %d  b=  %d",a,b);
   
//process five
    a=5,
    b=10;
    a=b+a,b=a-b,a=a-b;
    printf("\na= %d  b=  %d",a,b);
    getch();

3.

#include<stdio.h>
int main(){
  int a[10],b[10],c[10],i;
  printf("Enter First array->");
  for(i=0;i<10;i++)
  scanf("%d",&a[i]);
  printf("\nEnter Second array->");
  for(i=0;i<10;i++)
            scanf("%d",&b[i]);
  printf("Arrays before swapping");
  printf("\nFirst array->");
  for(i=0;i<10;i++){
            printf("%d",a[i]);
  }
  printf("\nSecond array->");
  for(i=0;i<10;i++){
            printf("%d",b[i]);
  }
  for(i=0;i<10;i++){
            //write any swapping technique
            c[i]=a[i];
            a[i]=b[i];
            b[i]=c[i];
  }
  printf("\nArrays after swapping");
  printf("\nFirst array->");
  for(i=0;i<10;i++){
            printf("%d",a[i]);
  }
  printf("\nSecond array->");
  for(i=0;i<10;i++){
            printf("%d",b[i]);
  }
  return 0;
}

4.

#include<stdio.h>
int main(){
  int i=0,j=0,k=0;
  char str1[20],str2[20],temp[20];
  puts("Enter first string");
  gets(str1);
  puts("Enter second string");
  gets(str2);
  printf("Before swaping the strings are\n");
  puts(str1);
  puts(str2);
  while(str1[i]!='\0'){
             temp[j++]=str1[i++];
  }
  temp[j]='\0';
  i=0,j=0;
  while(str2[i]!='\0'){
              str1[j++]=str2[i++];
  }
  str1[j]='\0';
  i=0,j=0;
  while(temp[i]!='\0'){
              str2[j++]=temp[i++];
  }
  str2[j]='\0';
  printf("After swaping the strings are\n");
  puts(str1);
  puts(str2);
  return 0;
}

#include<stdio.h>/*binary to decimal*/
int main(){
  long int no,n=0,j=1,rem,no1;
  printf("Enter any number any binary form->");
  scanf("%ld",&no);
  no1=no;
  while(no!=0){
      rem=no%10;
      n=n+rem*j;
      j=j*2;
      no=no/10;
  }
  printf("\nThe value of binary no. %ld is ->
%ld",no1,n);
  return 0;
}

#include<stdio.h>/*decimal 2 binary*/
int main(){
  long int m,no=0,a=1;
  int n,rem;
  printf("Enter any decimal number->");
  scanf("%d",&n);
  m=n;
  while(n!=0){
      rem=n%2;
      no=no+rem*a;
      n=n/2;
     a=a*10;
  }
  printf("The value %ld in binary is->",m);
  printf("%ld",no);
  return 0;
}

#include<stdio.h>/*fern. To celsius*/
int main(){
  long int no,n=0,j=1,rem,no1;
  printf("Enter any number any binary form->");
  scanf("%ld",&no);
  no1=no;
  while(no!=0){
      rem=no%10;
      n=n+rem*j;
      j=j*2;
      no=no/10;
  }
  printf("\nThe value of binary no. %ld is ->
%ld",no1,n);
  return 0;
}

#include<stdio.h>/*decimal to octal*/
int main(){
  int i=0,j=0,rem=0,a[10],b[10];
  long int num;
  printf("\nEnter a number :");
  scanf("%ld",&num);
  while(num){
    if(num<8){
         a[j++]=num;
         break;
     }
     else{
         a[j++]=num%8;
         num=num/8;
     }
   }
   for(i=j-1;i>=0;i--)
   b[rem++]=a[i];
   printf("\nOctal equivalent :");
   for(j=0;j<rem;j++)
   printf("%d",b[j]);
   return 0;
}

#include<stdio.h>/*addition and subtraction of


complex numbers*/
int main(){
  int a,b,c,d,x,y;
  printf("\nEnter the first complex number:");
  scanf("%d%d",&a,&b);
  printf("\nEnter the second complex number:");
  scanf("%d%d",&c,&d);
  if(b<0)
      printf("%d-i\n",a-b);
  else
      printf("d+i\n",a+b);
  if(d<0)
      printf("d-i\n",c-d);
  else
      printf("%d+i\n",c+d);
  printf("\nADDITION ");
  x=a+c;
  y=b+d;
  if(y>0)
      printf("%d-i%d",x,-y);
  else
      printf("%d+i%d",x,+y);
  printf("\n\nSUBTRACTION ");
  x=a-c;
  y=b-d;
  if(y<0)
      printf("%d-i%d",x,-y);
  else
      printf("%d+i%d",x,+y);
  return 0;
}

You might also like