WAP to read a number from the user and test whether the number is negative. [ Show message “ THE NUMBER IS NEGATIVE” if it is a negative number , otherwise show nothing.
#include<stdio.h>
int main() {
int num;
scanf("%d", & num);
if (num < 0)
printf("Negative");
return 0;
}
WRITE A C ROGRAM TO FIND ALL ROOTS OF QUADRATIC EQUATION
#include <stdio.h>
#include <math.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;
}
WRITE A C ROGRAM CHECK WHETHER A YEAR IS LEAP OR NOT
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", & year);
// leap year if perfectly divisible by 400
if (year % 400 == 0) {
printf("%d is a leap year.", year);
}
// not a leap year if divisible by 100
// but not divisible by 400
else if (year % 100 == 0) {
printf("%d is not a leap year.", year);
}
// leap year if not divisible by 100
// but divisible by 4
else if (year % 4 == 0) {
printf("%d is a leap year.", year);
}
// all other years are not leap years
else {
printf("%d is not a leap year.", year);
}
return 0;
}
WRITE A C ROGRAM TO FIND HCF AND LCM OF TWO NUMBERS
#include <stdio.h>
int main() {
int a, b, x, y, t, gcd, lcm;
printf("Enter two integers\n");
scanf("%d%d", & x, & y);
a = x;
b = y;
while (b != 0) {
t = b;
b = a % b;
a = t;
}
gcd = a;
lcm = (x * y) / gcd;
printf("Greatest common divisor of %d and %d = %d\n", x, y, gcd);
printf("Least common multiple of %d and %d = %d\n", x, y, lcm);
return 0;
}
WRITE A PROGRAM TO CHECK WHETHER GIVEN NUMBER IS PRIME OR NOT.
#include <stdio.h>
int main() {
int n; //Declare the nummber
printf("Enter the number: ");
scanf("%d", & n); //Initialize the number
if (n == 1) {
printf("1 is neither prime nor composite.");
return 0;
}
int count = 0; //Declare a count variable
for (int i = 2; i < n; i++) //Check for factors
{
if (n % i == 0)
count++;
}
if (count == 0) //Check whether Prime or not
{
printf("%d is a prime number.", n);
} else {
printf("%d is not a prime number.", n);
}
return 0;
}
WRITE A C ROGRAM TO FIND GREATER OF THREE NUMBERS
int main() {
int A, B, C;
printf("Enter the numbers A, B and C: ");
scanf("%d %d %d", & A, & B, & C);
// finding max using compound expressions
if (A >= B && A >= C)
printf("%d is the largest number.", A);
else if (B >= A && B >= C)
printf("%d is the largest number.", B);
else
printf("%d is the largest number.", C);
return 0;
}
WRITE A C ROGRAM TO CHECK WHETHER A NUMBER IS ARMSTRONG OR NOT
#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;
}
WRITE A C ROGRAM TO CHECK WHETHER A NUMBER IS PALLINDROME OR NOT
#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 * 10) + r;
n = n / 10;
}
if (temp == sum)
printf("palindrome number ");
else
printf("not palindrome");
return 0;
}
WRITE A C ROGRAM TO FIND PRIME NUMBER BETWEEN TWO INTERVAL
#include<stdio.h>
#include<math.h>
void main() {
int num, m, n, d;
clrscr();
printf("Enter the Range Between m,n ");
scanf("%d%d", & m, & n);
for (num = m; num <= n; num++) {
for (d = 2; d < num; d++) {
if (num % d == 0)
break;
}
if (d == num)
printf("%d ", num);
}
getch();
}
WRITE A C ROGRAM TO FIND REVERSE OF A NUMBER
#include<stdio.h>
int main() {
int n, reverse = 0, rem;
printf("Enter a number: ");
scanf("%d", & n);
while (n != 0) {
rem = n % 10;
reverse = reverse * 10 + rem;
n /= 10;
}
printf("Reversed Number: %d", reverse);
return 0;
}
WRITE A C ROGRAM TO CHECK WHETHER A NUMBER IS TRIANGULAR OR NOT.
Triangular Numbers are those numbers which are obtained by continued summation of the natural numbers 1, 2, 3, 4, 5, etc.
Triangular Number Example: 15 is Triangular Number because it can be obtained by 1+2+3+4+5+6 i.e. 1+2+3+4+5+6=15
#include<stdio.h>
#include<conio.h>
int main() {
int number, i, sum = 0;
clrscr();
printf("Enter number: ");
scanf("%d", & number);
for (i = 0; i < number; i++) {
sum = sum + i;
if (sum == number) {
printf("%d is TRIANGULAR NUMBER.", number);
break;
}
}
if (number == i) {
printf("%d is NOT TRIANGULAR NUMBER.", number);
}
getch();
return (0);
}
WRITE A C ROGRAM TO CHECK WHETHER A CHARACTER IS VOWEL OR CONSONANT
#include <stdio.h>
int main() {
char c;
int lowercase_vowel, uppercase_vowel;
printf("Enter an alphabet: ");
scanf("%c", & c);
// evaluates to 1 if variable c is a lowercase vowel
lowercase_vowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
// evaluates to 1 if variable c is a uppercase vowel
uppercase_vowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
// evaluates to 1 (true) if c is a vowel
if (lowercase_vowel || uppercase_vowel)
printf("%c is a vowel.", c);
else
printf("%c is a consonant.", c);
return 0;
}
WRITE A C ROGRAM TO PRIME FACTOR OF A NUMBER
A prime factor is a natural number, other than 1, whose only factors are 1 and itself. The first few prime numbers are actually 2, 3, 5, 7, 11
#include <stdio.h>
int main() {
int Number, i = 1, j, Count;
printf("\n Please Enter number to Find Factors : ");
scanf("%d", & Number);
while (i <= Number) {
Count = 0;
if (Number % i == 0) {
j = 1;
while (j <= i) {
if (i % j == 0) {
Count++;
}
j++;
}
if (Count == 2) {
printf("\n %d ", i);
}
}
i++;
}
return 0;
}
WRITE A C ROGRAM TO CHECK WHETHER A NUMBER IS STRONG NUMBER OR NOT.
Strong numbers are those numbers whose sum of factorial of each digits is equal to the original number. For example 1 is strong number because 1!=1, 2 is strong number i.e. 2! = 2, 145 is strong number i.e. 1! + 4! + 5! = 1 + 24 + 120 = 145 etc.
#include<stdio.h>
int main() {
int number, original, rem, sum = 0, fact, i;
printf("Enter number: ");
scanf("%d", & number);
original = number;
/* Finding sum */
while (number != 0) {
rem = number % 10;
/* Finding Factorial */
fact = 1;
for (i = 1; i <= rem; i++) {
fact = fact * i;
}
sum = sum + fact;
number = number / 10;
}
/* Making decision */
if (sum == original) {
printf("%d is STRONG.", original);
} else {
printf("%d is NOT STRONG.", original);
}
return (0);
}
WRITE A C ROGRAM TO CONVERT DECIMAL NUMBER TO BINARY NUMBER.
#include<stdio.h>
int main()
{
int num, bin = 0, rem = 0, place = 1;
printf("Enter a decimal number\n");
scanf("%d", &num);
printf("\nBinary equivalent of %d is ", num);
while(num)
{
rem = num % 2;
num = num / 2;
bin = bin + (rem * place);
place = place * 10;
}
printf("%d\n", bin);
return 0;
}
WRITE A C PROGRAM TO CONVER BINARY NUMBER TO DECIMAL NUMBER
#include<stdio.h>
#include<math.h>
int main() {
int i, bin_num, decimal_num = 0, rem;
printf("enter a binary number\n");
scanf("%d", & bin_num);
for (i = 0; bin_num != 0; ++i) {
rem = bin_num % 10;
bin_num = bin_num / 10;
decimal_num = decimal_num + (rem) * (pow(2, i));
}
printf("\n Decimal Number of given binary number: %d", decimal_num);
return 0;
}
WRITE A C PROGRAM TO EVALUATE SINE SERIES
#include<stdio.h>
#include<conio.h>
void main() {
int i, n;
float x, sum, t;
clrscr();
printf(" Enter the value for x : ");
scanf("%f", & x);
printf(" Enter the value for n : ");
scanf("%d", & n);
x = x * 3.14159 / 180;
t = x;
sum = x;
/* Loop to calculate the value of Sine */
for (i = 1; i <= n; i++) {
t = (t * (-1) * x * x) / (2 * i * (2 * i + 1));
sum = sum + t;
}
printf(" The value of Sin(%f) = %.4f", x, sum);
getch();
}
WRITE A C PROGRAM TO CHECK WHETHER A NUMBER IS PERFECT NUMBER OR NOT .
/*
* C Program to Check whether a given Number is Perfect Number using for loop
*/
#include <stdio.h>
int main()
{
int number, rem, sum = 0, i;
printf("Enter a Number: ");
scanf("%d", &number);
for (i = 1; i <= (number - 1); i++)
{
rem = number % i;
if (rem == 0)
{
sum = sum + i;
}
}
if (sum == number)
printf("%d is perfect number", number);
else
printf("%d is not a perfect number", number);
return 0;
}
#include<stdio.h>
#include<conio.h>
int main() {
long int number, sum, step = 1, rem;
clrscr();
printf("Enter number: ");
scanf("%ld", & number);
do {
sum = 0;
while (number != 0) {
rem = number % 10;
sum = sum + rem;
number = number / 10;
}
printf("Step-%ld Sum = %ld\n", step, sum);
number = sum;
step = step + 1;
} while (number > 9);
getch();
return (0);
}
C PROGRAM TO CHECK WHETHER TWO NUMBER IS CO-PRIME OR NOT.
Two numbers are said to be co-prime numbers if they do not have a common factor other than 1 or two numbers whose Highest Common Factor (HCF) or Greatest Common Divisor (GCD) is 1 are known as co-prime numbers.
#include<stdio.h>
#include<conio.h>
int main() {
int num1, num2, hcf, i;
clrscr();
printf("Enter two numbers:\n");
scanf("%d%d", & num1, & num2);
// Finding HCF
for (i = 1; i <= num1; i++) {
if (num1 % i == 0 && num2 % i == 0) {
hcf = i;
}
}
// Making Decision
if (hcf == 1) {
printf("%d and %d are CO-PRIME NUMBERS.", num1, num2);
} else {
printf("%d and %d are NOT CO-PRIME NUMBERS.", num1, num2);
}
getch();
return (0);
}
C PROGRAM TO GENERATE MULTIPLICATION TABLE OF 1 TO 10.
#include<stdio.h>
#include<conio.h>
int main() {
int i, j, product;
clrscr();
/* Generating Multiplication Table */
for (i = 1; i <= 10; i++) {
for (j = 1; j <= 10; j++) {
product = i * j;
printf("%d x %d = %d\t", i, j, product);
}
printf("\n");
}
getch();
return (0);
}
C PROGRAM TO GENERATE ASCII TABLE
#include<stdio.h>
#include<conio.h>
int main() {
int i;
clrscr();
for (i = 0; i <= 255; i++) {
printf("%d = %c\t", i, i);
}
getch();
return (0);
}