0% found this document useful (0 votes)
5 views4 pages

C Programming Assignment No 2

The document contains ten C programs that demonstrate basic programming concepts. These programs include checking if a number is even or odd, finding the largest of two or three numbers, calculating factorials, reversing a number, computing simple interest, generating Fibonacci series, checking for vowels or consonants, swapping numbers, and finding the largest among three numbers. Each program includes necessary input/output operations and logic to perform the specified tasks.

Uploaded by

nikhiltormal54
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)
5 views4 pages

C Programming Assignment No 2

The document contains ten C programs that demonstrate basic programming concepts. These programs include checking if a number is even or odd, finding the largest of two or three numbers, calculating factorials, reversing a number, computing simple interest, generating Fibonacci series, checking for vowels or consonants, swapping numbers, and finding the largest among three numbers. Each program includes necessary input/output operations and logic to perform the specified tasks.

Uploaded by

nikhiltormal54
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/ 4

1)Write a c program to check given number is even or odd

#include <stdio.h>
void main()
{
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if (num % 2 == 0)
{
printf("%d is an even number.\n", num);
}
else
{
printf("%d is an odd number.\n", num);
}
getch ();
}
2) write a c program to find the largest of two numbers
#include <stdio.h>
void main()
{
int num1, num2;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf(“%d”, &num2);
if (num1 > num2)
{
printf("%d is the largest number.\n", num1);
}
else if (num2 > num1)
{
printf("%d is the largest number.\n", num2);
}
else {
printf("Both numbers are equal.\n");
}
getch ();
}
3) Write a c program to find the largest three numbers
#include <stdio.h>
void main()
{
int num1, num2, num3;
printf("Enter three integers: \n");
scanf("%d %d %d", &num1, &num2, &num3);
if (num1 >= num2 && num1 >= num3)
{
printf("The largest number is: %d\n", num1);
}
else if (num2 >= num1 && num2 >= num3)
{
printf("The largest number is: %d\n", num2);
}
else
{
printf("The largest number is: %d\n", num3);
}
getch();
}
4) Write a c program to find the factorial of given number
#include <stdio.h>
void main()
{
int num;
long long factorial = 1;
printf("Enter a non-negative integer: ");
scanf("%d", &num);
if (num < 0)
{
printf("Factorial is not defined for negative numbers.\n");
}
else if (num == 0)
{
printf("Factorial of 0 is: 1\n");
}
else
{
for (int i = 1; i <= num; i++)
{
factorial *= i;
}
printf("Factorial of %d is: %lld\n",num,factorial);
}
getch();
}
5) Write a c program to print reverse of a number
#include <stdio.h>
void main()
{
int num, reverse= 0, remainder;
printf("Enter an integer: ");
scanf("%d", &num);
while (num != 0)
{
remainder = num % 10;
reverse= reverse * 10 + remainder;
num /= 10;
}
printf("ReversedNumber=%d\n",reverse);
getch();
}
6) Write a c program to find simple interest.
#include <stdio.h>
void main()
{
float P = 1, R = 1, T = 1;
float SI = (P * T * R) / 100;
printf("Simple Interest = %f\n", SI);
getch();
}
7) Write a C program to the Fibonacci series.
#include <stdio.h>
void main()
{
int i, n;
int t1 = 0, t2 = 1;
int nextTerm = t1 + t2;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: %d, %d, ", t1, t2);
for (i = 3; i <= n; ++i)
{
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
getch();
}
8) Write a c program check vowel or consonants.
#include <stdio.h>
void main()
{
char c;
int lowercase_vowel, uppercase_vowel;
printf("Enter an alphabet: ");
scanf("%c", &c);
lowercase_vowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
uppercase_vowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
if (lowercase_vowel || uppercase_vowel)
printf("%c is a vowel.", c);
else
printf("%c is a consonant.", c);
getch();
}
9) Write a c program swap number using a temporary variable.
#include<stdio.h>
void main()
{
double first, second, temp;
printf("Enter first number: ");
scanf("%lf", &first);
printf("Enter second number: ");
scanf("%lf", &second);
temp = first;
first = second;
second = temp;
printf("\nAfter swapping, first number = %.2lf\n", first);
printf("After swapping, second number = %.2lf", second);
getch();
}
10) Write a c program to find the largest among three numbers.
#include <stdio.h>
void main()
{
double n1, n2, n3;
printf("Enter three different numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
if (n1 >= n2 && n1 >= n3)
printf("%.2f is the largest number.", n1);
if (n2 >= n1 && n2 >= n3)
printf("%.2f is the largest number.", n2);
if (n3 >= n1 && n3 >= n2)
printf("%.2f is the largest number.", n3);
getch();
}

You might also like