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

c file-1

The document contains a series of C programming exercises, each demonstrating different programming concepts such as printing messages, user input, arithmetic operations, control structures, and functions. Each exercise includes the code, a brief description of its purpose, and sample output. Topics covered range from basic input/output to more complex operations like calculating factorials and checking leap years.

Uploaded by

ra5031632
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

c file-1

The document contains a series of C programming exercises, each demonstrating different programming concepts such as printing messages, user input, arithmetic operations, control structures, and functions. Each exercise includes the code, a brief description of its purpose, and sample output. Topics covered range from basic input/output to more complex operations like calculating factorials and checking leap years.

Uploaded by

ra5031632
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 36

1.

Write a program to print hello world

#include <stdio.h>
int main()
{
printf("Hello World");
return 0;
}
Output
Hello World

2. Write a program to print your own name

#include <stdio.h>
int main()
{
printf("Ramavtar");
return 0;
}
Output
Ramavtar

1
3. Write a program to print an integer entered by the user

#include <stdio.h>
int main() {
int number;

printf("Enter an integer: ");

scanf("%d", &number);
printf("You entered: %d", number);

return 0;
}
Output
Enter an integer: 87
You entered: 87

4. Write a program to add two numbers

#include <stdio.h>
int main() {

int num1, num2, sum;

printf("Enter two integers: ");


scanf("%d %d", &num1, &num2);
sum = num1 + num2;

printf("%d + %d = %d", num1, num2, sum);


return 0;
}
Output
Enter two integers: 7898
5641
7898 + 5641 = 13539

2
5. Write a program to check whether a number is prime or not.

#include <stdio.h>

int main() {
int n,c;
printf("enter the number: ");
scanf("%d",&n);
if (n <= 1)
printf("%d is NOT prime\n", n);
else {
for (int i = 1; i <= n; i++) {
if (n % i == 0)
c++;
}
if (c > 2)
printf("%d is NOT prime\n", n);

// else it is prime
else
printf("%d is prime", n);
}
return 0;
}
Output 1
enter the number: 7
7 is prime
Output 2
enter the number: 9
9 is not prime

6. Write a program to multiply two floating point numbers

3
#include <stdio.h>

int main()
{
float n,c;
printf("enter two numbers :");
scanf("%f %f", &n, &c);
printf("%f", n*c);

return 0;
}
Output
Enter two numbers : 589.59
2.456
1448.033203

7. Write a program to print the ASCII value of a character.

#include <stdio.h>
int main()
{
char c;
printf("Enter a character: ");
scanf("%c", &c);
printf("ASCII value of %c = %d", c, c);
return 0;
}
Output 1
Enter a character: A
ASCII value of A = 65
Output 2
Enter a character: a
ASCII value of a = 97
8. Write a C program to Swap Two Numbers.

4
#include<stdio.h>
int main(){
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 1
Enter first number: 1.20
Enter second number: 2.45
Output 2
After swapping, first number = 2.45
After swapping, second number = 1.20

9. Write a C program to Calculate Fahrenheit to Celsius.

#include<stdio.h>
5
int main()
{
float fahrenheit, celsius;
//get the limit of fibonacci series
printf("Enter Fahrenheit: ");
scanf("%f",&fahrenheit);
celsius = (fahrenheit - 32)*5/9;
printf("Celsius: %f ", celsius);
return 0;
}
Output
Enter Fahrenheit: 9
Celsius: -12.777778

10. Write a C program to Find the Size of int, float, double, and char.

#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
11. Write a C program to Add Two Complex Numbers .

#include <stdio.h>

6
typedef struct complex {
float real;
float imag;
} complex;
complex add(complex n1, complex n2);
int main() {
complex n1, n2, result;

printf("For 1st complex number \n");


printf("Enter the real and imaginary parts: ");
scanf("%f %f", &n1.real, &n1.imag);
printf("\nFor 2nd complex number \n");
printf("Enter the real and imaginary parts: ");
scanf("%f %f", &n2.real, &n2.imag);

result = add(n1, n2);

printf("Sum = %.1f + %.1fi", result.real, result.imag);


return 0;
}

complex add(complex n1, complex n2) {


complex temp;
temp.real = n1.real + n2.real;
temp.imag = n1.imag + n2.imag;
return (temp);
}
Output
For 1st complex number
Enter the real and imaginary parts: 2.1
-2.3
For 2nd complex number
Enter the real and imaginary parts: 5.6 23.2
Sum = 7.7 + 20.9i
12. Write a C program to Print Prime Numbers From 1 to N.
#include <stdio.h>

7
int main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);

// 0 and 1 are not prime numbers


// change flag to 1 for non-prime number
if (n == 0 || n == 1)
flag = 1;

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

// if n is divisible by i, then n is not prime


// change flag to 1 for non-prime number
if (n % i == 0) {
flag = 1;
break;
}
}

// flag is 0 for prime numbers


if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);

return 0;
}
Output 1
Enter a positive integer: 29
29 is a prime number.
Output 2
Enter a positive integer: 8
8 is not a prime number.
13. Write a C program to Find Simple Interest
# include <conio.h>
# include <stdio.h>

8
# include <stdlib.h>

int main(){

//Simple interset program


int principal, rate, time, interest;

printf("Enter the principal: ");


scanf("%d", &principal);

printf("Enter the rate: ");


scanf("%d", &rate);

printf("Enter the time: ");


scanf("%d", &time);

interest = (principal * rate * time) / 100;


printf("The Simple interest is %d", interest);

return 0;

}
Output:
Enter the principal: 1000
Enter the rate: 2
Enter the time: 23
The Simple interest is 460

14. Write a C program to Find Compound Interest.


#include <stdio.h>
#include <math.h>

9
int main()
{
float principle, rate, time, CI;

/* Input principle, time and rate */


printf("Enter principle (amount): ");
scanf("%f", &principle);

printf("Enter time: ");


scanf("%f", &time);

printf("Enter rate: ");


scanf("%f", &rate);

/* Calculate compound interest */


CI = principle* (pow((1 + rate / 100), time));

/* Print the resultant CI */


printf("Compound Interest = %f", CI);

return 0;
}
Output
Enter principle (amount): 1200
Enter time: 2
Enter rate: 5.4
Compound Interest = 1333.099243

15. Write a C program for Area And Perimeter Of Rectangle.


#include <stdio.h>

10
void main() {
/* Variable Declaration. */
float lnth, wdth, perimeter, area;

/* Taking input of the length of the rectangle from the user */


printf("Enter the length of the Rectangle:\n");
scanf("%f", & lnth);

/* Taking input of the width of the rectangle from the user */


printf("Enter the width of the Rectangle:\n");
scanf("%f", & wdth);

/* Calculate perimeter of the rectangle */


perimeter = 2 * (lnth + wdth);
printf("Perimeter of the Rectangle: %0.4f\n", perimeter);

/* Calculate area of the rectangle */


area = lnth * wdth;
printf("Area of the Rectangle: %0.4f\n", area);
}
Output
Enter the length of the Rectangle:
50
Enter the width of the Rectangle:
80
Perimeter of the Rectangle: 260.0000
Area of the Rectangle: 4000.0000

16. Write a C program to Check Whether a Number is Positive, Negative, or


Zero.
#include <stdio.h>

int main()

11
{
int num;

/* Input number from user */


printf("Enter any number: ");
scanf("%d", &num);

if(num > 0)
{
printf("Number is POSITIVE");
}
if(num < 0)
{
printf("Number is NEGATIVE");
}
if(num == 0)
{
printf("Number is ZERO");
}

return 0;
}
Output 1
Enter any number: 9
Number is POSITIVE
Output 2
Enter any number: -5
Number is NEGATIVE
Output 3
Enter any number: 0
Number is ZERO
17. Write a C program to Check Whether Number is Even or Odd.
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);

12
// true if num is perfectly divisible by 2
if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);

return 0;
}
Output 1
Enter an integer: 7
7 is odd.
Output 2
Enter an integer: 8
8 is even.

18. Write a C program 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);
13
// 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;
}
Output 1
Enter an alphabet: G
G is a consonant.
Output 2
Enter an alphabet: e
e is a vowel.

19. Write a C program to Find Largest Number Among Three Numbers.


#include <stdio.h>

int main() {

double n1, n2, n3;

14
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);

// if n2 is greater than both n1 and n3, n2 is the largest


if (n2 >= n1 && n2 >= n3)
printf("%.2f is the largest number.", n2);

// if n3 is greater than both n1 and n2, n3 is the largest


if (n3 >= n1 && n3 >= n2)
printf("%.2f is the largest number.", n3);

return 0;
}
Output
Enter three different numbers: 25
95
65

95.00 is the largest number.

20. Write a C program to Calculate Sum of Natural Numbers .


#include <stdio.h>
int main() {
int n, i, sum = 0;

printf("Enter a positive integer: ");


scanf("%d", &n);

15
for (i = 1; i <= n; ++i) {
sum += i;
}

printf("Sum = %d", sum);


return 0;
}
Output
Enter a positive integer: 1000
Sum = 500500

21. Write a C program to Print Alphabets From A to Z Using Loop.


#include <stdio.h>
int main() {
char c;
printf("Enter u to display uppercase alphabets.\n");
printf("Enter l to display lowercase alphabets. \n");
scanf("%c", &c);

16
if (c == 'U' || c == 'u') {
for (c = 'A'; c <= 'Z'; ++c)
printf("%c ", c);
} else if (c == 'L' || c == 'l') {
for (c = 'a'; c <= 'z'; ++c)
printf("%c ", c);
} else {
printf("Error! You entered an invalid character.");
}

return 0;
}
Output 1
Enter u to display uppercase alphabets.
Enter l to display lowercase alphabets.
u
ABCDEFGHIJKLMNOPQRSTUVWXYZ

Output 2
Enter u to display uppercase alphabets.
Enter l to display lowercase alphabets.
l
abcdefghijklmnopqrstuvwxyz

22. Write a C program to Check Leap Year.


#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);

17
// 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;
}
Output 1
Enter a year: 1900
1900 is not a leap year.
Output 2
Enter a year: 2012
2012 is a leap year.

23. Write a C program to Find Factorial of a Number.


#include <stdio.h>
int main() {
int n, i;
unsigned long long fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);

18
// 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

24. Write a C program to Make a Simple Calculator .


#include <stdio.h>

int main() {

char op;
double first, second;
printf("Enter an operator (+, -, *, /): ");

19
scanf("%c", &op);
printf("Enter two operands: ");
scanf("%lf %lf", &first, &second);

switch (op) {
case '+':
printf("%.1lf + %.1lf = %.1lf", first, second, first + second);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", first, second, first - second);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", first, second, first * second);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf", first, second, first / second);
break;
// operator doesn't match any case constant
default:
printf("Error! operator is not correct");
}

return 0;
}
Output 1
Enter an operator (+, -, *, /): +
Enter two operands: 5.2
6.5
5.2 + 6.5 = 11.7
Output 2
Enter an operator (+, -, *, /): -
Enter two operands: 5.9
4.2
5.9 - 4.2 = 1.7

Output 3
Enter an operator (+, -, *, /): *
Enter two operands: 6.7
20
2.1
6.7 * 2.1 = 14.1

Output 4
Enter an operator (+, -, *, /): /
Enter two operands: 6.6
2.2
6.6 / 2.2 = 3.0

25. Write a C program to Generate Multiplication Table .


#include <stdio.h>
int main() {
int n;
printf("Enter an integer: ");
scanf("%d", &n);

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


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

21
}
return 0;
}
Output
Enter an integer: 8
8*1=8
8 * 2 = 16
8 * 3 = 24
8 * 4 = 32
8 * 5 = 40
8 * 6 = 48
8 * 7 = 56
8 * 8 = 64
8 * 9 = 72
8 * 10 = 80

26. Write a C program to Print Fibonacci Series.


#include <stdio.h>
int main() {

int i, n;

// initialize first and second terms


int t1 = 0, t2 = 1;

22
// initialize the next term (3rd term)
int nextTerm = t1 + t2;

// get no. of terms from user


printf("Enter the number of terms: ");
scanf("%d", &n);

// print the first two terms t1 and t2


printf("Fibonacci Series: %d, %d, ", t1, t2);

// print 3rd to nth terms


for (i = 3; i <= n; ++i) {
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}

return 0;
}
Output
Enter the number of terms: 15
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377,

27. Write a C program to Find LCM of Two Numbers.


#include <stdio.h>

int main() {

int n1, n2, max, lcm;

printf("Enter two positive integers: ");


scanf("%d %d", &n1, &n2);

23
// 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;
}

printf("The LCM of %d and %d is %d.", n1, n2, lcm);

return 0;
}
Output
Enter two positive integers: 521
720
The LCM of 521 and 720 is 375120.

28. Write a C program to Check Armstrong Number


#include <stdio.h>
int main() {
int num, originalNum, remainder, result = 0;
printf("Enter a three-digit integer: ");
scanf("%d", &num);
originalNum = num;

while (originalNum != 0) {

24
// remainder contains the last digit
remainder = originalNum % 10;

result += remainder * remainder * remainder;

// removing last digit from the orignal number


originalNum /= 10;
}

if (result == num)
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);

return 0;
}
Output 1
Enter a three-digit integer: 371
371 is an Armstrong number.
Output 2
Enter a three-digit integer: 56
56 is not an Armstrong number.

29. Write a C program to Display Armstrong Numbers Between 1 to 1000.


#include <math.h>
#include <stdio.h>
int main() {
int low, high, number, originalNumber, rem, count = 0;
double result = 0.0;
printf("Enter two numbers(intervals): ");
scanf("%d %d", &low, &high);
printf("Armstrong numbers between %d and %d are: ", low, high);

25
// swap numbers if high < low
if (high < low) {
high += low;
low = high - low;
high -= low;
}

// iterate number from (low + 1) to (high - 1)


// In each iteration, check if number is Armstrong
for (number = low + 1; number < high; ++number) {
originalNumber = number;

// number of digits calculation


while (originalNumber != 0) {
originalNumber /= 10;
++count;
}

originalNumber = number;

// result contains sum of nth power of individual digits


while (originalNumber != 0) {
rem = originalNumber % 10;
result += pow(rem, count);
originalNumber /= 10;
}

// check if number is equal to the sum of nth power of individual digits


if ((int)result == number) {
printf("%d ", number);
}

// resetting the values


count = 0;
result = 0;
}

return 0;
26
}
Output
Enter two numbers(intervals): 1

1000
Armstrong numbers between 1 and 1000 are: 2 3 4 5 6 7 8 9 153 370 371 407

30. Write a C program to Reverse a Number.


#include <stdio.h>

int main() {
int n, reverse = 0, remainder, original;

printf("Enter an integer: ");


scanf("%d", &n);

original = n;

27
while (n != 0) {
remainder = n % 10;
reverse = reverse * 10 + remainder;
n /= 10;
}

if (original % 10 == 0) {
printf("Reversed number = %d", reverse);

while (original % 10 == 0) {
printf("0");
original /= 10;
}
} else {
printf("Reversed number = %d", reverse);
}

return 0;
}
Output
Enter an integer: 2719
Reversed number = 9172

31. Write a C program to Check Whether a Number is a Palindrome or Not.


#include <stdio.h>
int main() {
int n, reversed = 0, remainder, original;
printf("Enter an integer: ");
scanf("%d", &n);
original = n;

// reversed integer is stored in reversed variable


while (n != 0) {
remainder = n % 10;
reversed = reversed * 10 + remainder;
28
n /= 10;
}

// palindrome if orignal and reversed are equal


if (original == reversed)
printf("%d is a palindrome.", original);
else
printf("%d is not a palindrome.", original);

return 0;
}
Output 1
Enter an integer: 2002
2002 is a palindrome.
Output 2
Enter an integer: 2020
2020 is not a palindrome.

32. Write a C program to Check Whether a Number is Prime or Not.


#include <stdio.h>

int main() {

int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);

// 0 and 1 are not prime numbers


// change flag to 1 for non-prime number
if (n == 0 || n == 1)

29
flag = 1;

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

// if n is divisible by i, then n is not prime


// change flag to 1 for non-prime number
if (n % i == 0) {
flag = 1;
break;
}
}
// flag is 0 for prime numbers
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);

return 0;
}
Output 1
Enter a positive integer: 7
7 is a prime number.
Output 2
Enter a positive integer: 8
8 is not a prime number.
33. Write a C program to Find All Factors of a Natural Number.
#include <stdio.h>
int main() {
int num, i;
printf("Enter a positive integer: ");
scanf("%d", &num);
printf("Factors of %d are: ", num);
for (i = 1; i <= num; ++i) {
if (num % i == 0) {
printf("%d ", i);
}
}

30
return 0;
}
Output
Enter a positive integer: 100
Factors of 100 are: 1 2 4 5 10 20 25 50 100

34. Write a C program to Print Simple Pyramid Pattern.


#include <stdio.h>
int main() {
int i, space, rows, k = 0, count = 0, count1 = 0;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (space = 1; space <= rows - i; ++space) {
printf(" ");
++count;
}
while (k != 2 * i - 1) {

31
if (count <= rows - 1) {
printf("%d ", i + k);
++count;
} else {
++count1;
printf("%d ", (i + k - 2 * count1));
}
++k;
}
count1 = count = k = 0;
printf("\n");
}
return 0;
}
Output
Enter the number of rows: 5
1
232
34543
4567654
567898765

35. Write a C program to Print Given Triangle.


#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("%d ", j);
}
printf("\n");
}

32
return 0;
}
Output
Enter the number of rows: 5
1
12
123
1234
12345

36. Write a C program to Print Inverted Pyramid.

#include <stdio.h>

int main() {
int n;
printf("Enter the number of rows: ");
scanf("%d", &n);

for (int i = n; i >= 1; i--) {


// First half: Descending numbers

33
for (int j = i; j >= 1; j--) {
printf("%d ", j);
}

// Second half: Ascending numbers


for (int j = 2; j <= i; j++) {
printf("%d ", j);
}

printf("\n");
}

return 0;
}
Output
Enter the number of rows: 5
543212345
4321234
32123
212
1

37. Write a C program to Print Character Pattern.


#include <stdio.h>
int main()
{
int i, j;
int rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
// Taking first character of alphabet
// which is useful to print pattern
char character = 'A';
// This loop is used to identify

34
// number rows
for(i = 0; i < rows; i++)
{
// This for loop is used to
// identify number of columns
// based on the rows
for(j = 0; j <= i; j++)
{
// Printing character to get
// the required pattern
printf("%c ",character);
// Incrementing character value so
// that it will print the next character
character++;
}
printf("\n");
}
return 0;
}
Output
Enter the number of rows: 4
A
BC
DEF
GHIJ
37. Write a C program to Print Number Pattern.
#include <stdio.h>
int main() {
int n;
printf("Enter the number of rows: ");
scanf("%d", &n);
// Upper half of the diamond
for (int i = 1; i <= n; i++) {
for (int j = i; j < n; j++) {
printf(" ");
}
for (int k = 1; k < (i * 2); k++) {

35
printf("%d", k);
}
printf("\n");
}

// Lower half of the diamond


for (int i = n - 1; i >= 1; i--) {
for (int j = n; j > i; j--) {
printf(" ");
}
for (int k = 1; k < (i * 2); k++) {
printf("%d", k);
}
printf("\n");
}
return 0;
}
Output
Enter the number of rows: 3
1
123
12345
123
1

36

You might also like