Showing posts with label given. Show all posts
Showing posts with label given. Show all posts

C Program To Find Week Day Of A Given Date

How to write a C Program To Find Week Day Of A Given Date in C Programming Language ?


A real time Program is an Integration of all the constructs what we have learned until now. Hence if your practice on the previous sessions are practically proper then we can attempt any logic which cal make intelligent program. Let us take a test. Let us write a Program to find the WEEK DAY of a Given DATE.

Solution For C Program :

/*C Program To Find Week Day Of A Given Date*/

#include<stdio.h>
void main()
{
int d, m, y, n, nleap, nyear, nord, nodd1, nodd2, day, j, sum;
printf("\nEnter Any Date (DD-MM-YYYY) Format Only : ");
scanf("%d-%d-%d", &d, &m, &y);
printf("\n\nDate Entered is : %d - %d - %d", d, m, y);
if(y >= 1600 && y <= 1900)
{
day = 0;
nyear = y - 1600;
}
if(y >= 1901 && y <= 2400)
{
day = 1;
nyear = y - 1901;
}
nleap = nyear / 4;
nord = nyear - nleap;
nodd1 = 2 * nleap + 1 * nord;
day += nodd1 % 7;
sum = 0;
for(j = 1; j < m; j++)
{
switch(j)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
sum += 31;
break;
case 4: case 6: case9: case 11:
sum += 30;
break;
case 2:
if(y % 4 == 0)
sum += 29;
else
sum += 28;
}
}
sum += d;
nodd2 = sum % 7;
day += nodd2;
day %= 7;
switch(day)
{
case 0:
printf("\nDay is Sunday ");
break;
case 1:
printf("\nDay is Monday ");
break;
case 2:
printf("\nDay is Tuesday ");
break;
case 3:
printf("\nDay is Wednesday ");
break;
case 4:
printf("\nDay is Thursday ");
break;
case 5:
printf("\nDay is Friday ");
break;
case 6:
printf("\nDay is Saturday ");
break;
}
}


You may also learn these C Program/Code :

C Program To Swap Two Numbers Without Using Third Variable

Give me an integer and I will sum it with the previous natural numbers

How to write a C Program Give me an integer and I will sum it with the previous natural numbers in C Programming Language ?

This C Program Change an Integer Number with Addition of Previous Natural Numbers.
 
Solution:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int geometric_sum (int a);
  4. int main ()
  5. {
  6.     int a = 0, sum = 0;
  7.     printf("Give me an integer and I will sum it with the previous natural numbers : ");
  8.     scanf("%d", &a);
  9.     sum = geometric_sum(a);
  10.     printf("Result: %d", sum);
  11. }
  12.  
  13. int geometric_sum (int a)
  14. {
  15.     if (== 0)
  16.     {
  17.         printf("Base case!\n");
  18.         return a;
  19.     }
  20.  
  21.     else
  22.     {
  23.         printf("1: sum (%d)\n", a);
  24.         return a + geometric_sum(a-1);
  25.     }
  26.  
  27. }

C Program to Check Given String is Palindrome or Not

How to write a C Program to Check Given String is Palindrome or Not in C Programming Language ?


Solution:

  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <string.h>
  4. void main()
  5. {
  6.  char a[10],b[10];
  7.  clrscr();
  8.  printf("\nEnter a string: ");
  9.  gets(a);
  10.  strcpy(b,a);
  11.  strrev(b);
  12.  if(strcmp(a,b)==0)
  13.  {
  14.   printf("%s is palindrom",a);
  15.  }
  16.  else
  17.  {
  18.   printf("%s is not palindrom",a);
  19.  }
  20.  getch();
  21. }

C Program That in a Given Natural Number x Insert Figure c at Position p

How to Write a program that in a given natural number x insert figure c at position p . The numbers x , c and p are entered from standard input . 


For example , x = 140 , c = 2 , p = 2 , the result is the 1420th


Solution:
  1. #include<stdio.h>
  2. #include<math.h>

  3. int main() {
  4.         int x,c,p,i,y,d;
  5.         scanf("%d%d%d", &x,&c,&p);
  6.         printf("Number: %d, figure %d, required position: %d\n", x,c,p);
  7.         i=1;   
  8.         y=10;  
  9.         if(i==p){
  10.                 x=x*10;
  11.                 x=x+c;
  12.                 printf("X: %d\n", x);
  13.         }
  14.         while(i<p){
  15.         i++;
  16.         c=c*10;
  17.                 if(i==p){
  18.                 d=x%y;
  19.                 x=x-d;
  20.                 x=x*y;
  21.                 x=x+c+d;
  22.                 printf("X: %d\n", x);  
  23.                 }
  24.         y=10*y;
  25.         }
  26.                
  27.  
  28.         return 0;
  29. }

Return the number of tokens given a command

C Program Return the number of tokens given a command


  1. /* in setup_stack()... */  
  2.  
  3.   int i = 0;
  4.   int argc = get_argc(file_name);
  5.   char *token, *save_ptr;
  6.   char **argv = malloc((argc + 1) * sizeof(char *));
  7.   size_t token_size;
  8.  
  9.   //generate argv and push args onto stack

  10.   for (token = strtok_r((char *)file_name, " ", &save_ptr); token != NULL; token = strtok_r(NULL, " ", &save_ptr)) {
  11.     token_size = strlen(token) + 1;
  12.     *esp = *esp - token_size;
  13.     argv[i] = *esp;
  14.     i++;
  15.     memcpy(*esp, token, token_size);
  16.   } argv[argc] = 0;
  17.  
  18.   //perform alignment

  19.   size_t alignment = (size_t) *esp % 4;
  20.   if (alignment) {
  21.     *esp = *esp - alignment;
  22.     memcpy(*esp, &argv[argc], alignment);
  23.   }
  24.  
  25.   //push argv pointers

  26.   for (= argc; i >= 0; i--) {
  27.     *esp = *esp - sizeof(char *);
  28.     memcpy(*esp, &argv[i], sizeof(char *));
  29.   }
  30.  
  31.   //push pointer to argv

  32.   char *ptr = *esp;
  33.   *esp = *esp - sizeof(char **);
  34.   memcpy(*esp, &ptr, sizeof(char **));
  35.  
  36.   //push argc
  37.   *esp = *esp - sizeof(int);
  38.   memcpy(*esp, &argc, sizeof(int));
  39.  
  40.  //push dummy return address

  41.   *esp = *esp - sizeof(void *);
  42.   memcpy(*esp, &argv[argc], sizeof(void *));
  43.  
  44.   free(argv);
  45.   return success;
  46. }
  47.  
  48. /* Return the number of tokens given a command 'file_name.' */

  49. int get_argc(const char* file_name) {
  50.     int argc = 0;
  51.     char *buffer, *token, *save_ptr;
  52.     buffer = malloc(sizeof(file_name));
  53.     strlcpy(buffer, file_name, sizeof(file_name));
  54.     for (token = strtok_r((char *)buffer, " ", &save_ptr); token != NULL; token = strtok_r(NULL, " ", &save_ptr)) {
  55.         argc++;
  56.     }
  57.     free(buffer);
  58.     return argc;
  59. }