18 Amazing programs in C.

 1.) WAP to swap the value of three variable without using fourth variable.

// program to swap the value of three
variable with using fourth variable.
#include <conio.h>
#include <stdio.h>

int main()
{
    int a, b, c, s;
    printf("Enter the value of a,b,c:\n");
    scanf("%d%d%d", &a, &b, &c);
s =a;
    a =b;
b =c;
    c =b;
    printf("The value of a, b and c after inter
-change is \na = %d,\nb = %d,\nc = %d", a, b, c);
    return 0;
}
Output:-
The value of a, b and c after interchange is a = 95, b = 3, c = 8

 2.) WAP to check the character is upper case or lower case.

// program to check the character is uppercase or lower case.
#include <stdio.h>

int main()
{
    char ch;
    printf("Enter the character lower as well upper case:\n");
    scanf("%c", &ch);
    if (ch >= 65 && ch <= 90)
    {
        printf("%c is the upper case character", ch);
    }
    else if (ch >= 97 && ch <= 122)
    {
        printf("%c is the lower case character", ch);
    }
    return 0;
}

Output:
Enter the character lower as well upper case: Z Z is the upper case character.

3.) WAP to check the triangle is an isosceles, equilateral, scalene according their side.

// Q3. PROGRAM TO CHECK THE TRIANGLE IS ISOSCELES,
EQUILATERAL, SCANLENE ACCORDING THIER SIDES.
#include <stdio.h>
#include <conio.h>

int main()
{
    int s1, s2, s3;
    printf("Enter the sides of the triangle :\n");
    scanf("%d%d%d", &s1, &s2, &s3);
    if ((s1 == s2 && s2 != s3) || (s2 == s3 && s3 != s1)
|| (s3 == s1 && s1 != s2))
    {
        printf("side of the triangle is %d, %d, %d is an
Isosceles Triangle\n", s1, s2, s3);
    }
    if (s1 == s2 && s2 == s3 && s3 == s1)
    {
        printf("side of the triangle is %d, %d, %d is an
Equilateral Triangle\n", s1, s2, s3);
    }
    if (s1 != s2 && s2 != s3 && s3 != s1)
    {
        printf("side of the triangle is %d, %d, %d is a
Scalene Triangle\n", s1, s2, s3);
    }
    return 0;
}

Output:
Enter the sides of the triangle : 7 8 6 side of the triangle is 7, 8, 6 is a Scalene Triangle.

4.)WAP to print the square of given range of numbers.

// Program to print the square of given range of numbers.
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
    int beg, end, i, sq;
    printf("Enter the range you can
find the square of the number:\n");
    scanf("%d%d", &beg, &end);
    for (i = beg; i <= end; i++)
    {
        sq = pow(i, 2);
        printf("square of %d is %d\n", i, sq);
    }
    return 0;
}

Output:
Enter the range you can find the square of the number:
5
10
square of 5 is 25
square of 6 is 36
square of 7 is 49
square of 8 is 64
square of 9 is 81

5.)WAP to print following outputs:

1                        OR                     *****

22                                                 ****

333                                               ***

4444                                             **

55555                                           *

/*Program to print
  1
  22
  333
  4444
  55555*/
  #include<stdio.h>
  #include<conio.h>

  int main()
  {
      int i, j;
      for(i=1;i<=5;i++)
      {
          for(j=1;j<=i;j++)
          {
              printf("%d",i);
          }
          printf("\n");
      }
      return 0;
  }
    
Output:-
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
#include<stdio.h>
#include<conio.h>

int main()
{
    int i; int j;
    for(i=5;i>0;i--)
    {
        for(j=5;j>5-i;j--)
        {
            printf("*");
        }
        printf("\n");
    }
    return 0;
}
Output:
***** **** *** ** *

6.)WAP using switch-case statement for following options:

1.a) factorial of a number.

2.b)Prime or not

3.c)odd or even

4.d)exit

// program using switch case statement for following option:
// a) factorial of a number.
// b) prime or not
// c) Odd or Even
// d) exit

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

int main()
{
    char ch;
    int num, i;
    while (1)
    {
        int fact =1;
        printf("Enter the choice where a)
calculate the factorial of a number,
b) check the number is prime or not ,
c) odd or even ,d) exit :\n");
        scanf(" %c", &ch);
        switch (ch)
        {
        case 'a':
            printf("Enter the number :\n");
            scanf("%d", &num);
            for (i = 1; i <= num; i++)
            {
                fact = fact * i;
            }
            printf("Factorial of %d! is %d\n", num, fact);
            break;
        case 'b':
            printf("Enter the number:\n");
            scanf("%d", &num);
            i = 2;
            while (i < num)
            {
                if (num % i == 0)
                {
                    printf("%d is not a prime\n", num);
                    break;
                }
                i += 1;
            }
            if (i == num)
            {
                printf("%d is a prime\n", num);
            }
            break;
        case 'c':
            printf("Enter the number:\n");
            scanf("%d", &num);
            if (num % 2 == 0)
            {
                printf("%d is an Even Number\n", num);
            }
            else
            {
                printf("%d is an Odd Number\n", num);
            }
            break;
        case 'd':
            exit(0);
        default:
            printf("Invalid choice!\a\n");
        }
    }
    return 0;
}

Output:-
Enter the choice where
a) calculate the factorial of a number,
b) check the number is prime or not ,
c) odd or even ,d) exit : a Enter the number : 5 Factorial of 5! is 120 Enter the choice where
a) calculate the factorial of a number,
b) check the number is prime or not ,
c) odd or even ,d) exit : d // Exit .

7.)WAP to convert any number (in digit) into words.

/* program to convert any number(in digit) into word.
for example : 4 3 2 0
           Four Three Two zero*/
#include <stdio.h>
#include <conio.h>
int main()
{
    int num, i, rev = 0, rem;
    printf("Enter the number :\n");
    scanf("%d", &num);
    while (num != 0) // loop for find reverse of the number.
    {
        rem = num % 10;
        rev = rev * 10 + rem;
        num /= 10;
    }
    num = rev;
    while (num != 0)
    {
        rem = num % 10;
        num /= 10;
        switch (rem)
        {
        case 0:
            printf("Zero\t");
            break;
        case 1:
            printf("First\t");
            break;
        case 2:
            printf("Two\t");
            break;
        case 3:
            printf("Three\t");
            break;
        case 4:
            printf("Four\t");
            break;
        case 5:
            printf("Five\t");
            break;
        case 6:
            printf("Six\t");
            break;
        case 7:
            printf("Seven\t");
            break;
        case 8:
            printf("Eight\t");
            break;
        case 9:
            printf("Nine\t");
            break;
        default :
            printf("Invalid Choice!");
        }
    }
    return 0;
}
Output :-
Enter the number : 86459 Eight Six Four Five Nine

8.)WAP of swapping using function.

 // program to swaping using function.

#include <stdio.h>
#include <conio.h>
void swap();
int main()
{
    swap();
    return 0;
}
void swap()
{
    int a, b, s;
    printf("Enter a:\n");
    scanf("%d", &a);
    printf("Enter b:\n");
    scanf("%d", &b);
    s = a;
    a = b;
    b = s;
    printf("a=%d\n", a);
    printf("b=%d\n", b);
}
Output:-

Enter a: 520 Enter b: 365 a=365 b=520

9.) WAP for factorial using Recursion.

// Program of factorial using recursion.
#include <stdio.h>
#include <conio.h>
int fact(int);
int main()
{
    int f, n;
    printf("Enter number:\n");
    scanf("%d", &n);
    f = fact(n);
    printf("%d!=%d", n, f);
    return 0;
}
int fact(int n)
{
    if (n == 0 || n == 1)
    {
        return 1;
    }
    else
    {
        return (n * fact(n - 1));
    }
}
Output:
Enter number: 5 5!=120
😄😄😄 Thank you 😄😄😄

10.)WAP for Fibonacci series using recursion.

// program of fibonacci series by using recursion.
#include <stdio.h>
#include <conio.h>
int fibo(int, int, int);
int main()
{
    int a,b,t,f;
    printf("Enter terms you can print:\n");
    scanf("%d",&t);
    printf("Enter first two terms:\n");
    scanf("%d%d",&a,&b);
    f=fibo(a,b,t);
    return 0;
}
int fibo(int a,int b,int t)
{
    if(t>0)
    {
        printf("%d\t",a);
        return(fibo(b,a+b,t-1));
    }
}
Output:
Enter terms you can print: 9 Enter first two terms: 1 1 1 1 2 3 5 8 13 21 34

11.) WAP to obtain running sum of first 25 Natural number using recursion

// program to obtain sum of first 25 natural number.
#include<stdio.h>
#include<conio.h>
int nat(int);
int main()
{
    int n,s;
    printf("How many natural number you can print:\n");
    scanf("%d",&n);
    s=nat(n);
    printf("The sum of first %d natural number is %d",n,s);
    return 0;
}
int nat(int n)
{
    if(n!=0)
    {
        return(nat(n-1)+n);
    }
}
Output :
How many natural number you can print: 25 The sum of first 25 natural number is 325
😄😄😄 Thank you 😄😄😄

12.)WAP to calculate area and perimeter of a Triangle, a Square, a Circle using macro.

#include<stdio.h>
#include<conio.h>
#define ptriangle(a,b,c) a+b+c
#define atriangle(b,h) (b*h)/2
#define psquare(a) 4*a
#define asquare(a) a*a
#define pcircle(r) 2*3.14*r
#define acircle(r) 3.14*r*r
int main()
{
    float a,b,c,side,r,perimeter,h,area;
    char ch;
    printf("Enter the choice in which you
can find the area and perimeter \na)
Triangle,\nb) Square,\nc)Circle:\n");
    scanf("%c",&ch);
    switch(ch)
    {
        case 'a':
            printf("Enter the sides of Triangle:\n");
            scanf("%f%f%f",&a,&b,&c);
            perimeter=ptriangle(a,b,c);
            printf("Enter the base and height of
triangle:\n");
            scanf("%f%f",&b,&h);
            area=atriangle(b,h);
            printf("Perimeter and area of triangle is
p=%.2f\ta=%.2f",perimeter,area);
            break;
        case 'b':
            printf("Enter the side of square:\n");
            scanf("%f",&side);
            perimeter=psquare(side);
            area=asquare(side);
            printf("Perimeter and area of square is
p=%.2f\ta=%.2f",perimeter,area);
            break;
        case 'c':
            printf("Enter the radius of circle:\n");
            scanf("%f",&r);
            perimeter=pcircle(r);
            area=acircle(r);
            printf("Perimeter and area of circle is
p=%.2f\ta=%.2f",perimeter,area);
            break;
        default :
            printf("Invalid Choice!");
    }
    return 0;
}
Output:
Enter the choice in which you can find
the area and perimeter a) Triangle, b) Square, c)Circle: a Enter the sides of Triangle: 4 2 1 Enter the base and height of triangle: 4 2 Perimeter and area of triangle is p=7.00
a=4.00

13.)Write a program for :

a. Linear search

#include<stdio.h>
int main()
{
    int list[10]={12,5,4,6,7,9,10,2,6,0};
    int search ,i;
    printf("Enter the element you
can search in the list:\n");
    scanf("%d", &search);
    for(i=0;i<10;i++)
    {
        if(search==list[i])
        {
            printf("Search successful at
the position %d",i+1);
            break;
        }
    }
    if(i==10)
    {
        printf("Invalid search!");
    }
    return 0;
}
Output :
Enter the element you can search in the list: 10 Search successful at the position 7

b. Binary search 

#include<stdio.h>

int main()
{
    int list[10]={1,2,3,4,5,6,7,8,9,10};//sorted array
    int search,beg,end,mid;
    printf("Enter the element you
can search in the list:\n");
    scanf("%d",&search);
    beg=0;
    end=9;
    while(beg<=end)
    {
        mid = (beg+end)/2;
        if(search==list[mid])
        {
            printf("search successful!\n");
            break;
        }
        else
        {
            if(search>list[mid])
            {
                beg=mid+1;
            }
            else
            {
                end=mid-1;
            }
        }
    }
    if(beg>end)
    {
        printf("Invalid search!");
    }
    return 0;
}
Output :
Enter the element you can search in the list: 6 search successful!

c. Selection sort :

#include<stdio.h>
int main()
{
    int arr[5]={9,5,10,2,7};
    int i,j,t;
    for(i=0;i<4;i++)
    {
        for(j=i+1;j<5;j++)
        {
            if(arr[i]>arr[j])
            {
                t=arr[i];
                arr[i]=arr[j];
                arr[j]=t;
            }
        }
    }
    printf("The sorted array are:\n");
    for(i=0;i<5;i++)
    {
        printf("%d\t",arr[i]);
    }
    return 0;
}
Output :
The sorted array are: 2 5 7 9 10

d. Bubble sort :

# include<stdio.h>

int main()
{
    int list[10]={6,3,8,9,1};
    int phase,step,i,temp;
    for(phase=0;phase<4;phase++)
    {
        for(step=0;step<4-phase;step++)
        {
            if(list[step]>list[step+1])
            {
                temp=list[step];
                list[step]=list[step+1];
                list[step+1]=temp;
            }
        }
    }
    printf("The sorted array are:\n");
    for(i=0;i<5;i++)
    {
        printf("%d\t",list[i]);
    }
    return 0;
}
Output :
The sorted array are: 1 3 6 8 9

e. Insertion sort :

# include<stdio.h>

int main()
{
    int list[10]={4,6,1,2,10,3,7,5,8,9};
    int phase,step,temp,i;
    for(phase=1;phase<10;phase++)
    {
        temp=list[phase];
        step=phase-1;
        while((step>=0)&&(temp<list[step]))
        {
            list[step+1]=list[step];
            step--;
        }
        list[step+1]=temp;
    }
    printf("The sorted array are:\n");
    for(i=0;i<10;i++)
    {
        printf("%d\t",list[i]);
    }
    return 0;
}
Output :
The sorted array are: 1 2 3 4 5 6 7 8 9 10

14.)WAP to show all string operation.

/* WAP to show all string operation*/
#include<stdio.h>
#include<string.h>
int main()
{
    char a[20],b[20];
    printf("Enter the two strings a and b:\n");
    gets(a);
    gets(b);
    printf("The lenght of string a is %d\n",strlen(a));// find lenght of a string.
    printf("The lenght of string b is %d\n",strlen(b));
    printf("Combine a and b is %s",strcat(a,b));
    if(strcmp(a,b)==0)
    {
        printf("strings are same\n");
    }
    else
    {
        printf("strings are not equal\n");
    }
    // strrev reverse the string.
    printf("Reverse of string a is %s",strrev(a));
    return 0;
}
Output:
Enter the two strings a and b: The lenght of string a is 5 The lenght of string b is Combine a and b is RohitRahul varshneystrings are not equal Reverse of string a is yenhsrav luhaRtihoR

15.) WAP to find the data (Roll number, name, department, course, year of joining) of student According to roll number using structure.   

#include<stdio.h>
#include<conio.h>
struct data
{
    int rollno;
    char name[50];
    char dep[20];
    char course[10];
    int yoj;
}d[2];
int main()
{
    int rn,i;
    printf("Enter the data (Roll number,name,
deparment,course,Year of joining):\n");
    for(i=0;i<2;i++)
    {
        scanf("%d%s%s%s%d",&d[i].rollno,&d[i].name,
        &d[i].dep,&d[i].course,&d[i].yoj);
    }
    printf("Enter the roll no:\n");
    scanf("%d",&rn);
    for(i=0;i<2;i++)
    {
        if(rn==d[i].rollno)
        {
            printf("%d\t%s\t%s\t%s\t%d",d[i].rollno,
            d[i].name,d[i].dep,d[i].course,d[i].yoj);
            break;
        }
    }
    if(i==2)
    {
        printf("Invalid Rollno!");
    }
    return 0;
}
Output :
Enter the data (Roll number,name,deparment,course,Year of joining): 1 mohit A BBA 2018 2 akshat B BCA 2019 Enter the roll no: 2 2 akshat B BCA 2019

16.) WAP to write and read character/string into and from file.

create a file and write data in the file.

// WAP to write and read character/string into and from file.
#include<stdio.h>
struct student
{
    int rollno;
    char name[20];
    float per;
}d;
FILE*fw;
int main()
{
    int i;
    fw = fopen("data_store.txt","w");
    printf("Enter the data (roll no.,
name, percentage of ) the student:\n");
    for(i=0;i<5;i++)// data of 5 student
    {
        scanf("%d%s%f",&d.rollno,d.name,&d.per);
        fprintf(fw,"%d\t%s\t%.2f\n",d.rollno,d.name,d.per);
        fflush(stdin);// data input next student easily.
    }
    fclose(fw);
    return 0;
}
Input : data_store.txt
Enter the data (roll no.,name,percentage of ) the student: 1 Rohit 86.5 2 Kapil 89.5 3 Samay 96.8 4 Mohan 56.2 5 Aditya 99
Output :
1 Rohit 86.50 2 Kapil 89.50 3 Samay 96.80 4 Mohan 56.20 5 Aditya 99.00

=> now Read the data of the file data_store.txt

#include<stdio.h>
FILE *fr;
int main()
{
    char str[80];
    char *ptr;
    fr=fopen("data_store.txt","r");
    while(1)
    {
        ptr = fgets(str,80,fr);
        if(ptr!=NULL)
        {
            printf("%s",ptr);
        }
        else
        {
            break;
        }
    }
    fclose(fr);
    return 0;
}
Output :
1 Rohit 86.50 2 Kapil 89.50 3 Samay 96.80 4 Mohan 56.20 5 Aditya 99.00

17.) WAP to count character , space, tab and newline in a  file.

#include <stdio.h>
FILE *fp;
int main()
{
    int count = 0, tab = 0, space = 0, line = 0;
    char ch;
    fp = fopen("data_store.txt", "r");
    while (1)
    {
        ch = fgetc(fp);
        if (ch == EOF)
        {
            break;
        }
        else
        {
            count++;
            if (ch == ' ')
                space++;
            if (ch == '\t')
                tab++;
            if (ch == '\n')
                line++;
        }
    }
    printf("Total Number of charater in the file:%d\n", count);
    printf("Total Number of tab in the file:%d\n", tab);
    printf("Total Number of line in the file:%d\n", line);
    printf("Total Number of space in the file:%d\n", space);
    fclose(fp);
    return 0;
}
Output:
Total Number of charater in the file:75 Total Number of tab in the file:9 Total Number of line in the file:4 Total Number of space in the file:5

18.) WAP to copy the content of one file into other file .

// WAP to copy the content of one file into another file.
#include<stdio.h>
#include<conio.h>
FILE *fr;// file read
FILE *fc;// file copy
int main()
{
    char *ptr,str[80];
    fr=fopen("data_store.txt","r");
    fc=fopen("xyz.txt","w");
    while (1)
    {
        ptr=fgets(str,80,fr);
        if(ptr==NULL)
        {
            break;
        }
        else
        {
            fputs(ptr,fc);
        }
    }
    fclose(fc);
    fclose(fr);
    return 0;
}
Output : file copy in xyz.txt
1 Rohit 86.50 2 Kapil 89.50 3 Samay 96.80 4 Mohan 56.20 5 Aditya 99.00

                                     **** I hope it is useful to you ****

Comments

Post a Comment

Popular posts from this blog

5 d.) program to print this :