Lab 1
C Program to Find Sum and Average of 3 Numbers
#include <stdio.h>
int main(){
int a, b, c, sum;
float avg;
// Asking for input
printf("Enter 3 numbers: \n");
scanf("%d %d %d", &a, &b, &c);
// Calculating sum
sum = a + b + c;
// Calculating average of 3 numbers
avg = sum / 3;
// Displaying output
printf("Sum = %d \n", sum);
printf("Average = %.2f", avg);
return 0;
}
Lab 2
Lab 4 Check weather the given number is armstrong or not
Lab 12
find the greatest common divisor (GCD) for the given two numbers
by using the non-recursive function
Let's see the fibonacci series program in c without recursion.
Lab 3
Program or code for prime numbers between 1 to n in c language
#include<stdio.h>
int main(){
int num,i,count,n;
printf("Enter max range: ");
scanf("%d",&n);
for(num = 1;num<=n;num++){
count = 0;
for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}
}
if(count==0 && num!= 1)
printf("%d ",num);
}
return 0;
}
Sample output:
Enter max range: 50
2 3 5 7 11 13Lab 4 Check weather the given number is armstrong or not
#include <stdio.h>
int main() {
int num, originalNum, remainder, result = 0;
printf("Enter a three-digit integer: ");
scanf("%d", &num);
originalNum = num;
while (originalNum != 0) {
// remainder contains the last digit
remainder = originalNum % 10;
result += remainder * remainder * remainder;
// removing last digit from the orignal number
originalNum /= 10;
}
if (result == num)
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);
return 0;
}
Lab 5
C Program for Arithmetic Operations using Switch Statement
#include<stdio.h> #include<conio.h> int main() { int a,b; int op; printf(" 1.Addition\n 2.Subtraction\n 3.Multiplication\n 4.Division\n"); printf("Enter the values of a & b: "); scanf("%d %d",&a,&b); printf("Enter your Choice : "); scanf("%d",&op); switch(op) { case 1 : printf("Sum of %d and %d is : %d",a,b,a+b); break; case 2 : printf("Difference of %d and %d is : %d",a,b,a-b); break; case 3 : printf("Multiplication of %d and %d is : %d",a,b,a*b); break; case 4 : printf("Division of Two Numbers is %d : ",a/b); break; default : printf(" Enter Your Correct Choice."); break; } return 0; } Lab 6Factorial of a Number Using Recursion
#include<stdio.h>
long int multiplyNumbers(int n);
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}
long int multiplyNumbers(int n) {
if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
}
Lab 7
Program to Add Two Matrices
#include <stdio.h>
int main() {
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);
printf("\nEnter elements of 1st matrix:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
printf("Enter elements of 2nd matrix:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element b%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}
// adding two matrices
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
sum[i][j] = a[i][j] + b[i][j];
}
// printing the result
printf("\nSum of two matrices: \n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", sum[i][j]);
if (j == c - 1) {
printf("\n\n");
}
}
return 0;
}
Lab 8
Sum of digits in c
- #include<stdio.h>
- int main()
- {
- int n,sum=0,m;
- printf("Enter a number:");
- scanf("%d",&n);
- while(n>0)
- {
- m=n%10;
- sum=sum+m;
- n=n/10;
- }
- printf("Sum is=%d",sum);
- return 0;
- }
- /*C program to check whether the given number is the Perfect number*/
- #include<stdio.h>
- #include<conio.h>
- void main()
- {
- // declare and initialize the variables
- int num, rem, sum = 0, i;
- // take an input from the user.
- printf("Enter a number\n");
- scanf("%d", &num);
- // find all divisors and add them
- for(i = 1; i < num; i++)
- {
- rem = num % i;
- if (rem == 0)
- {
- sum = sum + i;
- }
- }
- if (sum == num)
- printf(" %d is a Perfect Number");
- else
- printf("\n %d is not a Perfect Number");
- getch();
- }
Lab 10
- Program to check whether the number is strong or not.
- #include <stdio.h>
- int main()
- {
- int n;
- int sum=0;
- printf("Enter a number");
- scanf("%d",&n);
- int k=n;
- int r;
- while(k!=0)
- {
- r=k%10;
- int f=fact(r);
- k=k/10;
- sum=sum+f;
- }
- if(sum==n)
- {
- printf("\nNumber is a strong");
- }
- else
- {
- printf("\nNumber is not a strong");
- }
- return 0;
- }
- int fact(int r)
- {
- int mul=1;
- for(int i=1;i<=r;i++)
- {
- mul=mul*i;
- }
- return mul;
- }
Lab 11
Program to Find Roots of a Quadratic Equation
#include <math.h>
#include <stdio.h>
int main() {
double a, b, c, discriminant, root1, root2, realPart, imagPart;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf", &a, &b, &c);
discriminant = b * b - 4 * a * c;
// condition for real and different roots
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("root1 = %.2lf and root2 = %.2lf", root1, root2);
}
// condition for real and equal roots
else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
printf("root1 = root2 = %.2lf;", root1);
}
// if roots are not real
else {
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imagPart, realPart, imagPart);
}
return 0;
}Lab 12
GCD of Two Numbers using Recursion
#include <stdio.h>
int hcf(int n1, int n2);
int main() {
int n1, n2;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("G.C.D of %d and %d is %d.", n1, n2, hcf(n1, n2));
return 0;
}
int hcf(int n1, int n2) {
if (n2 != 0)
return hcf(n2, n1 % n2);
else
return n1;
}by using the non-recursive function
#include<stdio.h> #include<conio.h> #include<math.h> int gcdnonR(int i,int j){ int rem; rem=i-(i/j*j); if(rem==0) return j; else gcdnonR(j,rem); } void main(){ int a,b; printf("enter the two numbers:"); scanf("%d%d",&a,&b); printf("GCD of %d",gcdnonR(a,b)); getch(); }
No comments:
Post a Comment