Codetantra 😒 few important codes
/////*******USE DESKTOP SITE WHILE USING IN MOBILE ***********/////////////
1. Write the code to find the largest and the least of some numbers given by the user using arrays.
#include <stdio.h>
int main(){
int n,i,min,max;
printf("Enter total number of elements: ");
scanf("%d",&n);
int a[n];
// scanf("%d",a[i]);
printf("Enter the number: ");
scanf("%d",&a[i]);
// printf("%d",a[i]);
}
min = max = a[0];
for(i=1;i<n;i++){
if(a[i]>max){
max = a[i];
}
if(a[i]<min){
min = a[i];
}
}
printf("The largest number is: %d\n",max);
printf("The smallest number is: %d\n",min);
}
2. Write a C program to compute the Sum of the Series 1 + x + x2 + x3 + …………. +xn
#include <stdio.h>
#include <math.h>
int main(){
int x,n,sum = 0,a=0;
printf("Enter x and n : ");
scanf("%d%d",&x,&n);
// printf("%f%f\n",x,n);
if (x<0 || n<0){
printf("Illegal values\n");
}
else
{
while(a<=n){
sum = sum+ pow(x,a);
a++;
}
printf("Sum of series : %d\n",sum);
}
}
// printf("Sum of series : %d",sum);
// printf("Sum of series : %d",sum);
3. Write an algorithm, draw a flowchart and to write a C program to find Largest of given three numbers.
#include <stdio.h>
int main(){
int smallest , largest,a,b,c,max,min;
printf("Enter 3 integers: ");
scanf("%d%d%d",&a,&b,&c);
max = a;
//min = a;
largest = (b>max && b>c)? b: (c>max && c>b) ? c :a ;
//smallest = (b<min && b<c)? b: (c<min && c <b)? c : a;
printf("%d is largest\n",largest);
printf("%d is smallest\n",smallest);
return 0;
}
4. Write an algorithm, draw a flowchart and to write a C program to find the Lateral surface area of a right circular cone.
#include <stdio.h>
#define PI 3.14
int main(){
float radius, lsa,height;
printf("radius, height : ");
scanf("%f%f",&radius,&height);
lsa = PI * radius * sqrt(radius * radius + height * height);
printf("surface area : %f\n",lsa);
}
5. Write a C program to find the Largest and Smallest of Three numbers using Ternary operator
#include <stdio.h>
int main(){
int smallest , largest,a,b,c,max,min;
printf("Enter 3 integers: ");
scanf("%d%d%d",&a,&b,&c);
max = a;
min = a;
largest = (b>max && b>c)? b: (c>max && c>b) ? c :a ;
smallest = (b<min && b<c)? b: (c<min && c <b)? c : a;
printf("%d is largest\n",largest);
printf("%d is smallest\n",smallest);
return 0;
}
6. Write a C program to find the Sum of 1! + 2! + 3! + .... + n!
#include <stdio.h>
int main(){
int n,product=1,sum=0,i;
printf("Enter n value : ");
scanf("%d",&n);
i = 1;
while(i<=n){
product *=i;
sum = sum + product;
i++;
}
printf("Sum of series : %d\n",sum);
}
7. Write a C program to find Octal and Hexadecimal of a given Decimal integer.
#include <stdio.h>
int main(){
int a;
printf("Enter a decimal integer : ");
scanf("%d",&a);
printf("Octal number : %o\n",a);
printf("Hexadecimal number : %x\n",a);
}
8. Write an algorithm, draw a flowchart and to write a C program to find all Roots of a Quadratic equation.
#include <stdio.h>
#include <math.h>
int main(){
float a,b,c ,d,i;
float r1,r2;
printf("Enter coefficients a, b and c : ");
scanf("%f%f%f",&a,&b,&c);
d = (b*b) - (4*a*c);
i = (sqrt(-d))/(2*a);
if (d>0 && a!=0){
printf("The roots are real and distinct\n");
r1 = (-b + sqrt(d))/(2*a);
r2 = (-b- sqrt(d))/(2*a);
printf("root1 = %f and root2 = %f\n",r1,r2);
}
else if(b!=0 && a==0){
r1 =r2= -c/b;
printf("Linear equation\n");
printf("Root = %f\n",r1);
}
else if(a==0 && b==0&&c==0){
printf("Invalid coefficients\nEnter valid inputs\n");
}
else if(d<0){
r1 =(-b/(2*a));
r2 = (-b/(2*a));
printf("The roots are real and imaginary\n");
printf("root1 = %f+i%f\n",r1,fabs(i));
printf("root2 = %f-i%f\n",r2,fabs(i));
}
else if(d==0){
printf("The roots are real and equal\n");
r1 =r2= -b/2*a;
printf("root1 = root2 = %f\n",r1);
}
}
9. Write the code to print a multiplication table for a given number using
a) while loop b) for loop
#include <stdio.h>
int main(){
int table,i,res;
i = 1;
printf("Enter a number: ");
scanf("%d",&table);
while(i<=10){
printf("%d * %d = %d\n",table,i, table*i);
i++;
}
}
10. Write an algorithm, draw a flowchart and to write a C program to find the Volume and Total surface area of the Sphere
11. Write an algorithm, draw a flowchart and to write a C program for converting a given Celsius temperature to its equivalent Fahrenheit temperature
12. Write a C Program to find the Sum of Series 1- X2/2! + X4/4! - X6/6! + X8/8! - X10/10!
#include <stdio.h>
#include <math.h>
int main()
{
int n,fact=1,sign=-1;
float x,sum=0;
printf("Enter value of x : ");
scanf("%f",&x);
for(n=2;n<=10;n+=2){
fact = fact * n *(n-1);
sum += (sign * pow(x,n))/(fact);
sign *= -1;
}
sum++;
printf("Sum of series : %f\n",sum);
return 0;
}
13. Write a C program to find the Sum of individual digits of a given number.
#include <stdio.h>
int main(){
int sum=0,n,lastdigit;
printf("Enter an integer : ");
scanf("%d",&n);
printf("The sum of digits of the given number %d ",n);
while(n!=0){
lastdigit = n % 10;
sum += lastdigit;
n/=10;
}
printf("= %d\n",sum);
}
14. Write the code to perform the operations +, -, *, /, % between two given integers using switch statement.
#include <stdio.h>
int main(){
int a,b,c;
do{
printf("Enter the first number: ");
scanf("%d",&a);
printf("Enter the second number: ");
scanf("%d",&b);
printf("1.Addition\n");
printf("2.Subtraction\n");
printf("3.Multiply\n");
printf("4.Divide\n");
printf("5.Modulus\n");
printf("6.Exit\n");
printf("Enter your choice: ");
scanf("%d",&c);
switch(c){
case 1:
printf("The addition of 2 numbers is: %d\n",a+b);
break;
// fun();
case 2:
printf("The differnce of 2 numbers is: %d\n",a-b);
break;
// fun();
case 3:
printf("The product of 2 numbers is: %d\n",a*b);
break;
// fun();
case 4:
printf("The division of 2 numbers is: %d\n",a/b);
break;
// fun();
case 5:
printf("The modulus of 2 numbers is: %d\n",a%b);
// fun();
}
}while(c<=5);
}
15. Write a C program to Find the Areas of shapes like Square, Circle, Triangle and Rectangle.
/* Write your complete code here and Map your output with the visible as well as
hidden test cases.*/
#include <stdio.h>
#define PI 3.14
#include <math.h>
int main(){
float sideLength, radius, height1,height,base,width,areaS,AOC,AOT,AOR;
printf("side length: ");
scanf("%f",&sideLength);
printf("radius: ");
scanf("%f",&radius);
printf("base, height: ");
scanf("%f%f",&base,&height1);
printf("width, height: ");
scanf("%f%f",&width,&height);
areaS = sideLength*sideLength;
AOC = PI * radius * radius;
AOT = 0.5* base * height1;
AOR = width * height;
printf("Area of square: %.2f\n",areaS);
printf("Area of circle: %.2f\n",AOC);
printf("Area of triangle: %.2f\n",AOT);
printf("Area of rectangle: %.2f\n",AOR);
return 0;
}
}
Comments
Post a Comment