0% found this document useful (0 votes)
18 views12 pages

CODE

The document contains four C programs: one for calculating the factorial of a number using recursion, another for swapping two numbers using pointers, a third for simulating the solar system with graphical representation of planets, and a fourth for determining a zodiac sign based on a user's birth date. Each program includes user input, function definitions, and output statements. The code demonstrates fundamental programming concepts such as recursion, pointers, graphics, and conditional statements.

Uploaded by

nisha langeh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views12 pages

CODE

The document contains four C programs: one for calculating the factorial of a number using recursion, another for swapping two numbers using pointers, a third for simulating the solar system with graphical representation of planets, and a fourth for determining a zodiac sign based on a user's birth date. Each program includes user input, function definitions, and output statements. The code demonstrates fundamental programming concepts such as recursion, pointers, graphics, and conditional statements.

Uploaded by

nisha langeh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

1.

//Factorial of a number using Recursion


#include<stdio.h>

#include<conio.h>

long int multiplyNumbers(int n);

int main() {

int n;

clrscr();

printf("Enter a positive integer: ");

scanf("%d",&n);

printf("Factorial of %d = %ld", n, multiplyNumbers(n));

getch();

return 0;

long int multiplyNumbers(int n) {

if (n>=1)

return n*multiplyNumbers(n-1);

else

return 1;

}
2. /* Program to swap two numbers using pointers*/
#include <stdio.h>

#include<conio.h>

// function to swap the two numbers

void swap(int *x,int *y)

int t;

t = *x;

*x = *y;

*y = t;

int main()

int num1,num2;

clrscr();

printf("Enter value of num1: ");

scanf("%d",&num1);

printf("Enter value of num2: ");

scanf("%d",&num2);

//displaying numbers before swapping

printf("Before Swapping: num1 is: %d, num2 is: %d\n",num1,num2);


//calling the user defined function swap()

swap(&num1,&num2);

//displaying numbers after swapping

printf("After Swapping: num1 is: %d, num2 is: %d\n",num1,num2);

getch();

return 0;

}
3. //SOLAR SYSTEM
#include <stdio.h>

#include <conio.h>

#include <graphics.h>

#include <dos.h>

#include <math.h>

/* manipulates the position of planets on the orbit */

void planetMotion(int xrad, int yrad, int midx, int midy, int x[60], int y[60]) {

int i, j = 0;

/* positions of planets in their corresponding orbits */

for (i = 360; i > 0; i = i - 6) {

x[j] = midx - (xrad * cos((i * 3.14) / 180));

y[j++] = midy - (yrad * sin((i * 3.14) / 180));

return;

int main() {

/* request auto detection */

int gdriver = DETECT, gmode, err;

int i = 0, midx, midy;

int xrad[9], yrad[9], x[9][60], y[9][60];

int pos[9], planet[9], tmp;


/* initialize graphic mode */

initgraph(&gdriver, &gmode, "C:/TURBOC3/BGI");

err = graphresult();

if (err != grOk) {

/* error occurred */

printf("Graphics Error: %s",

grapherrormsg(err));

return 0;

/* mid positions at x and y-axis */

midx = getmaxx() / 2;

midy = getmaxy() / 2;

/* manipulating radius of all 9 planets */

planet[0] = 7;

for (i = 1; i < 9; i++) {

planet[i] = planet[i - 1] + 1;

/* offset position for the planets on their corresponding orbit */

for (i = 0; i < 9; i++) {

pos[i] = i * 6;

}
/* orbits for all 9 planets */

xrad[0] = 60, yrad[0] = 30;

for (i = 1; i < 9; i++) {

xrad[i] = xrad[i - 1] + 30;

yrad[i] = yrad[i - 1] + 15;

/* positions of planets on their corresponding orbits */

for (i = 0; i < 9; i++) {

planetMotion(xrad[i], yrad[i], midx, midy, x[i], y[i]);

while (!kbhit()) {

/* drawing 9 orbits */

setcolor(WHITE);

for (i = 0; i < 9; i++) {

ellipse(midx, midy, 0, 360, xrad[i], yrad[i]);

/* sun at the mid of the solar system */

setcolor(YELLOW);

setfillstyle(SOLID_FILL, YELLOW);

circle(midx, midy, 20);

floodfill(midx, midy, YELLOW);


/* mercury in first orbit */

setcolor(CYAN);

setfillstyle(SOLID_FILL, CYAN);

pieslice(x[0][pos[0]], y[0][pos[0]], 0, 360, planet[0]);

/* venus in second orbit */

setcolor(GREEN);

setfillstyle(SOLID_FILL, GREEN);

pieslice(x[1][pos[1]], y[1][pos[1]], 0, 360, planet[1]);

/* earth in third orbit */

setcolor(BLUE);

setfillstyle(SOLID_FILL, BLUE);

pieslice(x[2][pos[2]], y[2][pos[2]], 0, 360, planet[2]);

/* mars in fourth orbit */

setcolor(RED);

setfillstyle(SOLID_FILL, RED);

pieslice(x[3][pos[3]], y[3][pos[3]], 0, 360, planet[3]);

/* jupiter in fifth orbit */

setcolor(BROWN);

setfillstyle(SOLID_FILL, BROWN);

pieslice(x[4][pos[4]], y[4][pos[4]], 0, 360, planet[4]);


/* saturn in sixth orbit */

setcolor(LIGHTGRAY);

setfillstyle(SOLID_FILL, LIGHTGRAY);

pieslice(x[5][pos[5]], y[5][pos[5]], 0, 360, planet[5]);

/* uranus in sevth orbit */

setcolor(BROWN);

setfillstyle(SOLID_FILL, BROWN);

pieslice(x[6][pos[6]], y[6][pos[6]], 0, 360, planet[6]);

/* neptune in eigth orbit */

setcolor(LIGHTBLUE);

setfillstyle(SOLID_FILL, LIGHTBLUE);

pieslice(x[7][pos[7]], y[7][pos[7]], 0, 360, planet[7]);

/* pluto in ninth orbit */

setcolor(LIGHTRED);

setfillstyle(SOLID_FILL, LIGHTRED);

pieslice(x[8][pos[8]], y[8][pos[8]], 0, 360, planet[8]);

/* checking for one complete rotation */

for (i = 0; i < 9; i++) {

if (pos[i] <= 0) {

pos[i] = 59;

} else {
pos[i] = pos[i] - 1;

/* sleep for 100 milliseconds */

delay(100);

/* clears graphic screen */

cleardevice();

/* deallocate memory allocated for graphic screen */

closegraph();

return 0;

}
4. //Program To Display Zodiac Sign for Given Date of Birth
#include<stdio.h>

#include<conio.h>

int main()

int m, day;

clrscr();

printf("Enter your birth month(1-12)\n");

scanf("%d", &m);

printf("Enter your birth day\n");

scanf("%d", &day);

if( (m == 12 && day >= 22) || (m == 1 && day <= 19) )

printf("Your Zodiac Sign based on your Birth date is Capricorn\n");

else if( (m == 1 && day >= 20) || (m == 2 && day <= 17) )

printf("Your Zodiac Sign based on your Birth date is Aquarius\n");

else if( (m == 2 && day >= 18) || (m == 3 && day <= 19) )

printf("Your Zodiac Sign based on your Birth date is Pisces\n");


}

else if( (m == 3 && day >= 20) || (m == 4 && day <= 19) )

printf("Your Zodiac Sign based on your Birth date is Aries\n");

else if( (m == 4 && day >= 20) || (m == 5 && day <= 20) )

printf("Your Zodiac Sign based on your Birth date is Taurus\n");

else if( (m == 5 && day >= 21) || (m == 6 && day <= 20) )

printf("Your Zodiac Sign based on your Birth date is Gemini\n");

else if( (m == 6 && day >= 21) || (m == 7 && day <= 22) )

printf("Your Zodiac Sign based on your Birth date is Cancer\n");

else if( (m == 7 && day >= 23) || (m == 8 && day <= 22) )

printf("Your Zodiac Sign based on your Birth date is Leo\n");

else if( (m == 8 && day >= 23) || (m == 9 && day <= 22) )

printf("Your Zodiac Sign based on your Birth date is Virgo\n");

else if( (m == 9 && day >= 23) || (m == 10 && day <= 22) )
{

printf("Your Zodiac Sign based on your Birth date is Libra\n");

else if( (m == 10 && day >= 23) || (m == 11 && day <= 21) )

printf("Your Zodiac Sign based on your Birth date is Scorpio\n");

else if( (m == 11 && day >= 22) || (m == 12 && day <= 21) )

printf("Your Zodiac Sign based on your Birth date is Sagittarius\n");

else

printf("Invalid Birth date entered\n");

getch();

return 0;

You might also like