0% found this document useful (0 votes)
9 views

C Practical File

The document contains a series of C programming exercises, each demonstrating different programming concepts such as checking even/odd numbers, finding the largest of three numbers, checking divisibility, and calculating grades. It includes programs for mathematical operations, matrix manipulations, and algorithms like Fibonacci series, HCF, and prime number checks. Each program is accompanied by its respective code and expected output.

Uploaded by

23224
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

C Practical File

The document contains a series of C programming exercises, each demonstrating different programming concepts such as checking even/odd numbers, finding the largest of three numbers, checking divisibility, and calculating grades. It includes programs for mathematical operations, matrix manipulations, and algorithms like Fibonacci series, HCF, and prime number checks. Each program is accompanied by its respective code and expected output.

Uploaded by

23224
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

1.

Write a Program to check if the number entered through keyboard is


even or odd.

#include <stdio.h>
int main()
{
int a;
printf("Enter Your Number: ");
scanf("%d",&a);
if(a%2==0)
printf("%d is Even",a);
else
printf("%d is Odd",a);
return 0;
}

Output :

1|Page
2. Write a Program to find largest number among three numbers using
conditional operator.

#include <stdio.h>

int main()
{
int a,b,c;
int large;
printf("Enter Three Numbers:");
scanf("%d%d%d",&a,&b,&c);
large = (a>b)?(a>c?a:c):(b>c?b:c);
printf("Largest Number is:%d",large);
return 0;
}

OUTPUT :

2|Page
3. Write a program to check wheather the number is divisible by 5 or
not.

#include <stdio.h>
int main ()
{
int x;
printf("Enter your number : ");
scanf("%d",&x);
if(x%5==0){
printf("The number is divisible by 5");
}
else{
printf("The number is not divisble by 5");
}
return 0;
}

OUTPUT :

3|Page
4. Write a Program to print grade obtained by a student using if else
if ladder.

#include <stdio.h>
int main ()
{
float n;
printf("Enter your percentage : ");
scanf("%f",&n);

if(n>=90){
printf("A grade");
}
else if(n>=80){
printf("B grade");
}
else if(n>=60){
printf("C grade");
}
else if(n>=40){
printf("D grade");
}
else{
printf("Fail");
}
return 0;
}

OUTPUT:

4|Page
5. Write a Program to check if the given year is leap year or a common
year.

#include <stdio.h>
int main()
{
int n;
printf("Enter the Year you want to check:");
scanf("%d",&n);
if(n%100==0)
{
if(n%400==0)
{printf("%d is a Leap Year",n);}
else
printf("%d is not a Leap year",n);
}
else
{
if(n%4==0)
printf("%d is a Leap Year",n);
else
printf("%d is not a Leap Year",n);
}
return 0;
}

5|Page
6. Write a Program of sum of digits of a number.

#include <stdio.h>
int main ()
{
int i, n, sum=0;
printf("Enter the number : ");
scanf("%d",&n);
for (i=n ; i>0 ;i= i/10)
{
sum = sum + (i%10);
}
printf("The sum of the following number : %d",sum);
return 0;
}

OUTPUT :

6|Page
7. Write a Program to find reverse of a number.

//reverse of a number
#include <stdio.h>
int main ()
{
int i , n,a;
printf ("Enter the number : ");
scanf ("%d",&n);
for(i=n; i>0 ; i=i/10)
{
a = (a*10)+ (i%10);
}
printf("Reverse of the number is = %d",a);
return 0;
}

OUTPUT:

7|Page
8. Write a Program to print factorial of a number from 1 to n.

#include <stdio.h>
int main ()
{
int n,fact=1;
printf("Enter the number : ");
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
fact=fact*i;
printf("%d! = %d\n",i,fact);
}
return 0;
}

OUTPUT:

8|Page
9. Write a Program to convert Decimal Number into Binary Number System.

#include <stdio.h>
#include <math.h>
int main ()
{
int num,rem=0,count=0;
printf("Enter the number to be converted : ");
scanf("%d",&num);

for(int i=num;i!=0;i=i/2)
{
rem=rem +(i%2)*pow(10,count);
count++;
}
printf("Binary code of the given number = %d",rem);
return 0;
}

OUTPUT:

9|Page
10. Write a Program to find the number of digits in a Number.

// count number of digits


#include <stdio.h>
int main ()
{
int i , n , count=0;
printf("Please provide the number : ");
scanf("%d",&n);
for (i=n; i>0; i=i/10)
{
count++;
}

printf ("count = %d",count);


return 0;
}

OUTPUT:

10 | P a g e
11.Write a Program to Fibonacci series upto n terms.

#include <stdio.h>
int main()
{
int a=0,b=1,sum,num;
printf("Enter the number : ");
scanf("%d",&num);

for(int i=0;i<num;i++)
{
printf("%d ",a);
sum=a+b;
a=b;
b=sum;
}

return 0;
}

OUTPUT:

11 | P a g e
12.Write a Program to read and print elements of a matrix.

#include <stdio.h>
main()
{
int m,n;

printf("Enter Number of Rows and Columns respectively:");


scanf("%d%d",&m,&n);
int a[m][n];
printf("Enter all the %d elements of %d * %d Matrix one by
one\n",m*n,m,n);

for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
printf("Enter element [%d][%d]:",i+1,j+1);
scanf("%d", &a[i][j]);
}
}
printf("The entered Matrix is:\n");
for(int i=0;i<m;i++)
{

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

OUTPUT:

12 | P a g e
13.Write a Program to sum and product of all elements of a matrix.

#include <stdio.h>
int main()
{
int m,n;
printf("Enter the Order of matrix [m][n] : ");
scanf("%d%d",&m,&n);
int a[m][n];
printf("Enter %d elements of matrix: ",m*n);
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("The Entered Matrix is:\n");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
int sum=0;

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

}
printf("Sum of all Elements of Matrix is: %d\n",sum);

int pro=1;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
pro=pro*a[i][j];
}

}
printf("Product of all Elements of Matrix is: %d",pro);
return 0;
}

13 | P a g e
OUTPUT:

14 | P a g e
14.Write a Program for Addition and subtraction of two matrices.

#include <stdio.h>
main()
{
int m, n;
printf("Enter the Order of matrix [m][n]");
scanf("%d%d", &m, &n);
int a[m][n];
int b[m][n];
printf("Enter elements of 1st matrix:");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("Enter elements of 2nd matrix:");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
scanf("%d", &b[i][j]);
}
}
printf("First Matrix is :\n");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
printf("%d\t", a[i][j]);
}
printf("\n");
}
printf("Second matrix is: \n");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
printf("%d\t", b[i][j]);
}
printf("\n");
}
printf("\n");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
printf("Sum is = %d\t", a[i][j] + b[i][j]);

15 | P a g e
}
printf("\n");
}
}

OUTPUT:

16 | P a g e
15.Write a Program to find Area and perimeter of a rectangle.
#include <stdio.h>
int main()
{
int l,b;
printf("Enter Length of Rectangle:");
scanf("%d",&l);
printf("Enter Breadth of Rectangle:");
scanf("%d",&b);

printf("Perimeter is %d\n",2*(l+b));
printf("Area of Rectangle is %d",l*b);
return 0;
}

OUTPUT:

17 | P a g e
16.Write a Program to print a right angle triangle.

#include <stdio.h>
int main ()
{
int r;
printf("Enter number of rows : ");
scanf("%d",&r);
for(int i=1;i<=r;i++){
for(int j=1;j<=i;j++){
printf("*\t");
}
printf("\n");
}
return 0;
}

OUTPUT:

18 | P a g e
17.Write a Program to find HCF of two numbers.

#include <stdio.h>
int main ()
{
int a,b,i;
printf("Enter the smaller number = ");
scanf("%d",&a);
printf("Enter the bigger number = ");
scanf("%d",&b);
{
for(i=a; i>0; i--)
{
if (a%i==0 && b%i==0)
{
printf("The HCF of the two number is : %d",i);
break;
}
}
}

return 0;
}

OUTPUT:

19 | P a g e
18.Write a Program to find if a number is a Palindrome or not.

#include <stdio.h>
int main()
{
int n;
printf("Enter the Number: ");
scanf("%d",&n);
int rev=0;
for(int i=n;i>0;i/=10)
{
int r=i%10;
rev=rev*10+r;
}

if (rev==n)
printf("%d is a Palindrome Number",n);
else
printf("%d is NOT a Palindrome Number",n);
return 0;
}

OUTPUT:

20 | P a g e
19.Write a Program to find if a number is an Armstrong number or not.

#include <stdio.h>
#include <math.h>
int main()
{
int n;
printf("Enter a Number n:");
scanf("%d",&n);

int arm=0,r;
for(int i=n;i>0;i/=10)
{
r=i%10;
arm=arm+pow(r,3);
}
if(arm==n)
printf("The Number %d is an armstrong number",n);
else
printf("The Number %d is not an armstrong number",n);
return 0;
}

OUTPUT:

21 | P a g e
20.Write a Program to calculate Roots of a quadratic equation.

#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c;
printf("Quadratic Equation of form ax^2+bx+c=0");
printf("\nEnter coefficient of x^2 i.e. a:");
scanf("%f", &a);
printf("\nEnter coefficient of x i.e. b:");
scanf("%f", &b);
printf("\nEnter the constant term i.e. c:");
scanf("%f", &c);
printf("Quadratic Equation %.2fx^2+%.2fx+%.2f=0", a, b, c);
float D = b * b - 4 * a * c;
if (D < 0)
{
printf("\nRoots are imaginary");
printf("\nFirst Root=%.2f+sqrt(%.2f)i",-b/2*a,-D/2*a);
printf("\nSecond Root=%.2f-sqrt(%.2f)i",-b/2*a,-D/2* a);
}
else if (D == 0)
{
printf("\nBoth roots are equal");
printf("\nRoot=%.2f", -b / 2 * a);
}
else
{
printf("\nReal Roots Exist");
printf("\nFirst Root=%.2f", (-b + sqrt(D)) / 2 * a);
printf("\nSecond Root=%.2f", (-b - sqrt(D)) / 2 * a);
}

return 0;
}

22 | P a g e
OUTPUT:

23 | P a g e
21.Write a Program to print percentage of marks scored out of 100.

#include <stdio.h>
int main()
{
float x1,x2,x3,x4,x5;
printf("Enter the marks of your 5 subjects : ");
scanf("%f%f%f%f%f",&x1,&x2,&x3,&x4,&x5);
float Percentage = (x1+x2+x3+x4+x5)/5;
printf ("Your percentage is = %.2f",Percentage);
return 0;
}

OUTPUT:

24 | P a g e
22.Write a Program for searching in a linear Array.

#include <stdio.h>

int main()
{
int n;
printf("Enter number of elements:");
scanf("%d", &n);
int a[n];
printf("Enter the %d elements.\n",n);
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
printf("The array is given as:");

for (int i = 0; i < n; i++)


{
printf("%d ", a[i]);
}
int loc = -1, num;
printf("\nEnter the Number you want to find : ");
scanf("%d", &num);
int i;
for (i = 0; i < n; i++)
{
if (a[i] == num)
{
loc = i;
printf("Location found is %d", loc);
break;
}
}
if (loc == -1)
{
printf("Number Not Found");
}

return 0;
}

25 | P a g e
OUTPUT:

26 | P a g e
23.Write a Program to check if a number is prime.

#include <stdio.h>
int main()
{
int n, i;
printf("Enter a Number:");
scanf("%d", &n);
for (i = 2; i <= n; i++)
{
if (n % i == 0)
break;
}
if (i != n)
{
printf("%d is NOT prime Number", n);
}
else
{
printf("%d is a prime Number", n);
}
return 0;
}

OUTPUT:

27 | P a g e
24.Write a Program to print all the prime numbers between two numbers.

#include <stdio.h>
main()
{
int a, b;
printf("Enter first Number:");
scanf("%d", &a);
printf("Enter second Number:");
scanf("%d", &b);
int i, j, c = 0;
printf("All Prime Numbers Between %d and %d are:\n", a, b);
for (i = a; i <= b; i++)
{
for (j = 2; j <= i; j++)
{
if (i % j == 0)
break;
}
if (j == i)
{
c++;
printf("(%d.)\t%d\n", c, i);
}
}
printf("Number of Primes between %d and %d is %d", a, b, c);
}

28 | P a g e
OUTPUT:

29 | P a g e
25.Write a Program to make a basic Calculator.

#include <stdio.h>

int main() {
char operator;
float num1, num2, result;

printf("Enter operator (+, -, *, /): ");


scanf(" %c", &operator);

printf("Enter two numbers: ");


scanf("%f %f", &num1, &num2);

switch(operator) {
case '+':
result = num1 + num2;
printf("Result: %.2f\n", result);
break;
case '-':
result = num1 - num2;
printf("Result: %.2f\n", result);
break;
case '*':
result = num1 * num2;
printf("Result: %.2f\n", result);
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
printf("Result: %.2f\n", result);
} else {
printf("Error: Division by zero\n");
}
break;
default:
printf("Error: Invalid operator\n");
}

return 0;
}

30 | P a g e
OUTPUT:

31 | P a g e

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy