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

Assignment 02

The document contains programs written in C language to solve various problems: 1) Eight programs are written to check if a number is positive or negative, even or odd, divisible by 5, divisible by both 3 and 5, swap values of variables, find the greatest among 3 numbers, calculate grade based on marks, and calculate roots of a quadratic equation. 2) Two more programs are written to calculate total, percentage and division of marks in 3 subjects, and check if angles can form a triangle. 3) The remaining programs calculate the sum of first N natural numbers, display multiplication table of a given number, calculate factorial of a number, check if a number is an Armstrong number, and determine if

Uploaded by

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

Assignment 02

The document contains programs written in C language to solve various problems: 1) Eight programs are written to check if a number is positive or negative, even or odd, divisible by 5, divisible by both 3 and 5, swap values of variables, find the greatest among 3 numbers, calculate grade based on marks, and calculate roots of a quadratic equation. 2) Two more programs are written to calculate total, percentage and division of marks in 3 subjects, and check if angles can form a triangle. 3) The remaining programs calculate the sum of first N natural numbers, display multiplication table of a given number, calculate factorial of a number, check if a number is an Armstrong number, and determine if

Uploaded by

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

ASSIGNMENT-02

NAME : L.Sampath
REG-NO : 23BCB7136
1) program to check the entered number is positive or negative

#include<stdio.h>

int main()
{
int n;

printf("enter a number");
scanf("%d",&n);
if(n>=0)
{
printf("the number is positive");
}
else{
printf("the number is negative");
}
return 0;
}
Output:-
2)To check the number is odd or even
#include<stdio.h>
int main()
{
int n;
printf("enter a number");
scanf("%d",&n);
if(n%2==0)
{
printf("the number is even");
}
else
{
printf("the number is odd");
}
return 0;
}
Output:-

3)program to check the number is divisible by 5


#include<stdio.h>
int main()
{
int n;
printf("enter a number");
scanf("%d",&n);
if(n%5==0)
{
printf("the number is divisible by 5");
}
else
{
printf("the number is not divisible by 5");
}
return 0;
}

4)Program to check the number is divisible by 3 and 5


#include<stdio.h>
int main()
{
int n;
printf("enter a number");
scanf("%d",&n);
if(n%5==0&&n%3==0)
{
printf("the number is divisible by 5 and 3");
}
else
{
printf("the number is not divisible by 5 and 3");
}
return 0;
}

5)Program to swap the values of variable


#include<stdio.h>
void main()
{
int a,b;
printf("enter a number");
scanf("%d %d",&a,&b);
int t = a;
a = b;
b = t;
printf("the value of a \n%d",a);
printf("the value of b \n%d",b);

}
6)program greatest among 3 numbers
#include<stdio.h>
void main()
{
int a,b,c;
printf("enter 3 number");
scanf("%d %d %d",&a,&b,&c);
if(a>c&&a>b)
{
printf("a is the greatest");
}
else if(b>a&&b>c)
{
printf("b is the greatest");
}
else
{
printf("c is the greatest");
} }

7) program for grade of 5


#include <stdio.h>

int main() {
int marks[5];
float total_marks = 0, percentage;
char grade;

for (int i = 0; i < 5; i++) {


printf("Enter marks for subject %d : ", i + 1);
scanf("%d", &marks[i]);
total_marks += marks[i];
}

percentage = (total_marks / 500.0) * 100;

if (percentage >= 90) {


grade = 'A';
} else if (percentage >= 80) {
grade = 'B';
} else if (percentage >= 70) {
grade = 'C';
} else if (percentage >= 60) {
grade = 'D';
} else if (percentage >= 40) {
grade = 'E';
} else {
grade = 'F';
}

printf("Total Marks : %.2f\n", total_marks);


printf("Percentage : %.2f%%\n", percentage);
printf("Grade : %c\n", grade);

return 0;
}
a) program to calculate the root of a quadratic equation
#include <stdio.h>
#include <math.h>

int main() {
double a, b, c, determinant, real_part, imaginary_part;

printf("Enter coefficients a, b and c: ");


scanf("%lf %lf %lf", &a, &b, &c);

determinant = b * b - 4 * a * c;

if (determinant > 0) {
real_part = (-b + sqrt(determinant)) / (2 * a);
imaginary_part = (-b - sqrt(determinant)) / (2 * a);
printf("Roots are %.2lf and %.2lf\n", real_part, imaginary_part);
} else if (determinant == 0) {
real_part = -b / (2 * a);
printf("Root is %.2lf\n", real_part);
} else {
real_part = -b / (2 * a);
imaginary_part = sqrt(-determinant) / (2 * a);
printf("Roots are %.2lf + %.2lfi and %.2lf - %.2lfi\n", real_part,
imaginary_part, real_part, imaginary_part);
}

return 0;

}
b) Write a C program to read the roll no, name and marks of three
subjects and calculate the total, percentage and division.
#include <stdio.h>

int main() {
int roll_no, marks[3];
char name[100];
float total, percentage;
char division;

printf("Enter roll no: ");


scanf("%d", &roll_no);

printf("Enter name: ");


scanf("%s", name);

printf("Enter marks for 3 subjects: ");


for (int i = 0; i < 3; i++) {
scanf("%d", &marks[i]);
}

total = marks[0] + marks[1] + marks[2];


percentage = (total / 300.0) * 100;

if (percentage >= 90) {


division = 'A';
} else if (percentage >= 80) {
division = 'B';
} else if (percentage >= 70) {
division = 'C';
} else if (percentage >= 60) {
division = 'D';
} else {
division = 'E';
}

printf("Roll no: %d\n", roll_no);


printf("Name: %s\n", name);
printf("Total marks: %.2f\n", total);
printf("Percentage: %.2f%%\n", percentage);
printf("Division: %c\n", division);

return 0;
}

c) program to check whether a triangle can be formed with the given


values for the angles
d)p#include <stdio.h>

int main() {
int angle1, angle2, angle3;

printf("Enter angle 1: ");


scanf("%d", &angle1);

printf("Enter angle 2: ");


scanf("%d", &angle2);

printf("Enter angle 3: ");


scanf("%d", &angle3);

if (angle1 + angle2 + angle3 == 180) {


printf("The triangle can be formed.\n");
} else {
printf("The triangle cannot be formed.\n");
}

return 0;
}
d)program to compute the sum of the first “N” natural numbers
#include <stdio.h>

int main() {
int N, sum = 0;

printf("Enter the number of natural numbers: ");


scanf("%d", &N);

for (int i = 1; i <= N; i++) {


sum += i;
}

printf("The sum of the first %d natural numbers is: %d\n", N, sum);

return 0;
}
e)program in C to display the multiplication table for a given integer
#include <stdio.h>

int main() {
int number, i;

printf("Enter the number: ");


scanf("%d", &number);

printf("\nMultiplication Table for %d:\n", number);

for(i = 1; i <= 10; i++) {


printf("%d * %d = %d\n", number, i, number * i);
}

return 0;
}
f)program to calculate the factorial of a given number.
#include <stdio.h>
#include <stdio.h>

int main() {
int number, i, fact = 1;

printf("Enter the number: ");


scanf("%d", &number);

for(i = 1; i <= number; i++) {


fact = fact * i;
}

printf("\nFactorial of %d is: ", fact);


}
g)program to check whether a given number is an Armstrong number
or not.
#include <stdio.h>
#include <math.h>

int main() {
int number, sum = 0, digits;
int temp = number;

printf("Enter the number: ");


scanf("%d", &number);

while(temp != 0) {
temp /= 10;
digits++;
}

temp = number;
while(temp != 0) {
int remainder = temp % 10;
sum += pow(remainder, digits);
temp /= 10;
}

if(sum == number) {
printf("%d is an Armstrong number.\n", number);
} else {
printf("%d is not an Armstrong number.\n", number);
}

return 0;
}

h)program to determine whether a given number is prime or not.


#include <stdio.h>
int check_prime(int n);

int main() {
int n;

printf("Enter a number: ");


scanf("%d", &n);

if(check_prime(n) == 1)
printf("%d is a prime number\n", n);
else
printf("%d is not a prime number\n", n);

return 0;
}

int check_prime(int n) {
int i;

if(n <= 1)
return 0;

for(i = 2; i <= n/2; ++i) {


if(n % i == 0)
return 0;
}

return 1;
}

You might also like