About Me

My photo
Vijayapur, Karnataka, India
I am interested in Teaching.

Tuesday 7 January 2020

Important C programs to be practice for C for Problem Solving I or II Semester B.E. Examination, VTU, Belagavi

C program to calculate simple interest.

#include<stdio.h>
void main()
{
float p, t, r, si;
printf("Enter principle amount, rate of interest and time period ");
scanf("%f%f%f",&p, &t, &r);
si=(p * t * r)/100;
printf(" Simple Interest=%f",si);
}

C program to calculate area and circumference of a circle

#include<stdio.h>
void main()
{
float r, area, cir;
printf("Enter radius of a circle ");
scanf("%f",&r);
area=3.142 * r * r;
cir=2 * r *r;
printf("\n Area of circle = %f ",area);
printf("\n Circumference of circle = %f ",cir);

}

C program to calculate area and perimeter of a rectangle

#include<stdio.h>
void main()
{
float l, b, area, per;
printf("Enter length and breadth of rectangle ");
scanf("%f%f",&l,&b);
area= l * b;
per=2 * (l+b);
printf("\n Area of rectangle = %f ",area);
printf("\n Perimeter of rectangle  = %f ",per);
}

C program to calculate area or triangle by reading its breadth and height (Inputs: b and h)

#include<stdio.h>
void main()
{
float b, h, area;
printf("Enter breadth and height of a rectangle ");
scanf("%f%f",&b, &h);
area= 0.5 * b * h;
printf("\n Area of triangle  = %f ",area);
}

C program to calculate area or triangle by reading 3 sides of  triangle (a,b,c)

#include<stdio.h>
#include<math.h>
void main()
{
float a, b, c, s, area;
printf("Enter 3 sides of triangle  ");
scanf("%f%f%f", &a, &b, &c);
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("\n Area of triangle  = %f ",area);
}



Do not forget to send feedback or comment.

C program to swap (interchange) the contents of two variables using temporary variable .

#include<stdio.h>
void main()
{
int a,b, temp;

printf("Enter two integers a and b ");
scanf("%d%d",&a,&b);

printf(" \n Before Swap  ");
printf(" \n a=%d b=%d  ",a,b);

temp=a;
a=b;
b=temp;

printf(" \n After Swap  ");
printf(" \n a=%d b=%d  ",a,b);

}
C program to check, whether the year is LEAP year or not.

# include<stdio.h>
void main()
{
int year;
printf("Enter year to be check");
scanf("%d",&year);
         if ( (year%4==0) && (year%100!=0) || (year %400==0) )
                     printf("Leap Year");
         else
                      printf("Not Leap Year");
}


Note:
If you get this programming question then write the above program.
If you won't get question on it; then us it as example for if else statement.

Do not forget to send feedback or comment.

C program to find largest of two numbers.

#include<stdio.h>
void main()
{
int a,b;
printf("Enter two numbers");
scanf("%d%d",&a,&b);
         if (a>b)
                     printf(" %d is largest",a);
         else
                      printf("%d is largest",b);
}

Note: Use this program as example for if else statement used in C.



C program to find largest of three  numbers.

#include<stdio.h>
void main()
{
int a,b,c;
printf("Enter three numbers");
scanf("%d%d%d",&a,&b,&c);
         if (a>b  && a>c)
                     printf(" %d is largest",a);
         else if (b>a && b>c)
                      printf("%d is largest",b);
         else
                      printf("%d is largest",c);
}

Do not forget to send feedback or comment.

C program to explain the working of switch statement

#include<stdio.h>
void main()
{
int ch;
printf("Enter your choice [1 2 3 ?] ");
scanf("%d",&c);

switch(ch)
{
case 1:
          printf("\n You have selected first choice");
          break;
case 2:
          printf("\n You have selected second choice");
          break;
case 3:
          printf("\n You have selected third choice");
          break;
default:
          printf("\n Sorry! Invalid choice ");
          break;
}


}


C program to check a number for palindrome or not.

#include<stdio.h>
void main()
{
int num, rem, rev=0,temp;

printf("Enter number to be check ");
scanf("%d",&num);

temp=num;

while (num !=0)
{
rem=num%10;
rev=(rev*10)+rem;
num=num/10;
}

if (temp = = rev)
      printf("\n Palidrome");
else
     printf("\n Not palindrome");

}

Do not forget to send feedback or comment.

C program to find factorial of number n

#include<stdio.h>
void main()
{
int n, i, prod=1;

printf("Enter a number ");
scanf("%d",&n);

for(i=1; i<=n; i++)
{
prod=prod*i;
i=i+1;
}   

printf("\n Factorial is %d", prod);

}


C program to implement BUBBLE sort technique

#include<stdio.h>
void main()
{
int n, i, j, temp ;
int a[100];

printf("Enter number of elements  ");
scanf("%d",&n);

printf("\n Enter %d numbers to be sort ",n);

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


for(i=0;i<n;i++)
{
    for(j=0;j<n-i-1;j++)
   {
                 if(a[j]>=a[j+1])
                {
                 temp=a[j];
                 a[j]=a[j+1];
                 a[j+1]=temp;
                 }
   }
}

printf("\n After Bubble Sort");

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



}

Do not forget to send feedback or comment.


C program to implement ADDITION OF TWO MATRICES 

#include<stdio.h>
void main()
{

int m,n,p,q;
int i, j;
in a[100][100],b[100][100],c[100][100];

printf("Enter the order of matrx A ");
scanf("%d%d",&m,&n);

printf("Enter the order of matrx B ");
scanf("%d%d",&p, &q);

if (m = = n  && p = = q)
{

printf("\n Matrix Addition is possible ");

printf("\n Enter elements of matrix A  ");
       for(i=0; i<m; i++)
       {
            for(j=0;j<n;j++)
            {
             scanf("%d", &a[i][j]);
            }
        }

printf("\n Enter elements of matrix B  ");
       for(i=0; i<p; i++)
       {
             for(j=0;j<q;j++)
            {
             scanf("%d", &b[i][j]);
             }
        }


       for(i=0; i<m; i++)
       {
               for(j=0;j<n;j++)
              {
                c[i][j]=a[i][j] +b[i][j];
              }
        }


printf("\n Resultant Matrix C  ");
       for(i=0; i<m; i++)
       {
                   for(j=0;j<n;j++)
                   {
                    printf(" %d ", c[i][j]);
                    }
         printf("\n");
        }

}
else
printf("\n Addition is not possible");
}

C program to implement MULTIPLICATION  OF TWO MATRICES 
In order to find matrix multiplication, the columns of first matrix must be equal to rows of second matrix.


#include<stdio.h>
void main()
{

int m,n,p,q;
int i,j,k;
in a[100][100],b[100][100],c[100][100];

printf("Enter the order of matrx A ");
scanf("%d%d",&m,&n);

printf("Enter the order of matrx B ");
scanf("%d%d",&p, &q);

if (n = = p)
{

printf("\n Matrix Multiplication is possible ");

printf("\n Enter elements of matrix A  ");
       for(i=0; i<m; i++)
       {
            for(j=0;j<n;j++)
            {
             scanf("%d", &a[i][j]);
            }
        }

printf("\n Enter elements of matrix B  ");
       for(i=0; i<p; i++)
       {
             for(j=0;j<q;j++)
            {
             scanf("%d", &b[i][j]);
             }
        }


       for(i=0; i<m; i++)
       {
               for(j=0;j<n;j++)
              {
                c[i][j]=0;
                      for(k=0;k<n;k++)
                      {
                        c[i][j]=c[i][j]+a[i][k]*b[k][j];
                       }
              }
        }


printf("\n Resultant Matrix C  ");
       for(i=0; i<m; i++)
       {
                   for(j=0;j<q;j++)
                   {
                    printf(" %d ", c[i][j]);
                    }
         printf("\n");
        }

}
else
printf("\n Matrix Multiplication is not possible");
}

C program to find length of a string without using built in function

#include<stdio.h>
void main()
{
char str[50];
int length=0;

printf("Enter a string ");
scanf("%s",str);

for(i=0;  str[i]!='\0' ; i++)
{
length=length+1;


printf("\n String length is %d", length);

}

C program to read and display N students information (Roll No, Name, Average Marks) using arrays of structure.

#include<stdio.h>

struct student
{
int rno;
char name[50];
float avg;
};

void main()
{
struct student s[50];
int n, i;

printf("Enter number of students ");
scanf("%d", &n);

printf("\n Enter roll number, name and average marks  of %d students", n);

for(i=0;i<n;i++)
{
scanf("%d %s %f",&s[i].rno, s[i].name,&s[i].avg);
}

printf("\n Student Details \n");

for(i=0;i<n;i++)
{
printf ("\n  %d     %s      %f", s[i].rno, s[i].name, s[i].avg);
}

}


C program to  swap two numbers using pointers and functions.

#include<stdio.h>

void swap(int *x, int *y);

void main()
{
int x=7, y=3;

printf("\n Before Swap \n");
printf("x=%  y=%d",x,y);

swap(&x, &y);

printf("\n After Swap \n");
printf("x=%  y=%d",x,y);

}

void swap(int *x, int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}


Note: Do not forget to send Feedback/Comments.
          I upload other programs shortly......


No comments:

GCD of two numbers and its application...

The greatest common divisor (gcd) of two numbers is the largest positive integer that divides both numbers without leaving a remainder. The ...