Search code snippets, questions, articles...

c code snippets

divisible1
// divisible
#include <stdio.h>

int main()
{
    int n=0;
    scanf("%d",&n);
    if(n%2==0)
        printf("Divisible by 2\n");
    if(n%3==0)
        printf("Divisible by 3\n");
    if(n%5==0)
        printf("Divisible by 5\n");
    if(n%2!=0 && n%3!=0 && n%5!=0)
        printf("Not Divisible by 2,3,5\n");


    return 0;
}
temperature1
#include <stdio.h>
#include<math.h>
//farenheit int input, celcius float output
int main()
{
    int f;float c;
    scanf("%d",&f);
    c=(f-32)*(5/9.0);
    printf("Celcius temp=%f",c);
    return 0;
}
debug1 watches
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int n=0;
    scanf("%d",&n);
    n=1000;
    printf("value of n=%d",n);
    return 0;
}
smiling duck
#include <stdio.h>
#include<math.h>
int main()
{
    printf("   _      _      _  \n");
    printf(" _(.)<  _(.)>  _(.)=    \n");
    printf("\\___)  \\___)  \\___)  ");

    return 0;
}
switch1
#include <stdio.h>
int main() {
    int x=2;
    switch(x)
    {
    case 1: printf("Choice is 1"); break;
    case 2: printf("Choice is 2"); break;
    case 3: printf("Choice is 3"); break;
    default: printf("Choice other than 1,2,3"); break;

    }
    return 0;
}
A program that takes the radius of a sphere as an integer as input and outputs the volume of that sphere as a floating point number. V=4/3*Pi*r*r*r
#include <stdio.h>
int main()
{
int r; double v; double pi=22/7.0;
printf("Radius of sphere=");
scanf("%d",&r);
v=(4/3.0)*pi*r*r*r;//int division
printf("Volume of sphere=%lf",v);

return 0;
}
Ma
n
mario harvard cs50
#include <stdio.h>
#include <cs50.h>
int main(void)
{

    int i = get_int("number: ");
    const int z = i;

    while (i > 0)
    {
        int n = i;
        while (n > 0)
        {
            printf(" ");
            n--;
        }
        int o = z;
        while (i <= o)
        {
            printf("#");
            o--;
        }

        printf("  ");

        int u = z;
        while (i <= u)
        {
            printf("#");
            u--;
        }


        i--;
        printf("\n");
    }

printf("\n");
}