C Program 2
C Program 2
Programming
NAME: ADITYA VERMA
ROLL NO.: 24BEC008
BRANCH: ECE
SUBMITTED TO:
DR. AMIT KANT PANDIT
INDEX
1. C "Hello, World!" Program
2. Program to Print an Integer
3. Program to Add Two Integers
4. Program to Multiply Two Numbers
5. Program to Print ASCII Value
OUTPUT
Hello, World!
2. Program to Print an Integer
#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
// reads and stores input
scanf("%d", &number);
// displays output
printf("You entered: %d", number);
return 0;
}
OUTPUT
Enter an integer: 25
You entered: 25
3. Program to Add Two Integers
#include <stdio.h>
int main()
{
int number1, number2, sum;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
// calculate the sum
sum = number1 + number2;
printf("%d + %d = %d", number1, number2, sum);
return 0;
}
Output
Enter two integers: 12
11
12 + 11 = 23
Output
Enter a character: G
ASCII value of G = 71
6. Program to Compute Quotient and
Remainder
#include <stdio.h>
int main()
{
int dividend, divisor, quotient, remainder;
printf("Enter dividend: ");
scanf("%d", ÷nd);
printf("Enter divisor: ");
scanf("%d", &divisor);
// Computes quotient
quotient = dividend / divisor;
// Computes remainder
remainder = dividend % divisor;
printf("Quotient = %d\n", quotient);
printf("Remainder = %d", remainder);
return 0;
}
Output
Enter dividend: 25
Enter divisor: 4
Quotient = 6
Remainder = 1
7. Program to Find the Size of
Variables
#include<stdio.h>
int main()
{
int intType;
float floatType;
double doubleType;
char charType;
// sizeof evaluates the size of a variable
printf("Size of int: %zu bytes\n", sizeof(intType));
printf("Size of float: %zu bytes\n", sizeof(floatType));
printf("Size of double: %zu bytes\n",
sizeof(doubleType));
printf("Size of char: %zu byte\n", sizeof(charType));
return 0;
}
Output
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte
Output
Size of int= 4 bytes
Size of long int= 8 bytes
Size of long long int= 8 bytes
Size of double = 8 bytes
Size of long double = 16 bytes
9. Swap Numbers Using Temporary
Variable
#include<stdio.h>
intmain()
{
double first, second, temp;
printf("Enter first number: ");
scanf("%lf", &first);
printf("Enter second number: ");
scanf("%lf", &second);
// value of first is assigned to temp
temp = first;
// value of second is assigned to first
first = second;
// value of temp (initial value of first) is assigned to
second
second = temp;
// %.2lf displays number up to 2 decimal points
printf("\nAfter swapping, first number = %.2lf\n", first);
printf("After swapping, second number = %.2lf",
second);
return 0;
}
Output
Enter first number: 1.20
Enter second number: 2.45
return 0;
}
Output
Enter an integer: -7
-7 is odd.
Output
Enter an alphabet: G
G is a consonant.
12. Program to Find the Largest
Number Among Three Numbers
I. Using if Statement
#include <stdio.h>
int main()
{
double n1, n2, n3;
printf("Enter three different numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
// if n1 is greater than both n2 and n3, n1 is the largest
if (n1 >= n2 && n1 >= n3)
printf("%.2f is the largest number.", n1);
return 0;
}
OUTPUT:
Enter three different numbers: 25 44 68
68.00 is the largest number.
II. Using if...else Ladder
#include <stdio.h>
int main ()
{
double n1, n2, n3;
printf("Enter three numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
// if n1 is greater than both n2 and n3, n1 is the largest
if (n1 >= n2 && n1 >= n3)
printf("%.2lf is the largest number.", n1);
// if n2 is greater than both n1 and n3, n2 is the largest
else if (n2 >= n1 && n2 >= n3)
printf("%.2lf is the largest number.", n2);
return 0;
}
OUTPUT:
Enter three numbers: 43 45 223
223.00 is the largest number.
III. Using Nested if...else
#include <stdio.h>
int main ()
{
double n1, n2, n3;
// outer if statement
if (n1 >= n2) {
// inner if...else
if (n1 >= n3)
printf("%.2lf is the largest number.", n1);
else
printf("%.2lf is the largest number.", n3);
}
// inner if...else
if (n2 >= n3)
printf("%.2lf is the largest number.", n2);
else
printf("%.2lf is the largest number.", n3);
}
OUTPUT:
Enter three numbers: 565 544 899
899.00 is the largest number
13. Program to Find Roots of a Quadratic Equation
#include <stdio.h>
#include <math.h>
int main() {
double a, b, c, discriminant , root1, root2, realPart,
imagPart;
printf("enter coefficient 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 = %0.2lf and Root2 = %0.2lf \n",
root1 , root2);
}
// condition for real and equal roots
else if ( discriminant ==0 ){
root1 = root2 = -b/(2*a);
printf("Root1 = Root2 = %0.2lf \n", root1);
}
// if roots are not real
else{
realPart= -b/(2*a);
imagPart= sqrt(-discriminant)/(2*a);
printf("Root1 = %0.2lf + %0.2lfi and Root2=
%0.2lf - %0.2lfi",realPart , imagPart , realPart ,
imagPart);
return 0;
}
Output
Enter coefficients a, b and c: 2.3
4
5.6
root1 = -0.87+1.30i and root2 = -0.87-1.30i
14. Program to Check Leap Year
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
return 0;
}
Output 1
Enter a year: 1900
1900 is not a leap year.
15. Check Positive or Negative Using Nested
if...else
#include <stdio.h>
int main () {
double num;
printf("Enter a number: ");
scanf("%lf", &num);
if (num <= 0.0) {
if (num == 0.0)
printf("You entered 0.");
else
printf("You entered a negative number.");
}
else
printf("You entered a positive number.");
return 0;
}
Output 1
Enter a number: 12.3
You entered a positive number.
Check Positive or Negative Using if...else
Ladder
#include <stdio.h>
int main() {
double num;
printf("Enter a number: ");
scanf("%lf", &num);
if (num < 0.0)
printf("You entered a negative number.");
else if (num > 0.0)
printf("You entered a positive number.");
else
printf("You entered 0.");
return 0;
}
Output 2
Enter a number: 0
You entered 0.
Output
Enter a character: *
* is not an alphabet
Output
Enter a positive integer: 100
Sum = 5050
18. Factorial of a Number
#include <stdio.h>
int main() {
int n, i;
unsigned long long fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);
// shows error if the user enters a negative integer
if (n < 0)
printf("Error! Factorial of a negative number
doesn't exist.");
else {
for (i = 1; i <= n; ++i) {
fact *= i;
}
printf("Factorial of %d = %llu", n, fact);
}
return 0;
}
Output
Enter an integer: 10
Factorial of 10 = 3628800
19. Multiplication Table Up to 10
#include <stdio.h>
int main() {
int n;
printf("Enter an integer: ");
scanf("%d", &n);
int i, n;
return 0;
}
Output
Enter the number of terms: 10
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
21. GCD Using for loop and if Statement
#include <stdio.h>
int main()
{
int n1, n2, i, gcd;
return 0;
}
#2: GCD Using while loop and if...else
Statement
#include <stdio.h>
int main()
{
int n1, n2;
printf("Enter two positive integers: ");
scanf("%d %d",&n1,&n2);
while(n1!=n2)
{
if(n1 > n2)
n1 -= n2;
else
n2 -= n1;
}
printf("GCD = %d",n1);
return 0;
}
Output
Enter two positive integers: 81
153
GCD = 9
Example #3: GCD for both positive and
negative numbers
#include <stdio.h>
int main()
{
int n1, n2;
while(n1!=n2)
{
if(n1 > n2)
n1 -= n2;
else
n2 -= n1;
}
printf("GCD = %d",n1);
return 0;
}
Output
Enter two integers: 81
-153
GCD = 9
22. LCM using while and if
#include <stdio.h>
int main() {
int n1, n2, max, lcm;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
// maximum number between n1 and n2 is
stored in max
max = (n1 > n2) ? n1 : n2;
lcm = max;
while ((lcm % n1 != 0) || (lcm % n2 != 0)) {
lcm += max;
}
return 0;
}
Output
Enter two positive integers: 72
120
The LCM of 72 and 120 is 360.