0% found this document useful (0 votes)
7 views6 pages

C Programs for Basic Calculations

C code practice

Uploaded by

mdragonborn007
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)
7 views6 pages

C Programs for Basic Calculations

C code practice

Uploaded by

mdragonborn007
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

1.

​ # Write a program that will take a number input determined whether


negative or positive?

#include <stdio.h>

int main() {
int num;

printf("Enter any value (not 0): ");


scanf("%d", &num);

if (num == 0) {
printf("Error: Zero is not allowed, try again!\n");
}
else if (num > 0) {
printf("Positive Number\n");
}
else {
printf("Negative Number\n");
}

return 0;
}

2. # Write a program that takes the marks of a student as input and


checks whether the student has passed or failed the exam. A student will
pass if his/her mark is not less than 40?

#include <stdio.h>

int main() {
int marks;

printf("Enter your marks: ");


scanf("%d", &marks);

if (marks >= 40) {


printf("You have passed the exam.\n");
}
else {
printf("You have failed the exam.\n");
}

return 0;
}
3. # Write a program that will takes two numbers input and max number
output.
#include <stdio.h>

int main() {
int num1, num2;

printf("Enter first number: ");


scanf("%d", &num1);

printf("Enter second number: ");


scanf("%d", &num2);

if (num1 > num2) {


printf("Maximum number is: %d\n", num1);
}
else if (num2 > num1) {
printf("Maximum number is: %d\n", num2);
}
else {
printf("Both numbers are equal: %d\n", num1);
}

return 0;
}

4. # Write a program to determine the color at a given position in a


sequence of colors where:

●​ 1 = Green
●​ 2 = Red
●​ 3 = Brown
●​ 4 = Yellow

The sequence repeats in the same order. What will be the color at
position 50?

#include <stdio.h>

int main() {
int n, colorCode;

printf("Enter a number: ");


scanf("%d", &n);

colorCode = n % 4;

if (colorCode == 1) {
printf("Color is Green\n");
}
else if (colorCode == 2) {
printf("Color is Red\n");
}
else if (colorCode == 3) {
printf("Color is Brown\n");
}
else {

printf("Color is Yellow\n");
}

return 0;
}

5. # Write a program that will take a number as input and determine how
many years, months and days ?

#include <stdio.h>

int main() {
int days, years, months;

printf("Enter total number of days: ");


scanf("%d", &days);

years = days / 365;


days = days % 365;

months = days / 30;


days = days % 30;

printf("Years: %d, Months: %d, Days: %d\n", years, months, days);

return 0;
}
7. # Write a program to print all even numbers from 1 to 100;

#include <stdio.h>

int main() {
int i, num = 100;

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


if(i%2==0){
printf("%d ",i);
}
}
}

8. # Write a program to print all Odd numbers from 1 to 100;

#include <stdio.h>

int main() {
int i, num = 100;

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


if(i%2==1){
printf("%d ",i);
}
}
}

9. # Write a program that takes a number and print the summation of that
number . For example, if the input is 5 then the program will print
120.(Hint : 1+2+3+4+5 = 15).

#include <stdio.h>

int main() {
int i, n, sum=0;
printf("Enter a number: ");
scanf("%d",&n);

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


sum=sum+i;
}
printf("Summation = %d\n", sum);
}
10. # Write a program that takes a number and print the factorial of that
number . For example, if the input is 5 then the program will print
120.(Hint : 1x2x3x4x5 = 120).

#include <stdio.h>

int main() {
int i, n, factorial=1;
printf("Enter a number: ");
scanf("%d",&n);

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


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

11. # Write a program that takes two numbers m and n as input and
calculates the summation of all even numbers between m and n
(inclusive).

#include <stdio.h>

int main() {
int m, n, sum = 0;

printf("Enter the starting number (m): ");


scanf("%d", &m);

printf("Enter the ending number (n): ");


scanf("%d", &n);

for (int i = m; i <= n; i++) {


if (i % 2 == 0) {
sum += i;
}
}

printf("Summation = %d\n", sum);

return 0;
}
12. # Write a program that takes two numbers m and n as input and
performs the following tasks:

1.​Calculate the summation of all even numbers between m and n


(inclusive).
2.​Calculate the summation of all odd numbers between m and n
(inclusive).
3.​Subtract the sum of odd numbers from the sum of even numbers
and print the result.
#include <stdio.h>

int main() {
int m, n;
int evenSum = 0, oddSum = 0;

printf("Enter starting number (m): ");


scanf("%d", &m);

printf("Enter ending number (n): ");


scanf("%d", &n);

for (int i = m; i <= n; i++) {


if (i % 2 == 0) {
evenSum += i; // even number add
} else {
oddSum += i; // odd number add
}
}

printf("Sum of even numbers from %d to %d: %d\n", m, n, evenSum);


printf("Sum of odd numbers from %d to %d: %d\n", m, n, oddSum);
printf("Subtraction (Even - Odd): %d\n", evenSum - oddSum);

return 0;
}

You might also like