PROBLEM SOLVING USING C PROGRAMING
[Link] whether the given year is leap year or not.
Program:
#include<stdio.h>
void main()
{
int year;
printf("enter the year");
scanf("%d",&year);
if((year%4==0 && year%100!=0) || year%400==0)
{
printf("year is a leap year");//we can also use other condition in else if
}else
{
printf("year is not a leap year");
}
return 0;
}
Output:
[Link] whether a given number is even or odd.
Program:
#include<stdio.h>
void main()
{
int number;
printf("enter the number");
scanf("%d",&number);
if(number%2==0)
{
printf("The given number is even %d",number);
}else
{
printf("The given number is odd %d",number);
}
return 0;
}
Output:
[Link] whether the given number is negative or positive.
Program:
#include<stdio.h>
void main()
{
int number;
printf("enter the number");
scanf("%d",&number);
if(number>0)
{
printf("The given number is positive %d",number);
}
else if(number<0)
{
printf("The given number is negative %d",number);
}
else
{
printf("The given number is neither positive nor negative",number);
}
return 0;
}
Output:
[Link] of N numbers:
Program:
#include<stdio.h>
void main()
{
int number,i=0,sum=0;
printf("enter the number");
scanf("%d",&number);
while(i<=number){
sum = sum+i;
i++;
}
printf("%d",sum);
return 0;
}
Output:
[Link] of digits for a given number.
Program:
#include<stdio.h>
void main()
{
int number,i,sum=0;
printf("enter the positive number");
scanf("%d",&number);
while(number>0){
i = number%10;//it returns the unit digit of a number
sum = sum+i;
number = number/10;//it will remove the last element from a given number
}
printf("%d",sum);
return 0;
}
Output:
[Link] series for even and odd of a given number.
#include<stdio.h>
void main()
{
int number,i=1;//Here the value of a i start from one since zero is neither positive nor negative
printf("enter the number");
scanf("%d",&number);
while(i<=number){
if(i%2==0){
printf("\neven number %d ",i);
}else{
printf("\nodd number %d ",i);
}
i++;
}
return 0;
}
Output:
[Link] of a given number .
Program:
#include<stdio.h>
void 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);
}
}
Output:
[Link] series of a number.
Program:
#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);
// 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:
[Link] whether a given number is pallidrome or not:
#include<stdio.h>
void main()
{
int n, reversed = 0, remainder, original;
printf("Enter an integer: ");
scanf("%d", &n);
original = n;
while (n != 0) {
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}
if (original == reversed) //we did n't want to use curly braces for conditional statements in a single
statement
printf("%d is a palindrome.", original);
else
printf("%d is not a palindrome.", original);
return 0;
}
Output:
[Link] whether a number is Armstrong or not:
Program:
#include<stdio.h>
void main()
{
int num, originalNum, remainder, result = 0;
printf("Enter a three-digit integer: ");
scanf("%d", &num);
originalNum = num;
while (originalNum != 0) {
remainder = originalNum % 10;
result += remainder * remainder * remainder;
originalNum /= 10;
}
if (result == num)
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);
return 0;
}
Output:
Enter a three-digit integer: 123
123 is not an Armstrong number.
11. Write a C program to find the largest among three numbers using if-else statements
#include<stdio.h>
int main()
{
int a, b ,c;
printf("Enter the values of A,B C ");
scanf("%d%d%d",&a,&b,&c);
if(a>b&&a>c)
{
printf(" A is Bigger");
}
else
{
if(b>c)
printf("B is Bigger");
else
printf("C is Bigger");
}
}
Output:
Enter the values of A,B C
15
56
78
C is Bigger
12. Write a C Program find the ASCII value of a given character.
#include<stdio.h>
int main()
{
char c;
printf("Enter the character:");
scanf("%c",&c);
printf("Entered character %c=%d",c,c);
}
Output:
Enter the character:A
Entered character A=65
13. Write a C Program to Swap Two Numbers
#include<stdio.h>
int main ()
{
int a,b,t;
printf("Enter two values:");
scanf("%d%d",&a,&b);
t=a;
a=b;
b=t;
printf("After Swaping :%d\n%d",a,b);
return 0;
}
Output:
Enter two values:45
89
After Swaping :89
45
14. Write a C program to find the GCD (Greatest Common Divisor) of two numbers
#include <stdio.h>
int main()
{
int n1, n2, i, gcd;
printf("Enter two integers: ");
scanf("%d %d", &n1, &n2);
for(i=1; i <= n1 && i <= n2; ++i)
{
// Checks if i is factor of both integers
if(n1%i==0 && n2%i==0)
gcd = i;
}
printf("G.C.D of %d and %d is %d", n1, n2, gcd);
return 0;
}
Output:
Enter two integers: 45 5
G.C.D of 45 and 5 is 5