C Programming if else Problem with Solution
C Programming के कुछ if else problem का solutions. 👇1. Program to take an alphabet from user and check whether it is a vowel or not.
#include<stdio.h>
void main()
{
char ch;
clrscr();
printf("Enter an alphabet : ");
scanf("%c", &ch);
if (ch=='a' || ch=='A' || ch=='e' || ch=='E' || ch=='i' || ch=='I' || ch=='o' || ch=='O' || ch=='u' || ch=='U')
printf("%c is a vowel.", ch);
else
printf("%c is not a vowel.", ch);
getch();
}
2. Program to take two numbers and check whether they are amicable numbers or not.
#include<stdio.h>
#include<conio.h>
int check(int a, int b)
{
int s = 0, i;
for (i = 1; i < a; i++)
{
if (a % i == 0)
{
s = s + i;
}
}
if (s == b)
{
s = 0;
for (i = 1; i < b; i++)
{
if (b % i == 0)
{
s = s + i;
}
}
if (s == a)
return 1;
else
return 0;
}
return 0;
}
void main()
{
int a, b;
clrscr();
printf("Enter 1st number : ");
scanf("%d", &a);
printf("Enter 2nd number : ");
scanf("%d", &b);
if (check(a, b))
{
printf("\n%d and %d are Amicable Numbers.", a, b);
}
else
{
printf("\n%d and %d are not Amicable Numbers.", a, b);
}
}
3. Program to accept two integer numbers and print the GCD(Greatest Common Divisor).
#include<stdio.h>
#include<conio.h>
void main()
{
int x, y, m, i;
clrscr();
printf("Enter 1st number : ");
scanf("%d", &x);
printf("Enter 2nd number : ");
scanf("%d", &y);
if (x > y)
m = y;
else
m = x;
for (i = m; i >= 1; i--)
{
if (x % i == 0 && y % i == 0)
{
printf("GCD of two number is : %d", i);
break;
}
}
getch();
}
4. Program to print sum of 'n' prime numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i = 3, count, c, sum = 2;
clrscr();
printf("Enter total number of prime numbers for addition : ");
scanf("%d", &n);
if (n >= 1)
{
printf("\nFirst %d prime numbers are :", n);
printf("\n2 ");
}
for (count = 2; count <= n;)
{
for (c = 2; c <= i - 1; c++)
{
if (i % c == 0)
break;
}
if (c == i)
{
sum = sum + i;
printf("%d ", i);
count++;
}
i++;
}
printf("\nSum : %d", sum);
getch();
}
5. Program to print sum of factorial series 1/1! + 2/2! +...1/N!
#include<stdio.h>
#include<conio.h>
double sumseries(double);
void main()
{
double number, sum;
clrscr();
printf("Enter the number : ");
scanf("%lf", &number);
sum = sumseries(number);
printf("\nSum of the above series = %lf ", sum);
getch();
}
double sumseries(double m)
{
double sum2 = 0, f = 1, i;
for (i = 1; i <= m; i++)
{
f = f * i;
sum2 = sum2 + (i / f);
if (i == m)
{
printf("%.2lf / %.2lf = %lf", i, f, sum2);
}
else
{
printf("%.2lf / %.2lf + \n", i, f);
}
}
return(sum2);
}
6. Program to check the leap year.
#include<stdio.h>
#include<conio.h>
void main()
{
int year;
clrscr();
printf("Enter a year : ");
scanf("%d", &year);
if (year % 400 == 0)
printf("\n%d is a leap year.", year);
else if (year % 100 == 0)
printf("\n%d is not a leap year.", year);
else if (year % 4 == 0)
printf("\n%d is a leap year.", year);
else
printf("%d is not a leap year.", year);
getch();
}
0 टिप्पणियाँ
अगर ऊपर दिए गए पाठ में आपको कोई Doubt है , तो Comment करे हमारी Team जल्द ही आपकी सहायता करेंगी।