Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
94 views

C Programming

The document contains 18 code snippets demonstrating various programming concepts in C language: 1. Multiplication table, pyramid, swapping numbers, Fibonacci series, getting user input. 2. Checking prime numbers, palindromic numbers, odd/even numbers, finding greatest of 3 numbers. 3. Finding greatest of 10 numbers entered by user, printing star pyramids, diamond pattern. 4. Generating and checking Armstrong numbers, Floyd's triangle, Pascal's triangle. 5. Implementing bubble sort, calculating HCF and LCM of two numbers.

Uploaded by

Kartik Shroff
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views

C Programming

The document contains 18 code snippets demonstrating various programming concepts in C language: 1. Multiplication table, pyramid, swapping numbers, Fibonacci series, getting user input. 2. Checking prime numbers, palindromic numbers, odd/even numbers, finding greatest of 3 numbers. 3. Finding greatest of 10 numbers entered by user, printing star pyramids, diamond pattern. 4. Generating and checking Armstrong numbers, Floyd's triangle, Pascal's triangle. 5. Implementing bubble sort, calculating HCF and LCM of two numbers.

Uploaded by

Kartik Shroff
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

1.

Multiplication Table
#include <stdio.h>
int main() {
int num, i = 1;
printf(" Enter any Number:");
scanf("%d", &num);
printf("Multiplication table of %d: ", num);
while (i <= 10) {
printf(" %d x %d = %d", num, i, num * i);
i++;
}
return 0;
}
2.Pyramid
# include<iostream.h>
# include <conio.h>
main()
{
int space=10; \\ to print the pyramid in center, you can also increase the # of
spaces
for (int i=0;i<=5;i++)
{
for (int k=0;k<space;k++)
{
cout<<" ";
}
for (int j=0;j<2*i-1;j++)
{
cout<<"*";
}
space--;
cout<<endl;
}
getch();
}
3.Swapping Two Numbers
#include <stdio.h>
int main() {
int a, b, temp;
printf("Enter the value of a and b: ");
scanf("%d %d", &a, &b);
printf("Before swapping a=%d, b=%d ", a, b);
/*Swapping logic */
temp = a;
a = b;
b = temp;
printf("After swapping a=%d, b=%d", a, b);
return 0;
}

4.Fibonnaci series
#include<stdio.h>
int main() {
//array fib stores numbers of fibonacci series
int i, fib[25];
//initialized first element to 0
fib[0] = 0;
//initialized second element to 1
fib[1] = 1;
//loop to generate ten elements
for (i = 2; i < 10; i++) {
//i'th element of series is equal to the sum of i-1'th element and i-2'th
element.
fib[i] = fib[i - 1] + fib[i - 2];
}
printf("The fibonacci series is as follows ");
//print all numbers in the series
for (i = 0; i < 10; i++) {
printf("%d ", fib[i]);
}
return 0; }
5.C Ask Number from 1 to 9
#include<stdio.h>
int getnumber();

int main() {
int input = 0; //call a function to input number from key board
input = getnumber();

//when input is not in the range of 1 to 9,print error message


while (!((input <= 9) && (input >= 1))) {
printf("[ERROR] The number you entered is out of range");
//input another number
input = getnumber();
}
//this function is repeated until a valid input is given by user.
printf(" The number you entered is %d", input);
return 0;
}

//this function returns the number given by user


int getnumber() {
int number;
//asks user for a input in given range
printf(" Enter a number between 1 to 9 ");
scanf("%d", &number);
return (number);
}
6.Checking Prime Numbers
#include <stdio.h>
main() {
int n, i, c = 0;
printf("Enter any number n: ");
scanf("%d", &n);
/*logic*/
for (i = 1; i <= n; i++) {
if (n % i == 0) {
c++;
}
}
if (c == 2) {
printf("n is a Prime number");
}
else {
printf("n is not a Prime number");
}
return 0;
}

7.Check weather given number is palindromic


#include <stdio.h>
int main() {
int n, n1, rev = 0, rem;
printf("Enter any number: ");
scanf("%d", &n);
n1 = n;
/* logic */
while (n > 0){
rem = n % 10;
rev = rev * 10 + rem;
n = n / 10; }
if (n1 == rev){
printf("Given number is a palindromic number"); }
else{
printf("Given number is not a palindromic number");
}
return 0; }
8.Checking weather given number is odd or even
#include <stdio.h>
int main() {
int a;
printf("Enter a: ");
scanf("%d", &a);
/* logic */
if (a % 2 == 0) {
printf("The given number is EVEN ");
}
else {
printf("The given number is ODD ");
}
return 0;
}

9.Finding greater number from given three number


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

if (a > b && a > c) {

printf("a is Greater than b and c");


}
else if (b > a && b > c) {
printf("b is Greater than a and c");
}
else if (c > a && c > b) {
printf("c is Greater than a and b");
}
else {
printf("all are equal or any two values are equal");
}
return 0; }
10.Find greatest Among ten number
#include <stdio.h>
int main() {
int a[10];
int i;
int greatest;
printf("Enter ten values:");
//Store 10 numbers in an array
for (i = 0; i < 10; i++) {
scanf("%d", &a[i]);
}
//Assume that a[0] is greatest
greatest = a[0];
for (i = 0; i < 10; i++) {
if (a[i] > greatest) {
greatest = a[i];
}
}
printf(" Greatest of ten numbers is %d", greatest);
return 0;
}

11.Staring pyramid in c
#include <stdio.h>

int main() {
int rows, star, spaces;
int number_of_stars = 6;
int number_of_rows = number_of_stars;

for (rows=1; rows <= number_of_rows; rows++) {


for (spaces=1; spaces <= number_of_stars; spaces++) {
printf(" ");
}
for (star=1; star <= rows; star++) {
printf("*");
printf(" ");
}
printf("\n");
number_of_stars = number_of_stars - 1;
}
return 0;
}
12.Diamond in C

#include <stdio.h>

int main()
{
int n, c, k, space = 1;

printf("Enter number of rows\n");


scanf("%d", &n);

space = n - 1;

for (k = 1; k <= n; k++)


{
for (c = 1; c <= space; c++)
printf(" ");

space--;

for (c = 1; c <= 2*k-1; c++)


printf("*");

printf("\n");
}

space = 1;

for (k = 1; k <= n - 1; k++)


{
for (c = 1; c <= space; c++)
printf(" ");

space++;

for (c = 1 ; c <= 2*(n-k)-1; c++)


printf("*");

printf("\n");
}

return 0;
}
13.Generate ArmStrong Number

#include<stdio.h>
#include<conio.h>

main()
{
int r;
long number = 0, c, sum = 0, temp;

printf("Enter the maximum range upto which you want to find armstrong numbers ");
scanf("%ld",&number);

printf("Following armstrong numbers are found from 1 to %ld\n",number);

for( c = 1 ; c <= number ; c++ )


{
temp = c;
while( temp != 0 )
{
r = temp%10;
sum = sum + r*r*r;
temp = temp/10;
}
if ( c == sum )
printf("%ld\n", c);
sum = 0;
}

getch();
return 0;
}
14.Find Armstrong Number
#include <stdio.h>

main()
{
int number, sum = 0, temp, remainder;

printf("Enter a number\n");
scanf("%d",&number);

temp = number;

while( temp != 0 )
{
remainder = temp%10;
sum = sum + remainder*remainder*remainder;
temp = temp/10;
}

if ( number == sum )
printf("Entered number is an armstrong number.");
else
printf("Entered number is not an armstrong number.");

return 0;
}

15.Floyed’s triangle
#include <stdio.h>

int main()
{
int n, i, c, a = 1;

printf("Enter the number of rows of Floyd's triangle to print\n");


scanf("%d", &n);

for (i = 1; i <= n; i++)


{
for (c = 1; c <= i; c++)
{
printf("%d ",a);
a++;
}
printf("\n");
}

return 0;
}
16.Pascal Triangle in C

#include<stdio.h>

long factorial(int);

main()
{
int i, n, c;

printf("Enter the number of rows you wish to see in pascal triangle\n");


scanf("%d",&n);

for ( i = 0 ; i < n ; i++ )


{
for ( c = 0 ; c <= ( n - i - 2 ) ; c++ )
printf(" ");

for( c = 0 ; c <= i ; c++ )


printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c)));

printf("\n");
}

return 0;
}

long factorial(int n)
{
int c;
long result = 1;

for( c = 1 ; c <= n ; c++ )


result = result*c;

return ( result );
}
17.Bubblesort in c

/* Bubble sort code */

#include <stdio.h>

int main()
{
int array[100], n, c, d, swap;

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++)


scanf("%d", &array[c]);

for (c = 0 ; c < ( n - 1 ); c++)


{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}

printf("Sorted list in ascending order:\n");

for ( c = 0 ; c < n ; c++ )


printf("%d\n", array[c]);

return 0;
}
18.HCF LCM in C

#include <stdio.h>

int main() {
int a, b, x, y, t, gcd, lcm;

printf("Enter two integers\n");


scanf("%d%d", &x, &y);

a = x;
b = y;

while (b != 0) {
t = b;
b = a % b;
a = t;
}

gcd = a;
lcm = (x*y)/gcd;

printf("Greatest common divisor of %d and %d = %d\n", x, y, gcd);


printf("Least common multiple of %d and %d = %d\n", x, y, lcm);

return 0;
}

19.Find String length

#include<stdio.h>
#include<string.h>

main()
{
char a[100];
int length;

printf("Enter a string to calculate it's length\n");


gets(a);

length = strlen(a);

printf("Length of entered string is = %d\n",length);

return 0;
}
20.String comparision using strcmp
#include<stdio.h>
#include<string.h>

main()
{
char a[100], b[100];

printf("Enter the first string\n");


gets(a);

printf("Enter the second string\n");


gets(b);

if( strcmp(a,b) == 0 )
printf("Entered strings are equal.\n");
else
printf("Entered strings are not equal.\n");

return 0;
}
21.String comparision without strcmp
int compare(char a[], char b[])
{
int c = 0;

while( a[c] == b[c] )


{
if( a[c] == '\0' || b[c] == '\0' )
break;
c++;
}
if( a[c] == '\0' && b[c] == '\0' )
return 0;
else
return -1;
}
22.String concatenate
#include<stdio.h>
#include<conio.h>
#include<string.h>

main()
{
char a[100], b[100];

printf("Enter the first string\n");


gets(a);

printf("Enter the second string\n");


gets(b);

strcat(a,b);

printf("String obtained on concatenation is %s\n",a);

getch();
return 0;
}
23.Programme to check leap year

#include <stdio.h>

int main()
{
int year;

printf("Enter a year to check if it is a leap year\n");


scanf("%d", &year);

if ( year%400 == 0)
printf("%d is a leap year.\n", year);
else if ( year%100 == 0)
printf("%d is not a leap year.\n", year);
else if ( year%4 == 0 )
printf("%d is a leap year.\n", year);
else
printf("%d is not a leap year.\n", year);

return 0;
}
24.Check weather input alphabet is vowel or not
#include <stdio.h>

main()
{
char ch;

printf("Enter a character\n");
scanf("%c", &ch);

if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch =='o' ||


ch=='O' || ch == 'u' || ch == 'U')
printf("%c is a vowel.\n", ch);
else
printf("%c is not a vowel.\n", ch);

return 0;
}

25.Insertion sort in c
* insertion sort ascending order */

#include <stdio.h>

int main()
{
int n, array[1000], c, d, t;

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++) {


scanf("%d", &array[c]);
}

for (c = 1 ; c <= n - 1; c++) {


d = c;

while ( d > 0 && array[d] < array[d-1]) {


t = array[d];
array[d] = array[d-1];
array[d-1] = t;

d--;
}
}

printf("Sorted list in ascending order:\n");

for (c = 0; c <= n - 1; c++) {


printf("%d\n", array[c]);
}

return 0; }
26.Selection sort in c
#include<stdio.h>

main()
{
int array[100], n, c, d, position, swap;

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);

for ( c = 0 ; c < n ; c++ )


scanf("%d", &array[c]);

for ( c = 0 ; c < ( n - 1 ) ; c++ )


{
position = c;

for ( d = c + 1 ; d < n ; d++ )


{
if ( array[position] > array[d] )
position = d;
}
if ( position != c )
{
swap = array[c];
array[c] = array[position];
array[position] = swap;
}
}

printf("Sorted list in ascending order:\n");

for ( c = 0 ; c < n ; c++ )


printf("%d\n", array[c]);

return 0;
}
27.Sort string in C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void sort_string(char*);

main()
{
char string[100];

printf("Enter some text\n");


gets(string);

sort_string(string);
printf("%s\n", string);

return 0;
}

void sort_string(char *s)


{
int c, d = 0, length;
char *pointer, *result, ch;

length = strlen(s);

result = (char*)malloc(length+1);

pointer = s;

for ( ch = 'a' ; ch <= 'z' ; ch++ )


{
for ( c = 0 ; c < length ; c++ )
{
if ( *pointer == ch )
{
*(result+d) = *pointer;
d++;
}
pointer++;
}
pointer = s;
}
*(result+d) = '\0';

strcpy(s, result);
free(result);
}
28.strlwr in c
#include<stdio.h>
#include<string.h>

main()
{
char string[] = "Strlwr in C";

printf("%s\n",strlwr(string));

return 0;
}

29.Strupr in c

#include<stdio.h>
#include<string.h>

main()
{
char string[] = "strupr in c";

printf("%s\n",strupr(string));

return 0;
}

30.To Print Date

#include<stdio.h>
#include<conio.h>
#include<dos.h>

main()
{
struct date d;

getdate(&d);

printf("Current system date is %d/%d/%d",d.da_day,d.da_mon,d.da_year);


getch();
return 0;
}
31.Substring in C
#include <stdio.h>
#include <malloc.h>

char* substring(char*, int, int);

main()
{
char string[100], *pointer;
int position, length;

printf("Enter a string\n");
gets(string);

printf("Enter the position and length of substring\n");


scanf("%d%d",&position, &length);

pointer = substring( string, position, length);

printf("Required substring is \"%s\"\n", pointer);

free(pointer);

return 0;
}

/*C substring function: It returns a pointer to the substring */

char *substring(char *string, int position, int length)


{
char *pointer;
int c;

pointer = malloc(length+1);

if (pointer == NULL)
{
printf("Unable to allocate memory.\n");
exit(EXIT_FAILURE);
}

for (c = 0 ; c < position -1 ; c++)


string++;

for (c = 0 ; c < length ; c++)


{
*(pointer+c) = *string;
string++;
}

*(pointer+c) = '\0';

return pointer;
}
32.Swap two String

#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<conio.h>

main()
{
char first[100], second[100], *temp;

printf("Enter the first string ");


gets(first);

printf("Enter the second string ");


gets(second);

printf("\nBefore Swapping\n");
printf("First string: %s\n",first);
printf("Second string: %s\n\n",second);

temp = (char*)malloc(100);

strcpy(temp,first);
strcpy(first,second);
strcpy(second,temp);

printf("After Swapping\n");
printf("First string: %s\n",first);
printf("Second string: %s\n",second);

getch();
return 0;
}

33.Reverse String

#include<stdio.h>
#include<string.h>

main()
{
char arr[100];

printf("Enter a string to reverse\n");


gets(arr);

strrev(arr);

printf("Reverse of entered string is \n%s\n",arr);

return 0;
}
34.Shutdown pc in widows XP

#include <stdio.h>
#include <stdlib.h>

main()
{
char ch;

printf("Do you want to shutdown your computer now (y/n)\n");


scanf("%c",&ch);

if (ch == 'y' || ch == 'Y')


system("C:\\WINDOWS\\System32\\shutdown -s");

return 0;
}

35.Shutdown pc in widows7

#include <stdio.h>
#include <stdlib.h>

main()
{
char ch;

printf("Do you want to shutdown your computer now (y/n)\n");


scanf("%c",&ch);

if (ch == 'y' || ch == 'Y')


system("C:\\WINDOWS\\System32\\shutdown /s");

return 0;
}

36.Shutdown pc ubentu linux

#include <stdio.h>

int main() {
system("shutdown -P now");
return 0;
}
37.The remainder (%) operator
#include <stdio.h>

int
main (void)
{
int a, b, c, d;

/* a few operations */
a = 10 % 3;
b = -10 % 3;
c = 10 % -3;
d = -10 % -3;

/* you need to double the % to display on screen */


printf ("10 %% 3 is %d\n", a);
printf ("-10 %% 3 is %d\n", b);
printf ("10 %% -3 is %d\n", c);
printf ("-10 %% -3 is %d\n", d);

return (0);
}

38. Integer division


#include <stdio.h>

int
main (void)
{
int a, b, c;
double x, y, z, w;

a = 10; b = 20;

/* dividing two integers */


z = a / b;
c = a / b;
printf ("The value of z is %5.3lf ", z);
printf ("and the value of c is %d\n", c);

/* converting (casting) one operand before the division*/


x = (double)a / b;
printf ("The value of x is %5.3lf\n", x);

/* casting the quotient after the division*/


y = (double) (a / b);
printf ("The value of y is %5.3lf\n", y);

/* casting both operands before the division*/


w = (double)a / (double)b;
printf ("The value of w is %5.3lf\n", w);

return (0); }
39. Functions from the math library
#include <stdio.h>
#include <math.h>
#define PI 3.1416

int
main (void)
{
double before, after, x, y, z;

/* the ceil function */


before = -217.5;
after = ceil (before);
printf ("The ceiling of %3.1lf is %3.1lf\n", before, after);

/* the floor function */


before = -217.5;
after = floor (before);
printf ("The floor of %3.1lf is %3.1lf\n", before, after);

/* the log function */


before = 200.0;
after = log (before);
printf ("The ln of %3.1lf is %3.1lf\n", before, after);

/* the log10 function*/


before = 200.0;
after = log10 (before);
printf ("The log of %3.1lf is %3.1lf\n", before, after);

/* the sqrt function*/


before = 200.0;
after = sqrt (before);
printf ("The square root of %3.1lf is %3.1lf\n", before, after);

/* the fabs function (for doubles)*/


before = -413.56;
after = fabs (before);
printf ("The absolute value of %3.1lf is %3.1lf\n", before, after);

/* the sin function */


before = 45.0;
after = sin (before * PI / 180);
printf ("The sine of %3.1lf is %5.3lf\n", before, after);

/* the cos function */


before = 45.0;
after = cos (before * PI / 180);
printf ("The cosine of %3.1lf is %5.3lf\n", before, after);
/* the tan function */
before = 45.0;
after = tan (before * PI / 180);
printf ("The tangent of %3.1lf is %5.3lf\n", before, after);

/* the exp function */


before = 10.0;
after = exp (before);
printf ("e to the power of %3.1lf is %5.3lf\n", before, after);

/* the pow function */


x = 9.0; y = 3.0;
z = pow (x, y);
printf ("%3.1lf to the power of %3.1lf is %3.1lf\n", x, y, z);

return (0);
}

40. getchar() and putchar()

#include <stdio.h>

int
main (void)
{
char c;

printf ("Enter a character: ");


/* getchar() gets a single character from the keyboard */
c = getchar();

printf ("The character is: ");


/* putchar() prints a single character on the screen */
putchar (c);

return (0);
}
41. The (-)unary operator

#include <stdio.h>

int
main (void)
{
int a, b, c;

a = 10; b = 20;

/* b is 20 so -b is -20 */
b = -b + a;

/* with multiple similar unary operators, use parentheses */


/* do you understand why c is 30? */
c = -b - (-a) + -b;
printf ("The value of b is %d ", b);
printf ("and the value of c is %d\n", c);

return (0);
}
42. Increment (++) and decrement (--) */

#include <stdio.h>

int
main (void)
{
int a, b;

a = 5;

/* increment (++) */
/* a is incremented by 1 */
++a;
printf ("After ++a, a is now %d\n", a);

/* a is once more incremented by 1 */


a++;
printf ("After a++, a is now %d\n", a);

/* a is incremented but b gets the current a */


b = a++;
printf ("After b=a++, a is now %d and b is %d\n", a, b);

/* a is incremented and b gets the incremented a */


b = ++a;
printf ("After b=++a, a is now %d and b is %d\n", a, b);

/* decrement (--) */
/* a is decremented by 1 */
--a;
printf ("After --a, a is now %d\n", a);

/* a is once more decremented by 1 */


a--;
printf ("After a--, a is now %d\n", a);

/* a is decremented but b gets the current a */


b = a--;
printf ("After b=a--, a is now %d and b is %d\n", a, b);

/* a is decremented and b gets the decremented a */


b = --a;
printf ("After b=++a, a is now %d and b is %d\n", a, b);

return (0);
}
43. The random [rand()] function */
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int
main (void)
{
/* initialize random generator */
srand (time(NULL));

/* generate random numbers */


printf("RAND_MAX = %d\n", RAND_MAX);
printf ("A number between 0 and RAND_MAX : %d\n", rand());
printf ("A number between 0 and 99: %d\n", rand() % 100);
printf ("A number between 0 and 9: %d\n", rand() % 10);
printf ("A number between 1 and 6: %d\n", (rand() % 6) + 1);

return (0);
}

44. demonstrates how to assign values to pointer


variables.
#include <stdio.h>

int
main (void)
{
/* c and d are pointers to integers */
int a, b, *c, *d, e;

a = 10;
b = a * 3;
c = &a; /* address of a goes into c */
d = &b; /* address of b goes into d */
e = *c + *d; /* *c is a and *d is b */
*d = a;
d = &a;
*c = *d - a % b + *c;

printf ("Do you understand why\n");


printf ("a= %d, b= %d, e= %d ?\n", a, b, e);

return (0);
}
45.just a silly program playing with pointers */
#include <stdio.h>

int
main (void)
{
/* a and e are integers */
int a, e;

/* b is a pointer to an integer */
int* b;

/* c is a pointer to a pointer to an integer */


int** c;

/* d is a pointer to a pointer to a pointer to an integer */


int*** d;

a = 25; /* a contains the integer 25 */


b = &a; /* b contains the address of a */
c = &b; /* c contains the address of b */
d = &c; /* d contains the address of c */

/* Do you understand that ***d is actually a? */


e = ***d * 2;

printf ("%d", e);

return (0);
}

46. Simple countdown using a for loop */


#include <stdio.h>

int
main (void)
{
int time;

/* the loop */
for (time=60; time>=0; time=time-1)
{
printf ("%d ", time);
}

printf ("\n\nLIFTOFF!\n\n");

return(0);
}
47. A user-defined function without argument nor
result */

#include <stdio.h>

/* the function definition */


void
stars (void)
{
printf ("********************************\n");
}
/* end of function definition */

/* main program */
int
main (void)
{
printf ("The line of stars comes from a function.\n");

/* calling the function */


stars ();

/* the function can be called as will*/


printf ("\n");
stars ();
printf ("Hello world!\n");
stars ();

return (0);
}
48. A void function with one argument */

#include <stdio.h>

/* a void function returns nothing */


void
stars2 (int n)
{
int i;

/* a loop displaying a star at


each iteration */
for (i=1; i<=n; ++i)
{
printf ("*");
}
/* change line after each series */
printf ("\n");
}

int
main (void)
{
int a;

a=10;

/* the argument may be a constant,


a variable or an expression */

stars2 (20);
stars2 (a);
stars2 (a+2);

return (0);
}
49. The factorial function: one argument, one result
#include <stdio.h>

/* this function will return an integer */


int
factorial (int n)
{
int i, product;

product = 1; /* initialization */

/* computes n*n-1... */
for (i=n; i>1; i=i-1)
{
product = product * i;
}

/* the value that goes out */


return (product);
}

int
main (void)
{
int a, result;

printf ("Enter an integer number: ");


scanf ("%d", &a);

/* calling the function */


result = factorial (a);

/* printing the report */


printf ("The factorial of %d is %d.\n", a, result);

return (0);
}
50. Problem: Write a program that prints out all the
prime numbers between 1 and 1000. */

#include <stdio.h>
#include <math.h>

/* function returns true if n is even. */


/* it returns false if n is odd */
int
even (int n)
{
return (!(n%2));
}

/* function returns true if n is prime */


int
prime3 (int n)
{

int divisor, i;
/* looking for a divisor. if found, it
is not a prime number */

divisor = 0;

/* eliminating even numbers except 2 */


if (even (n))
{
if (n==2)
divisor=0;
else
divisor=1;
}
else
{
if (n==1)
divisor=0; /* 1 is a prime number */
else
/* trying to divide number by 3,5,7,... */
/* to find a divisor until sqrt(n) */
for (i=3; i<=sqrt(n); i=i+2)
{
if (!(n%i))
divisor=i;
}
}

/* if there is a divisor then NOT prime */


return (!divisor);

/* This main program prints all prime numbers


between 1 and 1000 */

int
main (void)
{
int x;

for (x=1; x<=1000; ++x)


if (prime3 (x))
printf (" %d ", x);

return (0);
}
51. Function finding prime numbers (no pointers)
#include <stdio.h>
#include <math.h>

/* function returns true if n is even. */


/* it returns false if n is odd */
int
even (int n)
{
return (n%2==0);
}

/* function returns true if n is prime */


int
prime (int n)
{
int divisor, i;

/* looking for a divisor. if found, it


is not a prime number */

divisor = 0;

/* eliminating even numbers except 2 */


if (even (n))
{
if (n == 2)
divisor = 0;
else
divisor = 2;
}
else
{
if (n == 1)
divisor = 0; /* 1 is a prime number */
else
/* trying to divide number by 3,5,7,...
*/
/* to find a divisor until we reach n-1
*/
for (i = 3; i < n; i = i + 2)
{
if (n % i == 0)
divisor = i;
}
}

/* if there is a divisor (not zero) then NOT prime


*/
return (!divisor);
}

int
main (void)
{
int x;

printf ("Enter a positive integer number: ");


scanf ("%d", &x);

/* testing for prime and printing the report */


if (prime (x))
printf ("%d is a prime number.\n", x);
else
printf ("%d is not a prime number.\n", x);

return (0);
}

52. Reading a string with gets */


#include <stdio.h>
#include <string.h>

int
main (void)
{
char city[20], city2[20];

printf ("What is the capital of Canada? ");


/* the string is read with gets */
gets (city);

printf ("What is the capital of Argentina? ");


gets (city2);

/* here is the report */


printf ("\nThe capital of Canada is: %s.", city);
printf ("\nThe capital of Argentina is: %s.", city2);

return (0);
}

You might also like