Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
3 views2 pages

C Language Practice Programs Anas

The document contains four C programming examples: checking for prime numbers, calculating the factorial of a number, implementing a simple calculator using switch statements, and printing a star pattern in the shape of a pyramid. Each example includes user input and relevant calculations or outputs. These programs demonstrate basic programming concepts such as loops, conditionals, and user interaction.

Uploaded by

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

C Language Practice Programs Anas

The document contains four C programming examples: checking for prime numbers, calculating the factorial of a number, implementing a simple calculator using switch statements, and printing a star pattern in the shape of a pyramid. Each example includes user input and relevant calculations or outputs. These programs demonstrate basic programming concepts such as loops, conditionals, and user interaction.

Uploaded by

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

1.

Check Prime Number

#include <stdio.h>

int main() {
int num, i, isPrime = 1;
printf("Enter a number: ");
scanf("%d", &num);

if (num <= 1) isPrime = 0;


else {
for (i = 2; i <= num / 2; ++i) {
if (num % i == 0) {
isPrime = 0;
break;
}
}
}

if (isPrime)
printf("%d is a Prime number.\n", num);
else
printf("%d is not a Prime number.\n", num);

return 0;
}

2. Factorial of a Number

#include <stdio.h>

int main() {
int n, i;
long long fact = 1;
printf("Enter a number: ");
scanf("%d", &n);

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


fact *= i;
}

printf("Factorial of %d = %lld\n", n, fact);


return 0;
}

3. Simple Calculator using Switch

#include <stdio.h>

int main() {
char op;
float num1, num2;
printf("Enter operator (+, -, *, /): ");
scanf(" %c", &op);
printf("Enter two operands: ");
scanf("%f %f", &num1, &num2);

switch(op) {
case '+':
printf("%.2f + %.2f = %.2f\n", num1, num2, num1 + num2);
break;
case '-':
printf("%.2f - %.2f = %.2f\n", num1, num2, num1 - num2);
break;
case '*':
printf("%.2f * %.2f = %.2f\n", num1, num2, num1 * num2);
break;
case '/':
if(num2 != 0)
printf("%.2f / %.2f = %.2f\n", num1, num2, num1 / num2);
else
printf("Cannot divide by zero.\n");
break;
default:
printf("Invalid operator\n");
}
return 0;
}

4. Print a Star Pattern (Pyramid)

#include <stdio.h>

int main() {
int i, j, space, rows;
printf("Enter number of rows: ");
scanf("%d", &rows);

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


for(space = 1; space <= rows - i; space++)
printf(" ");
for(j = 1; j <= 2 * i - 1; j++)
printf("*");
printf("\n");
}
return 0;
}

You might also like