100% found this document useful (1 vote)
829 views

C Programming Exercises

The document provides 16 code examples for C programming exercises that demonstrate how to: 1) find the maximum of two integers, 2) print natural numbers from 1 to n, 3) find the maximum of three numbers using nested if statements, 4) print even and odd numbers within a range, 5) print alphabets from a-z using ASCII values, and 6) calculate the sum of natural numbers within a range. Each code example is accompanied by its expected output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
829 views

C Programming Exercises

The document provides 16 code examples for C programming exercises that demonstrate how to: 1) find the maximum of two integers, 2) print natural numbers from 1 to n, 3) find the maximum of three numbers using nested if statements, 4) print even and odd numbers within a range, 5) print alphabets from a-z using ASCII values, and 6) calculate the sum of natural numbers within a range. Each code example is accompanied by its expected output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

C Programming Exercises

1. Write a C program to find maximum between two integers.


Code:
#include<stdio.h>
int main ()
{
int num1, num2, max;
printf("Enter a number: \n");
scanf("%d", &num1);
printf("Enter another number: \n");
scanf("%d", &num2);
if (num1>num2)
printf("\n%d is max\n", num1);
else
printf("\n%d is max\n", num2);

return 0;
}
Output:

2. Write a C program to find maximum between two integers.


Code:
#include<stdio.h>
int main ()
{
int num1, num2, max;
printf("Enter two numbers: \n");
scanf("%d%d", &num1,&num2);
if (num1>num2)
max= num1;
if (num2>num1)
max= num2;
printf("\n%d is max\n", max);

return 0;
}
3. Find maximum among three numbers using nested if statement.
Code:
#include <stdio.h>
int main()
{
int num1, num2, num3, max;
printf("Enter three numbers: \n");
scanf("%d%d%d", &num1, &num2, &num3);

if(num1 > num2)


{
if(num1 > num3)
{
max = num1;
}
else
{
max = num3;
}
}
else
{
if(num2 > num3)
{
max = num2;
}
else
{
max = num3;
}
}
printf("\nMaximum among all three numbers = %d\n", max);

return 0;

Output:
4. Print all natural numbers from 1 to n.
Code:
#include<stdio.h>
int main ()
{
int n, i;
printf("Enter limit:\n");
scanf("%d", &n);
printf("\nNatural numbers from 1 to %d", n);
for (i=1; i<=n; i++)
{
printf("\n%d\n", i);
}

return 0;
}
Output:

5. To print natural numbers in range.


Code:
#include<stdio.h>
int main ()
{
int start, end, i;
printf("Enter starting range:\n");
scanf("%d", &start);
printf("\nEnter ending range:\n");
scanf("%d", &end);
printf("\nNatural numbers from %d to %d is:", start, end);
for (i=start; i<=end; i++)
{
printf("%d ", i);
}

return 0;
}
Output:

6. To print natural numbers revers (n to 1).


Code:
#include<stdio.h>
int main ()
{
int start, i;
printf("Enter limit:\n");
scanf("%d", &start);
printf("\nNatural numbers from %d to 1", start);
for (i=start; i>=1; i--)
{
printf("\n%d\n", i);
}

return 0;
}
Output:
7. Natural numbers in reverse in range.
Code:
#include<stdio.h>
int main ()
{
int start, end, i;
printf("Enter starting range:\n");
scanf("%d", &start);
printf("\nEnter ending range:\n");
scanf("%d", &end);
printf("\nNatural numbers from %d to %d is:", end, start);
for (i=end; i>=start; i--)
{
printf("%d ", i);
}

return 0;
}
Output:

8. To print Alphabets from a-z.


Code:
#include<stdio.h>
int main ()
{
char ch;
printf("Alphabets from a- z:\n\n");
for (ch='a'; ch<='z'; ch++)
{
printf("%c ", ch);
}

return 0;
}
Output:
9. Program to display alphabets from a-z by ASCII values.
Code:
#include<stdio.h>
int main ()
{
int i;
printf("Alphabets from a-z:\n");
for (i=97; i<=122; i++)
{
printf("%c\t", i);
}

return 0;
}

Output:

10. To print all even numbers from 1 to n (Using if statement)


Code:
#include <stdio.h>
int main()
{
int i, n;
printf("Print all even numbers till: \n");
scanf("%d", &n);

printf("\nEven numbers from 1 to %d are: \n", n);

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


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

return 0;
}
Output:

11. Print all even numbers without using if statement.


Code:
#include<stdio.h>
int main ()
{
int i, n;
printf("All even numbers till:\n");
scanf("%d", &n);
for (i=2; i<=n; i+=2)\
{
printf("%d\n", i);
}

return 0;
}
Output:

12. Print odd numbers 1 to n.


Code:
#include<stdio.h>
int main ()
{
int n, i;
printf("Odd numbers till: ");
scanf("%d", &n);
printf("\nAll odd numbers from 1 to %d are:\n", n);
for (i=1; i<=n; i++)
{
if(i%2!=0)
{
printf("%d\t", i);
}
}

return 0;
}
Output:

13. Print odd numbers 1 to n without using if statement:


Code:
#include<stdio.h>
int main ()
{
int n, i;
printf("Odd numbers till: ");
scanf("%d", &n);
printf("\nAll odd numbers from 1 to %d are:\n", n);
for (i=1; i<=n; i+=2)
{
printf("%d\t", i);
}

return 0;
}
Output:
14. Program to print odd numbers in given range:
Code:
#include <stdio.h>
int main()
{
int i, start, end;

printf("Enter lower limit: ");


scanf("%d", &start);
printf("Enter upper limit: ");
scanf("%d", &end);

printf("All odd numbers from %d to %d are: \n", start, end);

/* If start is not odd then make it odd */


if(start%2==0)
{
start++;
}

for(i=start; i<=end; i+=2)


{
printf("%d\n", i);
}

return 0;
}
Output:

15. Sum of natural numbers from 1 to n.


Code:
#include<stdio.h>
int main ()
{
int n, i, sum=0;
printf("Enter upper limit:");
scanf("%d", &n);
for (i=1; i<=n; i++)
{
sum+=i;
}
printf("\nSum of all natural numbers from 1 to %d is: %d\n\n", n, sum);

return 0;
}
Output:

16. Sum of all natural numbers in given range.


Code:
#include <stdio.h>

int main()
{
int i, start, end, sum=0;
printf("Enter lower limit: ");
scanf("%d", &start);
printf("Enter upper limit: ");
scanf("%d", &end);

for(i=start; i<=end; i++)


{
sum += i;
}

printf("\nSum of natural numbers from %d to %d = %d\n\n", start, end, sum);

return 0;
}
Output:

You might also like