Computer Scientists are Pretty Pessimistic

Showing posts with label Others. Show all posts
Showing posts with label Others. Show all posts

Tuesday, 29 March 2016

Find 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.

Solution ::

#include <stdio.h>
int main()
{
    int year;
    printf("Enter your Year: ");
    scanf("%d",&year);
    if(((year%4)==0)&&((year%100)!=0)||((year%400)==0))
    {
        printf("%d is a leap Year",year);
    }

    else
    {
         printf(" %d is not a Leap Year\n",year);
    }

    return 0;
}

Thursday, 10 December 2015

Write a program that uses a function named wt_on_mn which takes as argument your weight on earth, converts it to that on the moon and displays it. The weight on earth is supposed to be entered through keyboard as a floating point number, and it is known that the moons gravity is 17 percent of the gravity of earth.



# We know that , weight = mass X gravity ; 

Suppose your weight on earth is 80.55 Newton, So, your mass is = (80.55/9.8) = 8.22 kg ; Now if we multiply your mass with moon_gravity we will find your weight on moon (Simple enough) . 

# Given that moon_gravity is 17% of the gravity of earth.

So, weight = mass X moon_gravity ; 
                 
= mass X ( (9.8 X 17) / 100 ) ; // We use this formula in our code

= 8.22 X ( (9.8 X 17) / 100 ) ;

 = 13.69 N ; 

Note : If you have any confusion about this formula follow #Intermediate Physics Book (Chapter Name - Gravity)


Solution ::

  1. #include <stdio.h> 
  2. void wt_on_mn(float mass); //Declare Function, this function calculate the weight and print it so here have no return type; 
  3. void main () 
  4.     float weight,mass; 
  5.     printf("Enter your weight on earth:\n"); 
  6.     scanf("%f",&weight); 
  7.     mass = (weight/9.8) ; 
  8.     wt_on_mn(mass); //Function called 
  9. void wt_on_mn(float mass) //Function
  10.     float w_on_moon; 
  11.     w_on_moon = mass * ((9.8*17)/100); 
  12.     printf("\nYour weight on moon is : %.2f\n",w_on_moon); 

Here, I do this code in very details. A problem can be solved in many way. So, you can modify this code as your wish.


Happy Coding.


Monday, 5 October 2015

Using ternary operator (? :) write down an expression to find largest of three numbers.

CODE ::

#include <stdio.h>
int main ()
{
    int a=15,b=20,c=10,big;

    big = (a>b) ? a : b;

    big = (big>c) ? big : c;

    printf("%d\n",big);

    return 0;
}


Sunday, 4 October 2015

Write a program to print the sum and average of first n even numbers which are divisible by 3.

Enter n: 2 6+12 Sum is: 18 Average is: 9.0

CODE :: #include <stdio.h> int main () { int n,i=2,j,c=0,sum=0; double avg; printf("Enter n: "); scanf("%d",&n); while(c!=n) { if(i%3==0) { if(c<n-1) printf("%d+",i); else printf("%d",i); sum=sum+i; c++; } i=i+2; } printf("\nSum is: %d",sum); printf("\nAverage is: %.1lf",sum/(double)n); return 0; }  

Saturday, 3 October 2015

WAP to find n perfect numbers where n is the input from user.

Sample input : 3
Sample output : 6 28 496


CODE ::

#include <stdio.h>
int main ()
{
    long int i=1,j,n,sum,c=0;
    printf("Enter number of perfect number(s): ");
    scanf("%ld",&n);
    while(c!=n)
    {
        j=1;
        sum=0;
        while(j<i)
        {
            if(i%j==0)
                sum=sum+j;
            j++;
        }
        if(sum==i)
        {
            printf("%ld ",i);
            c++;
        }
        i++;
    }

    return 0;
}

Tuesday, 29 September 2015

Print a sentence in a frame.



CODE : :


#include <stdio.h>
#include <string.h>
int main ()
{
    int b[150],i,j=0,k,n,c=0,l,big=0,p=0,w=0;  //i,k,n=loop control
    char a[1000];
    printf("Enter a string: ");
    gets(a);
    l=strlen(a);
    for(i=0;i<=l;i++)
    {
        if(a[i]>='a'&&a[i]<='z'||a[i]>='A'&&a[i]<='Z')
            c++; //c=count letter in a word

        else if(a[i]==' '||i==l)
        {
            b[j]=c; //j=array control // b integer array, to put number of letter in a word
            c=0;
            j++;
        }
    }
    for(i=0;i<j;i++)
        {
            if(b[i]>big)
                big=b[i]; //maximum letter in a word (exp: my name is imtiyaz; here imtiyaz= 7)
        }                  // here big =7, for imtiyaz.
    for(i=1;i<=j+2;i++)
    {
        if(i==1||i==(j+2))
            {
                for(k=0;k<big+4;k++) //print (7+4)=11 star in first line
                    printf("*");
                printf("\n"); //new line after 1st and last line
            }
        else
        {
            printf("* ");//a space here
            while(a[p]!=' ')
            {
                printf("%c",a[p]);
                w++; //count letter my=2,name=4,is=2
                p++;
                if(p==l) //print char array p=0,p=1,p=2,p=3
                    break;
            }
                for(n=0;n<(big-w);n++) //print space after printing a word (* my      *), cause(7-2)=5space print
                    printf(" ");
                printf(" *"); //a space here
                p++; // increment p;
                w=0;
                printf("\n"); //new line after every line
        }
    }
    return 0;
}