0% found this document useful (0 votes)
88 views9 pages

C Programming Examples For Beginners With Solutions & Output

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)
88 views9 pages

C Programming Examples For Beginners With Solutions & Output

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/ 9

C Programming Examples For Beginners With Solutions & Output https://pwskills.

com/blog/c-programming-examples/

C Programming Examples for Beginners


Below, we will share C programming examples for beginners. You can also download C
language programs PDF.

1. C Hello World Program


#include <stdio.h>

int main() {

printf(“Hello, World!\n”);

return 0;

2. C Program to Print Your Own Name


#include <stdio.h>

int main() {

printf(“Your Name\n”);

return 0;

3. C Program to Print an Integer Entered By the


User
#include <stdio.h>

int main() {

int num;

1 of 9 12-12-2024, 19:00
C Programming Examples For Beginners With Solutions & Output https://pwskills.com/blog/c-programming-examples/

printf(“Enter an integer: “);

scanf(“%d”, &num);

printf(“You entered: %d\n”, num);

return 0;

4. C Program to Check Whether a Number is Prime


or Not
#include <stdio.h>

int main() {

int num, i, �ag = 0;

printf(“Enter a number: “);

scanf(“%d”, &num);

for (i = 2; i <= num / 2; ++i) {

if (num % i == 0) {

�ag = 1;

break;

if (�ag == 0)

printf(“%d is a prime number.\n”, num);

else

2 of 9 12-12-2024, 19:00
C Programming Examples For Beginners With Solutions & Output https://pwskills.com/blog/c-programming-examples/

printf(“%d is not a prime number.\n”, num);

return 0;

Also read: Top 30 Most Asked Basic Programming Questions Asked During Interviews

C Programming Examples With Solutions


Here are some C programming examples with solutions and codes:

1. C Program to Multiply two Floating-Point


Numbers
#include <stdio.h>

int main() {

�oat num1, num2, product;

printf(“Enter two �oating-point numbers: “);

scanf(“%f %f”, &num1, &num2);

product = num1 * num2;

printf(“Product: %f\n”, product);

return 0;

2. C Program to Print the ASCII Value of a Character


#include <stdio.h>

int main() {

char ch;

3 of 9 12-12-2024, 19:00
C Programming Examples For Beginners With Solutions & Output https://pwskills.com/blog/c-programming-examples/

printf(“Enter a character: “);

scanf(“%c”, &ch);

printf(“ASCII value of %c = %d\n”, ch, ch);

return 0;

3. C Program to Swap Two Numbers


#include <stdio.h>

int main() {

int num1, num2, temp;

printf(“Enter two numbers: “);

scanf(“%d %d”, &num1, &num2);

temp = num1;

num1 = num2;

num2 = temp;

printf(“After swapping: num1 = %d, num2 = %d\n”, num1, num2);

return 0;

4. C Program to Calculate Fahrenheit to Celsius


#include <stdio.h>

int main() {

�oat fahrenheit, celsius;

4 of 9 12-12-2024, 19:00
C Programming Examples For Beginners With Solutions & Output https://pwskills.com/blog/c-programming-examples/

printf(“Enter temperature in Fahrenheit: “);

scanf(“%f”, &fahrenheit);

celsius = (fahrenheit – 32) * 5 / 9;

printf(“Temperature in Celsius: %f\n”, celsius);

return 0;

5. C Program to Find the Size of int, �oat, double,


and char
#include <stdio.h>

int main() {

printf(“Size of int: %d bytes\n”, sizeof(int));

printf(“Size of �oat: %d bytes\n”, sizeof(�oat));

printf(“Size of double: %d bytes\n”, sizeof(double));

printf(“Size of char: %d byte\n”, sizeof(char));

return 0;

6. C Program to Print Prime Numbers From 1 to N


#include <stdio.h>

int main() {

int i, j, n;

printf(“Enter a number (N): “);

5 of 9 12-12-2024, 19:00
C Programming Examples For Beginners With Solutions & Output https://pwskills.com/blog/c-programming-examples/

scanf(“%d”, &n);

printf(“Prime numbers between 1 and %d are: “, n);

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

int isPrime = 1;

for (j = 2; j <= i / 2; ++j) {

if (i % j == 0) {

isPrime = 0;

break;

if (isPrime)

printf(“%d “, i);

return 0;

Also read: 10 Best Programming Languages for Game Development in 2024

C Programming Examples With Output


Learners can also download c programming examples with output PDF to start their C
language journey. But practice is the key to learning C language properly. Here are some of the
best C programming examples with output:

1. C Program to Check Whether a Number is


Positive, Negative, or Zero

6 of 9 12-12-2024, 19:00
C Programming Examples For Beginners With Solutions & Output https://pwskills.com/blog/c-programming-examples/

#include <stdio.h>

int main() {

int num;

printf(“Enter a number: “);

scanf(“%d”, &num);

if (num > 0)

printf(“Positive number\n”);

else if (num < 0)

printf(“Negative number\n”);

else

printf(“Zero\n”);

return 0;

Output:

Enter a number: 7

Positive number

2. C Program to Check Whether Number is Even or


Odd
#include <stdio.h>

int main() {

int num;

7 of 9 12-12-2024, 19:00
C Programming Examples For Beginners With Solutions & Output https://pwskills.com/blog/c-programming-examples/

printf(“Enter a number: “);

scanf(“%d”, &num);

if (num % 2 == 0)

printf(“Even number\n”);

else

printf(“Odd number\n”);

return 0;

Output:

Enter a number: 15

Odd number

3. C Program to Calculate Sum of Natural Numbers


#include <stdio.h>

int main() {

int n, sum = 0;

printf(“Enter a positive integer: “);

scanf(“%d”, &n);

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

sum += i;

printf(“Sum of natural numbers from 1 to %d: %d\n”, n, sum);

8 of 9 12-12-2024, 19:00
C Programming Examples For Beginners With Solutions & Output https://pwskills.com/blog/c-programming-examples/

return 0;

Output:

Enter a positive integer: 5

Sum of natural numbers from 1 to 5: 15

4. C Program to Print Alphabets From A to Z Using


Loop
#include <stdio.h>

int main() {

char ch;

printf(“Alphabets from A to Z:\n”);

for (ch = ‘A’; ch <= ‘Z’; ++ch) {

printf(“%c “, ch);

return 0;

Output:

Alphabets from A to Z:

ABCDEFGHIJKLMNOPQRSTUVWXYZ

9 of 9 12-12-2024, 19:00

You might also like