#include <stdio.h>
int sod(long int);
long int revnum(long int);
int isLeap(int);
void showTable(long int);
float power(int, int);
long int fact(long int);
int isValidDate(int,int,int);
int main(void)
{
int choice, year;
long int num;
do
{
printf("\n1.Sum of digits\n2.Reverse a number\n3.Leap\n4.Multiplication table\n\
5.Power\n6.Factorial\n7.Valid date\n8.Exit\nChoice :");
scanf("%d", &choice);
switch(choice)
{
case 1:
{
printf("\nEnter a number :");
scanf("%ld", &num);
printf("\nSum of digits is of %ld is %d", num, sod(num));
break;
}
case 2:
{
printf("\nEnter a number :");
scanf("%ld", &num);
printf("\nReverse of %ld is %ld", num, revnum(num));
break;
}
case 3:
{
printf("\nEnter a year :");
scanf("%d", &year);
if(isLeap(year))
printf("\n%d is a leap year", year);
else
printf("\n%d is NOT a leap year", year);
break;
}
case 4:
{
printf("\nEnter a number :");
scanf("%ld", &num);
showTable(num);
break;
}
case 5:
{
int base, index;
printf("\nEnter the base and index :");
scanf("%d%d",&base, &index);
printf("\n%d raised to %d is %f", base, index, power(base, index));
break;
}
case 6:
{
printf("\nEnter a number :");
scanf("%ld", &num);
printf("\nFactorial of %ld is %ld", num, fact(num));
break;
}
case 7:
{
int day, month;
printf("\nEnter Date (dd mm yy) : ");
scanf("%d%d%d", &day, &month, &year);
if(isValidDate(day, month, year))
printf("%d / %d / %d is a valid date", day, month, year);
else
printf("Invalid date");
break;
}
default :
printf("\nInvalid choice");
}
}while(choice != 8);
return 0;
}
int sod(long int num)
{
int s = 0;
while(num > 0)
{
s += (num % 10);
num /= 10;
}
return s;
}
long int revnum(long int num)
{
long int rev = 0;
while(num > 0)
{
rev = (rev * 10) + (num % 10);
num /= 10;
}
return rev;
}
int isLeap(int year)
{
return (year % 100 == 0 && year % 400 == 0) ||
(year % 100 != 0 && year % 4 == 0) ? 1 : 0;
}
void showTable(long int num)
{
int cnt;
for(cnt = 1 ; cnt <= 10 ; cnt++)
{
printf("\n%ld X %d = %ld", num, cnt, num * cnt);
}
}
float power(int base, int index)
{
int tmp;
float res = 1.0;
tmp = index;
for( ; index != 0 ; )
{
res *= base;
if(index > 0) index--;
else index++;
}
if(tmp < 0)
res = 1 / res;
return res;
}
long int fact(long int num)
{
long int f;
for(f = 1 ; num > 0 ; num--)
f *= num;
return f;
}
int isValidDate(int day,int month,int year)
{
if(year >= 1)
{
if((day >= 1 && day <= 31) &&
(month == 1 || month == 3 || month == 5 || month == 7 ||
month == 8 || month == 10 || month == 12))
{
return 1;
}
else if((day >= 1 && day <= 30) &&
(month == 4 || month == 6 || month == 9 || month == 11))
{
return 1;
}
else if((day >= 1 && day <= 29) && (month == 2) && isLeap(year))
{
return 1;
}
else if((day >= 1 && day <= 28) && (month == 2))
{
return 1;
}
}
return 0;
}
int sod(long int);
long int revnum(long int);
int isLeap(int);
void showTable(long int);
float power(int, int);
long int fact(long int);
int isValidDate(int,int,int);
int main(void)
{
int choice, year;
long int num;
do
{
printf("\n1.Sum of digits\n2.Reverse a number\n3.Leap\n4.Multiplication table\n\
5.Power\n6.Factorial\n7.Valid date\n8.Exit\nChoice :");
scanf("%d", &choice);
switch(choice)
{
case 1:
{
printf("\nEnter a number :");
scanf("%ld", &num);
printf("\nSum of digits is of %ld is %d", num, sod(num));
break;
}
case 2:
{
printf("\nEnter a number :");
scanf("%ld", &num);
printf("\nReverse of %ld is %ld", num, revnum(num));
break;
}
case 3:
{
printf("\nEnter a year :");
scanf("%d", &year);
if(isLeap(year))
printf("\n%d is a leap year", year);
else
printf("\n%d is NOT a leap year", year);
break;
}
case 4:
{
printf("\nEnter a number :");
scanf("%ld", &num);
showTable(num);
break;
}
case 5:
{
int base, index;
printf("\nEnter the base and index :");
scanf("%d%d",&base, &index);
printf("\n%d raised to %d is %f", base, index, power(base, index));
break;
}
case 6:
{
printf("\nEnter a number :");
scanf("%ld", &num);
printf("\nFactorial of %ld is %ld", num, fact(num));
break;
}
case 7:
{
int day, month;
printf("\nEnter Date (dd mm yy) : ");
scanf("%d%d%d", &day, &month, &year);
if(isValidDate(day, month, year))
printf("%d / %d / %d is a valid date", day, month, year);
else
printf("Invalid date");
break;
}
default :
printf("\nInvalid choice");
}
}while(choice != 8);
return 0;
}
int sod(long int num)
{
int s = 0;
while(num > 0)
{
s += (num % 10);
num /= 10;
}
return s;
}
long int revnum(long int num)
{
long int rev = 0;
while(num > 0)
{
rev = (rev * 10) + (num % 10);
num /= 10;
}
return rev;
}
int isLeap(int year)
{
return (year % 100 == 0 && year % 400 == 0) ||
(year % 100 != 0 && year % 4 == 0) ? 1 : 0;
}
void showTable(long int num)
{
int cnt;
for(cnt = 1 ; cnt <= 10 ; cnt++)
{
printf("\n%ld X %d = %ld", num, cnt, num * cnt);
}
}
float power(int base, int index)
{
int tmp;
float res = 1.0;
tmp = index;
for( ; index != 0 ; )
{
res *= base;
if(index > 0) index--;
else index++;
}
if(tmp < 0)
res = 1 / res;
return res;
}
long int fact(long int num)
{
long int f;
for(f = 1 ; num > 0 ; num--)
f *= num;
return f;
}
int isValidDate(int day,int month,int year)
{
if(year >= 1)
{
if((day >= 1 && day <= 31) &&
(month == 1 || month == 3 || month == 5 || month == 7 ||
month == 8 || month == 10 || month == 12))
{
return 1;
}
else if((day >= 1 && day <= 30) &&
(month == 4 || month == 6 || month == 9 || month == 11))
{
return 1;
}
else if((day >= 1 && day <= 29) && (month == 2) && isLeap(year))
{
return 1;
}
else if((day >= 1 && day <= 28) && (month == 2))
{
return 1;
}
}
return 0;
}
No comments:
Post a Comment
kiss on google ads if you are anonymous because your ip is trackable.thank you.
......from.admin