0% found this document useful (0 votes)
238 views44 pages

C Programs for Basic Algorithms

1. The document contains 10 code snippets that solve various programming problems. The first code snippet writes a program to reverse a 3 digit number and check if it is a palindrome without using loops. The second code snippet checks if 3 points fall on a straight line or form an equilateral triangle. The third determines if a character is a capital letter, small letter, digit or symbol. The remaining code snippets solve problems related to calculating areas of shapes, determining weight classes, calculating BMI, state of matter, grade of steel, maximum/minimum/average temperature, and calculating gratuity.
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)
238 views44 pages

C Programs for Basic Algorithms

1. The document contains 10 code snippets that solve various programming problems. The first code snippet writes a program to reverse a 3 digit number and check if it is a palindrome without using loops. The second code snippet checks if 3 points fall on a straight line or form an equilateral triangle. The third determines if a character is a capital letter, small letter, digit or symbol. The remaining code snippets solve problems related to calculating areas of shapes, determining weight classes, calculating BMI, state of matter, grade of steel, maximum/minimum/average temperature, and calculating gratuity.
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/ 44

1.

Write a program to reverse a three digit number without


using loop and check if thenumber is a palindrome number.
#include <stdio.h>
#include <conio.h>
void main()
{
int n,t,x,c;
printf ("C program to reverse a 3 digit number without loops and check\n");
printf ("whether it is a palindrome or not -NIVASHINI VINDHYA S-200201031 ");
printf ("\n\n");
printf ("enter a 3 digit number\n");
scanf ("%d" , &n);
printf ("The reversed number is: \n");
t=n%10;
printf ("%d",t);
t=n/10;
t=t%10;
printf("%d",t);
t=n/10;
t=t/10;
printf("%d",t);
printf ("\n");
x=n%10;
n=n/10;
c=n/10;
if (x==c)
printf ("The number is a palindrome.");
else
printf ("The number is not a palindrome.");
}

2. Given three points (x1, y1), (x2, y2) and (x3, y3), write a
program to check if all thethree points fall on one straight
line or together they make a equilateral triangle.
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int x1, y1, x2, y2, x3, y3;
double ab, bc, ac, abc;
printf("Enter the co-ordinates of first point (X1, Y1): ");
scanf("%d %d", &x1, &y1);
printf("Enter the co-ordinates of second point (X2, Y2): ");
scanf("%d %d", &x2, &y2);
printf("Enter the co-ordinates of third point (X3, Y3): ");
scanf("%d %d", &x3, &y3);
ab = sqrt(pow(x2-x1,2)+pow(y2-y1,2));
bc = sqrt(pow(x3-x2,2)+pow(y3-y2,2));
ac = sqrt(pow(x3-x1,2)+pow(y3-y1,2));
printf("ab: %f\t bc: %f\t ac: %f\n",ab, bc, ac);
abc = ab+bc;
if(abc==ac)
{
printf("ab + bc = ac\n");
printf("All the three points fall on one straight line.");
}
else
printf("All the three points are not present on one straight line.");
int side1=ab,side2=bc,side3=ac;
if(side1==side2 && side2==side3)
printf("\n\n forms an equilateral triangle\n");
else
printf("\n does not form an equilateral triangle\n");
return 0;

3. write a program to determine whether the character


entered is a capital letter, a smallcase letter, a digit or a
special symbol.
#include <stdio.h>
int main()
{
char ch;
printf("\n\nNIVASHINI VINDHYA S-200201031\n\n");
printf("enter a charachter\n");
scanf("%c",&ch);
if (ch>='a' && ch<='z')
printf("the charachter is a small letter\n");
else if(ch>='A' && ch<='Z')
printf("the charachter is a capital letter\n");
else if
(ch=='~'||ch=='!'||ch=='@'||ch=='#'||ch=='$'||ch=='%'||ch=='^'||ch=='&'||c
h=='*')
printf("the charachter is a special charachter\n");
else if (ch>='0' && ch<='9')
printf("the charachter is a digit\n");
return 0;
}

4. Write a Menu driven program to compute the area of any


five geometrical shapes.
#include <stdio.h>
void main()
{
int option;
float side, base, length, breadth, height, area, radius,AOT,b1,b2,h;
printf("NIVASHINI VINDHYA S-200201031\n\n");
printf("-------------------------\n");
printf(" 1 --> Circle\n");
printf(" 2 --> Rectangle\n");
printf(" 3 --> Triangle\n");
printf(" 4 --> Square\n");
printf("5--> Trapezium\n");
printf("-------------------------\n");
printf("Enter the option\n");
scanf("%d", &option);
switch(option)
{
case 1:
printf("Enter the radius\n");
scanf("%f", &radius);
area = 3.142 * radius * radius;
printf("Area of a circle = %f\n", area);
break;
case 2:
printf("Enter the breadth and length\n");
scanf("%f %f", &breadth, &length);
area = breadth * length;
printf("Area of a Reactangle = %f\n", area);
break;
case 3:
printf("Enter the base and height\n");
scanf("%f %f", &base, &height);
area = 0.5 * base * height;
printf("Area of a Triangle = %f\n", area);
break;
case 4:
printf("Enter the side\n");
scanf("%f", &side);
area = side * side;
printf("Area of a Square=%f\n", area);
break;
case 5:
printf("enter the size of two bases\n");
scanf("%f %f",&b1,&b2);
printf("enter the height\n");
scanf("%f",&h);
AOT=((b1+b2)*h)/2;
printf("\n Area of Trapezium = %f", AOT);
break;
default:
printf("Error in option selection\n");
break;
}
}

5. Write a program that receives weight as input and prints


out the boxer’s weight class based on the table.
#include<stdio.h>
int main()
{
int a;
printf("\n\nNIVASHINI VINDHYA S-200201031\n\n");
printf ("\nenter the weight \n");
scanf("%d" , &a);
if (a<115)
printf("Flyweight\n");
else if (a>=115 && a<=121)
printf("Bantamweight \n");
else if (a>=122 && a<=153)
printf("Featherweight \n");
else if (a>=154 && a<=189)
printf("Middleweight \n");
else if(a>=190)
printf("\nHeavyweight");
return 0;
}

6. Write a program that receives weight and height,


calculates the BMI, and reports the BMI category as per the
following table:
#include<stdio.h>
#include<conio.h>
#include<math.h>
//Weight in Kilograms
//Height in Meters
int main()
{
float weight, height, BMI;
printf("\n\nNIVASHINI VINDHYA S-200201031\n\n");
printf("Enter the weight of person: ");
scanf("%f", &weight);

printf("Enter the height of person: ");


scanf("%f", &height);
BMI = weight/(pow(height, 2));
printf("The BMI of person is: %.2f\n", BMI);

if (BMI>0 && BMI<=15)


printf("BMI Category is Starvation");

else if (BMI>=15.1 && BMI<=17.5)


printf("BMI Category is Anorexic");

else if (BMI>17.6 && BMI<=18.5)


printf("BMI Category is Underweight");

else if (BMI>18.6 && BMI<=24.9)


printf("BMI Category is Ideal");

else if (BMI>25 && BMI<=29.9)


printf("BMI Category is Overweight");

else if (BMI>30 && BMI<=30.9)


printf("BMI Category is Obese");
else if (BMI>=40)
printf("BMI Category is Morbidly Obese");
}

7. Use an appropriate control structure that will examine


the value of a floating-point variable called temperature
and print one of the following messages, depending on the
value assigned to temperature .
● ICE, if the value of temp is less than 0.
● WATER, if the value of temp lies between 0and 100.
● STEAM, if the value of temp exceeds 100.
#include<stdio.h>
int main()
{
float temperature;
printf("\n\nNIVASHINI VINDHYA S-200201031\n");
printf("\nenter the temperature in degrees:");
scanf("%f",&temperature);
if(temperature<=0)
printf("\nICE\n");
else if(temperature>=100)
printf("\nSTEAM\n");
else
printf("\nWATER\n");
return 0;
}

8. Write a program, which will require the user to give


values of hardness, carbon content and tensile strength of
the steel under consideration and output the grade of the
steel.
● Hardness must be greater than 50
● Carbon content must be less than 0.7
● Tensile strength must be greater than 5600
The grades are as follows:
1. Grade is 10 if all three conditions are met
2. Grade is 9 if conditions (i) and (ii) are met
3. Grade is 8 if conditions (ii) and (iii) are met
4. Grade is 7 if conditions (i) and (iii) are met
5. Grade is 6 if only one condition is met
6. Grade is 5 if none of the conditions are met
#include<stdio.h>
#include<conio.h>
int main()
{
int hd,ts;
float cc;
printf("\n\nNIVASHINI VINDHYA S-200201031\n\n");
printf("Enter the input of steel\n");
printf("Enter the hardness\n");
scanf("%d",&hd);
printf("\n Enter the carbon content\n");
scanf("%d",&cc);
printf("\n Enter the tensile strength\n");
scanf("%f",&ts);
if(hd>50 && cc<0.7 && ts>5600)
printf("\n Grade of steel:10");
else if(cc<0.7 && hd>50)
printf("\nGrade of steel:9");
else if(cc<0.7 && ts>5600)
printf("\nGrade of steel:8");
else if(hd>50 && ts>5600)
printf("\nGrade of steel:7");
else if(hd>50 || hd>50||ts>5600)
printf("\nGrade of steel:6");
else
printf("\nGrade of steel is:5");
return 0;
}

9. The temperature for the city was recorded over 60 days.


Write suitable functions to calculate the maximum,
minimum and average temperature.
/*C program to write suitable functions to calculate the maximum, minimum
and
average temperature – --NIVASHINI VINDHYA S -- 200201031*/
#include <stdio.h>
#define NUM 60
void max(int a[]);
void min(int a[]);
void avg(int a[]);
void main()
{
int
a[]={12,23,34,45,56,67,78,89,11,22,33,44,55,66,77,88,99,98,87,76,65,54,43,32,
21,
13,14,15,16,17,18,19,10,23,24,25,26,27,28,29,34,35,36,37,38,39,31,32,56,67,
67,35,79,34,12,34,67,48,36,68
};
int i;
printf ("C program to write suitable functions to calculate the maximum,\n");
printf ("minimum and average temperature – --NIVASHINI VINDHYA S --
200201031");
printf ("\n\n");
printf ("The recorded temperatures for 60 days are listed below.");
printf ("\n\n");
for (i=0;i<60;i++)
printf ("%d " , a[i]);
printf ("\n\n");
max(a);
printf("\n\n");
min(a);
printf ("\n\n");
avg(a);
}
void max(int a[])
{
int max,i;
max=a[0];
for (i=0;i<NUM;i++)
{
if (a[i]>max)
max=a[i];
}
printf ("The maximum temperature is = %d",max);
}
void min(int a[])
{
int min,i;
min=a[0];
for (i=0;i<NUM;i++)
{
if (a[i]<min)
min=a[i];
}
printf ("The minimum temperature is = %d",min);
}
void avg(int a[])
{
int i,sum;
float avg;
sum=0;
for (i=0;i<NUM;i++)
{
sum=sum+a[i];
}
avg=sum/NUM;
printf ("The average temperaure is = %f", avg);
}

10. The amount of gratuity for employees whose employer


is covered under the Gratuity Act can be calculated using
the formula: Gratuity = n*b*15 / 26. Where n = Tenure of
service completed in the company. b = Last drawn basic
salary + dearness allowance. Show how data can be
manipulated using pass by reference.
/*C program to calculate Gratuity – --NIVASHINI VINDHYA S -- 200201031 */
#include <stdio.h>
void gra(int *n, int *bs , int *da ,int *b,int *g);
void main()
{
int g,bs,b,da,n;
printf ("C program to calculate Gratuity – --NIVASHINI VINDHYA S --
200201031");
printf ("\n\n");
printf ("Enter the value of Tenure of service \n");
scanf("%d" , &n);
printf ("Enter the value of basic salary \n");
scanf("%d" , &bs);
printf ("Enter the value of dearness allowance \n");
scanf ("%d" ,&da);
gra(&n,&bs,&da,&b,&g);
printf ("Gratuity = %d" , g);
}
void gra(int *n, int *bs , int *da ,int *b,int *g)
{
*b=*bs+*da;
*g=((*n)*(*b)*15)/26;
}

11. Write a ‘C’ program to print the count of students under


each age group in your class.
#include <stdio.h>
int n,age[100];
void agegrp();
void main()
{
int i;
printf ("C program to print the count of different age groups \n");
printf (" – --NIVASHINI VINDHYA S -- 200201031\n");
printf ("\nEnter the number of students \n");
scanf ("%d" , &n);
printf ("\nEnter %d ages\n",n);
for (i=0;i<n;i++)
{
scanf ("%d" , &age[i]);
}
agegrp();
}
void agegrp()
{
int i,count,count1;
count=count1=0;
for (i=0;i<n;i++)
{
if(age[i]>=11 && age[i]<=19)
count++;
else if(age[i]>=20 && age[i]<=29)
count1++;
}
printf ("Number of students in the age group 11-19 is: %d \n",count);
printf ("Number of students in the age group 20-29 is: %d\n",count1);
}

12. Write a recursive function to calculate GCD of given two


numbers.
/*C program to find GCD using recursion – NIVASHINI VINDHYA .S --
200201031*/
#include <stdio.h>
int gcd(int a, int b);
void main()
{
int a, b, result;
printf ("C program to find GCD using recursion – NIVASHINI VINDHYA S --
200201031\n");
printf("\nEnter two numbers :\n");
scanf("%d %d", &a, &b);
result = gcd(a, b);
printf("The GCD of %d and %d is %d.\n", a, b, result);
}
int gcd(int a, int b)
{
if (b == 0) return a;
return gcd(b, a % b);
}

13. Write a program for interchanging the values of 2


numbers without using third variable.
#include <stdio.h>
int main()
{
int a,b,c;
printf("a,b:\n");
scanf("%d %d",&a,&b);
c=a;
a=b;
b=c;
printf("interchanged variables are:\n%d\n%d",a,b);
return 0;
}

14. Design an application to find the youngest and oldest


student in your class.
#include <stdio.h>
#include <conio.h>
void main()
{
char a[100][100];
int b[100],n,i,j,max,l,k,min;
clrscr();
printf ("C program to find the youngest and oldest student \n");
printf ("in the class – NIVASHINI VINDHYA S -- 200201031\n");
printf ("NOTE: Enter the names first and enter their corresponding ");
printf ("ages next. \n");
printf (" \nEnter the number of entries. ");
scanf ("%d" , &n);
printf ("Enter the names of the students. \n");
for (j=0;j<n;j++)
scanf ("%s" , &a[j]);
printf ("Enter the age of the students. \n");
for (i=0;i<n;i++)
scanf ("%d" , &b[i]);
max=b[0];
for (i=0;i<n;i++)
{
if (b[i]>max)
{
max=b[i];
l=i;
}
}
printf ("The oldest student is %s with age %d.\n",a[l],max);
min=b[0];
for (i=0;i<n;i++)
{
if (b[i]<min)
{
min=b[i];
k=i;
}
}
printf ("The youngest student is %s with age %d.",a[k],min);
}

15. The basic salary of an employee is input through the


keyboard. His dearness allowance is
40% of basic salary, and house rent allowance is 20% of
basic salary. Write a program to
calculate his gross salary
#include <stdio.h>
int main()
{
int dearness_allowance,house_rent,gross_salary,s;
printf("Reg no:200201031\n");
printf("\nenter basic salary=");
scanf("%d",&s);
dearness_allowance=(40*s)/100;
house_rent=(20*s)/100;
gross_salary=(s+dearness_allowance)-house_rent;
printf("\n gross salary = %d",gross_salary);
return 0;
}

16. Write a ‘C” program to evaluate the following


expressions
A.
#include <stdio.h>
#include <conio.h>
int main()
{
int x,y,a,b,c,d,Z;
printf("\nReg no:200201031\n");
printf ("\nEnter 2 values for x and y =");
scanf ("%d%d",&x,&y);
a=x+3;
b=x*x*x;
c=y-4;
d=y+5;
Z=(a*b)/(c*d);
printf ("The answer for the expression");
printf ("((x+3)*(x^3)/(y-4)(y+5)) is : \n");
printf ("%d",Z);

B.
#include <stdio.h>
int main()
{
int v,c,d,g,R,x,y,z;
printf("Reg no:200201031\n");
printf ("\nEnter a value for v\n");
scanf ("%d", &v);
printf ("Enter a value for c\n");
scanf ("%d", &c);
printf ("Enter a value for d\n");
scanf ("%d", &d);
printf ("Enter a value for g\n");
scanf ("%d", &g);
x=2*v;
y=6.22*(c+d);
z=g+v;
R=(x+y)/z;
printf ("The value for the expression");
printf(" R = (2v+6.22(c+d)/(g+v)) is:\n");
printf ("R = %d",R);
}

C.
#include <stdio.h>
int main()
{
int A,a,b,c,x,y,s1,s4,s2,s3;
printf("Reg no:200201031\n");
printf ("\nEnter a value for a : ");
scanf("%d",&a);
printf ("Enter a value for b : ");
scanf("%d",&b);
printf ("Enter a value for c :");
scanf("%d",&c);
printf ("Enter a value for y :");
scanf("%d",&y);
printf ("Enter a value for x :");
scanf("%d",&x);
s1=(7.7*b)*((x*y)+a);
s2= c-0.8+(2*b);
s3=x+a;
s4=(1/y);
A=((s1/s2)/(s3*s4));
printf ("The value for the expression ");
printf("A = [{7.7b(xy+a)}/(c-0.8+2b)]/{(x+a)(1/y)} is :\n");
printf ("A = %d",A);
}

D.
#include <stdio.h>
int main()
{
int X,x,s1,s2,s3,s4;
printf("Reg no:200201031\n");
printf ("\nEnter a value for x:");
scanf ("%d" , &x);
s1=((12*(x*x*x))/(4*x));
s2=((8*(x*x))/(4*x));
s3=(x/(8*x));
s4=(8/(8*x));
X=s1+s2+s3+s4;
printf ("The value for the expression");
printf(" X = (12x^3/4x)+(8x^2/4x)+(x/8x)+(8/8x) is: \n");
printf ("%d",X);
}

17. Given the angle, write a ‘C’ program to print all


trigonometric ratio
#include<stdio.h>
#include<math.h>
int main()
{
int degree, rad;
const float PI = 3.14159;
printf("Reg no:200201031\n");
printf("\nEnter angle in degree:");
scanf("%d", &degree);
rad = degree * (PI / 180.0);
printf("sin(%f) = %f\n", degree, sin(rad));
printf("cos(%f) = %f\n", degree, cos(rad));
printf("tan(%f) = %d\n", degree, tan(rad));
printf("cosec(%d) = %d\n", degree, 1/sin(rad));
printf("sec(%d) = %d\n", degree, 1/cos(rad));
printf("cot(%d) = %d\n", degree, 1/tan(rad));
return 0;
}

18. Write a C program to check a number is even or odd


using % operator.
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if(num % 2 == 0)
printf("%d is even number.", num);
else
printf("%d is odd number.", num);
return 0;
}

19. Write a C program to check a number is even or odd


using conditional operator.
#include < stdio.h >
int main()
{
int n;
printf("Enter an integer number\n");
scanf("%d", &n);
(n % 2 == 0) ? (printf("%d is Even number\n", n)) : (printf("%d is Odd
number\n", n));
return 0;
}

20. Write a C program to perform basic arithmetic


operations which are addition, subtraction,
multiplication and division of two numbers.
#include <stdio.h>
int main()
{
int num1, num2;
int sum, sub, mult, mod;
float div;

printf("Input any two numbers separated by comma : ");


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

sum = num1 + num2;


sub = num1 - num2;
mult = num1 * num2;
div = (float)num1 / num2;
mod = num1 % num2;

/*
* Prints the result of all arithmetic operations
*/
printf("The sum of the given numbers : %d\n", sum);
printf("The difference of the given numbers : %d\n", sub);
printf("The product of the given numbers : %d\n", mult);
printf("The quotient of the given numbers : %f\n", div);
printf("MODULUS = %d\n", mod);

return 0;
}

21. Execute a C code to check leap year, year will be entered


by the user.
#include <stdio.h>
int main()
{
int year;
printf("Enter a year :");
scanf("%d" ,&year);
if(year%4==0)
printf("%d is a leap year",year);
else
printf(" is not a leap year");
}

22. C program to calculate sum of digits of a number.


#include <stdio.h>
int main()
{
int a,b,num;
printf("enter 2 numbers: ");
scanf("%d %d", &a,&b);

num=a+b;
printf("The sum of the 2 numbers is %d",num);
}

23. Write a program for interchanging the values of 2


numbers using third variable.
#include <stdio.h>
int main()
{
int a,b,c;
printf("a,b:\n");
scanf("%d %d",&a,&b);
c=a;
a=b;
b=c;
printf("interchanged variables are:\n%d\n%d",a,b);
return 0;
}

24.Write a program to reverse the number entered by the


user and then print it on the screen.
For example, if user will enter 123 as input, then 321 will be
printed as output.
#include <stdio.h>
int main()
{
int n, rev = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
while (n != 0) {
remainder = n % 10;
rev = rev * 10 + remainder;
n /= 10;
}
printf("Reversed number = %d", rev);
return 0;
}

25.C program to check for palindrome number.


#include <stdio.h>
int main() {
int n, reversed = 0, remainder, original;
printf("Enter an integer: ");
scanf("%d", &n);
original = n;

// reversed integer is stored in reversed variable


while (n != 0) {
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}
// palindrome if orignal and reversed are equal
if (original == reversed)
printf("%d is a palindrome.", original);
else
printf("%d is not a palindrome.", original);

return 0;
}

26. C program to print the following pattern in C


*
**
***
****
#include <stdio.h>
int main()
{
int j,i;
for(i=1;i<=4;i++)
{
for(j=0;j<i;j++)
{
printf("*");
}
printf("\n");
}
return 0;
}

27. Write a program to performs addition of two numbers


using pointers.
#include <stdio.h>
int main()
{
int first, second, *p, *q, sum;
printf("Enter two integers to add\n");
scanf("%d%d", &first, &second);
p = &first;
q = &second;
sum = *p + *q;
printf("Sum of the numbers = %d\n", sum);
return 0;
}

28. Code finds maximum or largest element present in an


array using function.
#include <stdio.h>
int main()
{
int n,i ;
int mx=1,mn,a[30];
printf("Enter the values of N ");
scanf("%d",&n);
printf("Enter the values\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
mn=a[0];
for(i=0;i<n;i++)
{
if(mx<a[i])
mx=a[i];
else if(mn>a[i])
mn=a[i];
else continue ;

}
printf("\nThe maximum number in the array is %d",mx);
printf("\nThe minimum number in the array is %d",mn);

return 0;
}

29. C program to reverse a string without using strrev().


#include<stdio.h>
#include<string.h>
void main()
{
int i,n;
char str[20];
printf("Enter the String to get reversed\n");
gets(str);
n=strlen(str);
printf("\nReversed string is \n");
for(i=n-1;i>=0;i--)
{
printf("%c",str[i]);
}
}

30. C program to check a number is +ve or -ve using


function.

Ans(without function)
#include <stdio.h>
int main()
{
int num;

/* Input number from user */


printf("Enter any number: ");
scanf("%d", &num);

if(num > 0)
{
printf("Number is POSITIVE");
}
if(num < 0)
{
printf("Number is NEGATIVE");
}
if(num == 0)
{
printf("Number is ZERO");
}

return 0;
}

31. C program to count number of digits in a number.


#include <stdio.h>
int main() {
long long n;
int count = 0;
printf("Enter an integer: ");
scanf("%lld", &n);
// iterate at least once, then until n becomes 0
// remove last digit from n in each iteration
// increase count by 1 in each iteration
do {
n /= 10;
++count;
} while (n != 0);
printf("Number of digits: %d", count);
}

32. Write a program to print sum of first 10 natural


numbers.
#include <stdio.h>
void main()
{
int j, sum = 0;

printf("The first 10 natural number is :\n");


for (j = 1; j <= 10; j++)
{
sum = sum + j;
printf("%d ",j);
}
printf("\nThe Sum is : %d\n", sum);
}

33. Write a program to print sum of first 10 natural numbers


using function.
#include <stdio.h>
int sum(n);
void main()
{
int n=10;
sum(n);
}
int sum(int n)
{
int formula;
formula=(n*(n+1))/2;
printf("sum=%d",formula);
return 0;
}

34. C program to find square and cube of a given number.


#include<stdio.h>
int main(){
int n;
printf("Enter a number");
scanf("%d",&n);
printf("\nSquare of the number %d is %d",n,n*n);
printf("\nCube of the number %d is %d",n,n*n*n);
return 0;
}

35. C program to find length of a string without using


strlen().
#include <stdio.h>
int main()
{
char s[100];
int i;
printf(“Enter a string: “);
scanf(“%s”, s);
for(i = 0; s[i] != ‘\0’; ++i);
printf(“Length of string: %d”, i);
return 0;
}

36. Write a program to compute the sum of two matrices of


order mXn and then print it.
#include <stdio.h>
int main()
{
int m1[5][5],m2[5][5],i,j,r1,r2,c2,c1,sub[i][j];
printf("r1 c1 \n");
scanf("%d %d",&r1,&c1);
printf("r2 c2\n");
scanf("%d %d",&r2,&c2);
printf("elts of m1\n ");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&m1[i][j]);
}
}
printf("elts of m2\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("%d",&m2[i][j]);
}
}
if(r1==r2 && c1==c2)
{
for(i=0;i<r1;i++)
{
printf("\n");
for(j=0;j<c1;j++)
{
sub[i][j]=m1[i][j]-m2[i][j];
printf("%d\t",sub[i][j]);
}
}
}
else
{
printf("not possible ");
}
return 0;
}

37. same as 36 just subtract


38. Write a program to print vowel and consonants count.
#include <stdio.h>
int main()
{
//Counter variable to store the count of vowels and consonant
int i, vCount = 0, cCount = 0;
char str[] = "This is a really simple sentence";
for(i = 0; i < strlen(str); i++){
str[i] = tolower(str[i]);
if(str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u') {
//Increments the vowel counter
vCount++;
}
else if(str[i] >= 'a' && str[i] <= 'z'){
//Increments the consonant counter
cCount++;
}
}
printf("Number of vowels : %d\n", vCount);
printf("Number of consonant : %d", cCount);
return 0;
}

39. C program to make a simple calculator using switch-


case.
#include <stdio.h>
int main()
{
char op;
double first, second;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &op); printf("Enter two operands: ");
scanf("%lf %lf", &first, &second);
switch (op)
{
case '+':
printf("%.1lf + %.1lf = %.1lf", first, second, first + second);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", first, second, first - second);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", first, second, first * second);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf", first, second, first / second);
break;
// operator doesn't match any case constant
default:
printf("Error! operator is not correct");
}
return 0;
}

40. C code to find minimum or smallest element present in


an array.
#include <stdio.h>
int main()
{
//Initialize array
int arr[] = {25, 11, 7, 75, 56};
//Calculate length of array arr
int length = sizeof(arr)/sizeof(arr[0]);
//Initialize min with first element of array.
int min = arr[0];
//Loop through the array
for (int i = 0; i < length; i++) {
//Compare elements of array with min
if(arr[i] < min)
min = arr[i];
}
printf("Smallest element present in given array: %d\n", min);
return 0;
}

41. Read a character and check whether it is an alphabet or


not. If it is an alphabet then check
whether it is a vowel or a consonant using “if - else”.
/* C program to check whether a character is vowel or consonant */
#include <stdio.h>
int main()
{
char ch;
/* Input character from user */
printf("Enter any character: ");
scanf("%c", &ch);
/* Condition for vowel */ if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u'
|| ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U')
{ printf("'%c' is Vowel.", ch);
}
else if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
/* Condition for consonant */
printf("'%c' is Consonant.", ch);
}
else
{
/* * If it is neither vowel nor consonant * then it is not an alphabet. */

printf("'%c' is not an alphabet.", ch);


}
return 0;
}

42. Given the set of two elements, print all subsets .


#include <stdio.h>
#include <math.h>
int main()
{
int e1,e2,s;
printf("Reg no:200201031\n");
printf ("\nEnter the elements in the set\n");
scanf("%d%d" , &e1,&e2);
printf ("The set is: {%d,%d}",e1,e2);
s=pow(2,2);
printf (" The number of subsets are: %d",s);
printf ("\n{}");
printf ("\n{%d}",e1);
printf ("\n{%d}",e2);
printf ("\n{%d,%d}",e1,e2);
}

43. Given the coordinates (x,y) of the centre of a circle and


its radius r, determine whether
point(x,y) lies inside it or not using ternary (conditional)
operator.
#include <stdio.h>
#include <math.h>
int main()
{
int x1,x2,y1,y2,rad,max;
int dist;
printf("Reg no:200201031\n");
printf ("\nEnter the center of the circle\n");
scanf ("%d%d" , &x1,&y1);
printf ("Enter the radius of the circle\n");
scanf ("%d" , &rad);
printf ("Enter the point\n");
scanf ("%d%d" , &x2,&y2);
dist=sqrt (pow(x2-x1,2)+pow(y2-y1,2));
max = (dist>rad)? dist:rad;
if (max>rad)
printf ("point (%d,%d) lies outside the circle",x2,y2);
else
printf ("point (%d,%d) lies inside the circle",x2,y2);
}

44. Write a program to receive Cartesian coordinates (x, y)


of a point and convert them into polar coordinates (r,θ).
#include<stdio.h>
#include<math.h>
int main()
{
int x,y,rad,theta;
printf("Reg no:200201031\n");
printf("enter the x and y coordinates: \n");
scanf("%d,%d",&x,&y);
rad=sqrt(x*x+y*y);
theta=atan(y/x);
printf(" the polar coordinates are: (%d,%d)",rad,theta);
return 0;
}
45. C program to check Armstrong number for a 3-digit
number.
#include<stdio.h>
int main()
{
int n,r,sum=0,temp;
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
printf("armstrong number ");
else
printf("not armstrong number");
return 0;
}

You might also like