Computer Scientists are Pretty Pessimistic

Showing posts with label Programs Based on Control Structures. Show all posts
Showing posts with label Programs Based on Control Structures. Show all posts

Monday, 26 October 2015

According to the Gregorian calendar, it was Monday on the date 01/01/1900. If any year is input through the keyboard write a program to find out what is the day on 1st January of this year.


Solution ::

#include <stdio.h>
int main ()                                   //Monday-0
{                                             //Tuesday-1
   int c=0,m=1,i,y,p;                         //Wednesday-2
   printf("Enter a year: ");                  //Thursday-3
   scanf("%d",&y);                            //Friday-4
   for(i=1991;i<=y;i++)                       //Saturday-5
   {                                          //Sunday-6
       if((i%4)==0&&(i%100)!=0||(i%400)==0) //After 7 days,c = 0;(Monday, 1 week)
        {                                //If a year leap year, next year c=c+2
            c++;                              //if(c==0) ; c = 0 ; Monday
            p=i;                              //if(c==8) ; c = 1 ; Tuesday
        }                                     //c===day
        else if((i-1)==p)
            c=c+2;
       else
        c++;

        if(c==7)
            c=0;
        else if(c==8)
            c=1;
   }
   if(c==0)
    printf("MONDAY");
   else if (c==1)
    printf("TUESDAY");
   else if (c==2)
    printf("WEDNESDAY");
   else if (c==3)
    printf("THURSDAY");
   else if (c==4)
    printf("FRIDAY");
   else if (c==5)
    printf("SATURDAY");
   else if (c==6)
    printf("SUNDAY");
   return 0;
}

Monday, 5 October 2015

WAP whether a number is prime or not using function.

CODE ::

#include <stdio.h>
#include <math.h>
int check (int p);
int main ()
{
    int n;
    printf("Enter a number: ");
    scanf("%d",&n);
    if(check(n))
        printf("Prime\n");
    else
        printf("Not prime\n");
    return 0;
}
int check (int p)
{
    int i,f=0,limit;
    limit=sqrt(p);
    for(i=2;i<=limit;i++)
    {
        if(p%i==0)
        {
            f=1;
            break;
        }
    }
    if(f==0)
        return 1;

    return 0;
}

Saturday, 3 October 2015

WAP to check that a given year is a leap year or not.

Definition of leap year:

Rule #1: A year is called leap year if it is divisible by 400.
For example: 1600, 2000 etc leap year while 1500, 1700 are not leap year.
Rule #2: If year is not divisible by 400 as well as 100 but it is divisible by 4 then that year are also leap year.
For example: 2004, 2008, 1012 are leap year.

CODE ::

#include <stdio.h>
void main ()
{
    int year;
    printf("Enter a year: ");
    scanf("%d",&year);

    if((year%4==0)&&(year%100!=0)||(year%400==0))
        printf("LEAP YEAR\n");
    else
        printf("NOT LEAP YEAR\n");
}

Wednesday, 23 September 2015

WAP to find out the sum of the following series.


                                                                        1 + x2/2! + x4/4! + x6/6! + …….xn/n!


CODE : :

#include <stdio.h>
#include <math.h>
long long fact(int n);
int main ()
{
    long long i,x,n;
    double sum=1;
    printf("Enter x and n: ");
    scanf("%lld %lld",&x,&n);
    for(i=2;i<=n;i=i+2)
    {
        sum=sum+pow(x,i)/(double)fact(i);
    }
    printf("Sum is : %.3lf\n",sum);
}
long long fact(int n)
{
    if(n==1)
        return 1;
    return n*fact(n-1);
}

Monday, 21 September 2015

If a number 972 is entered through the keyboard, your program should print “Nine Seven Two”. Write the program such that it does this for any positive integers.


Code :::

#include <stdio.h>
int main ()
{
    char a[100];
    int i;
    printf("Enter a number: ");
    gets(a);
    for(i=0;i<a[i];i++)
    {
        if(a[i]=='0')
            printf("Zero ");
        else if(a[i]=='1')
            printf("One ");
        else if(a[i]=='2')
            printf("Two ");
        else if(a[i]=='3')
            printf("Three ");
        else if(a[i]=='4')
            printf("Four ");
        else if(a[i]=='5')
            printf("Five ");
        else if(a[i]=='6')
            printf("Six ");
        else if(a[i]=='7')
            printf("Seven ");
        else if(a[i]=='8')
            printf("Eight ");
        else if(a[i]=='9')
            printf("Nine ");
    }
    return 0;
}

WAP to input a number through the keyboard until a ‘.’ Every time a number is entered. The program should display whether it is greater than, less than or equal to the previous number.


Code  : :

#include<stdio.h>
int main()
{
    int num,a;
    scanf("%d",&a);
    while(scanf("%d",&num))
    {
         if(num == '.') break;
         else if(num>a)
            printf("Grater\n");
         else if(num<a)
            printf("Less\n");
         else if(num==a)
            printf("Equal\n");
         a=num;
    }
    return 0;
}