0% found this document useful (0 votes)
12 views8 pages

C Programming Basics: 20 Examples

The document contains a list of 20 programming exercises in C, covering basic concepts such as printing messages, arithmetic operations, control structures, and data structures. Each program includes an aim, code snippet, and expected output. The exercises range from simple tasks like printing 'Hello World' to more complex ones like calculating factorials and checking for prime numbers.

Uploaded by

teamshadow4424
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)
12 views8 pages

C Programming Basics: 20 Examples

The document contains a list of 20 programming exercises in C, covering basic concepts such as printing messages, arithmetic operations, control structures, and data structures. Each program includes an aim, code snippet, and expected output. The exercises range from simple tasks like printing 'Hello World' to more complex ones like calculating factorials and checking for prime numbers.

Uploaded by

teamshadow4424
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

INDEX

1. Program 1 – Hello World Program


2. Program 2 – Addition of Two Numbers
3. Program 3 – Swap Two Numbers
4. Program 4 – Find Area of Circle
5. Program 5 – Check Even or Odd
6. Program 6 – Largest of Two Numbers
7. Program 7 – Largest of Three Numbers
8. Program 8 – Factorial of a Number
9. Program 9 – Fibonacci Series
10. Program 10 – Sum of Digits
11. Program 11 – Reverse a Number
12. Program 12 – Palindrome Number
13. Program 13 – Simple Calculator
14. Program 14 – Check Prime Number
15. Program 15 – Print Multiplication Table
16. Program 16 – Count Positive, Negative & Zero
17. Program 17 – Find Armstrong Number
18. Program 18 – Print Patterns (Star Pattern)
19. Program 19 – Array: Largest Element
20. Program 20 – Structure Student Details
Program 1 – Hello World
Aim: To print "Hello World".
Code:
#include <stdio.h>
int main() {
printf("Hello World");
return 0;
}
Output: Hello World

Program 2 – Addition of Two Numbers


Aim: To add two numbers.
Code:
#include <stdio.h>
int main() {
int a = 5, b = 10, sum;
sum = a + b;
printf("Sum = %d", sum);
return 0;
}
Output: Sum = 15

Program 3 – Swap Two Numbers


Aim: To swap two numbers using a temporary variable.
Code:
#include <stdio.h>
int main() {
int a=10, b=20, temp;
temp=a;
a=b;
b=temp;
printf("After Swap: a=%d b=%d", a, b);
return 0;
}
Output: After Swap: a=20 b=10

Program 4 – Area of Circle


Aim: To find area of circle.
Code:
#include <stdio.h>
int main() {
float r=5, area;
area = 3.14 * r * r;
printf("Area = %.2f", area);
return 0;
}
Output: Area = 78.50

Program 5 – Check Even or Odd


Aim: To check if number is even or odd.
Code:
#include <stdio.h>
int main() {
int n=7;
if(n % 2 == 0)
printf("Even");
else
printf("Odd");
return 0;
}
Output: Odd

Program 6 – Largest of Two Numbers


Aim: To find largest of two numbers.
#include <stdio.h>
int main() {
int a=10, b=20;
if(a>b)
printf("Largest = %d", a);
else
printf("Largest = %d", b);
return 0;
}
Output: Largest = 20

Program 7 – Largest of Three Numbers


#include <stdio.h>
int main() {
int a=10,b=20,c=15;
if(a>b && a>c)
printf("Largest = %d", a);
else if(b>c)
printf("Largest = %d", b);
else
printf("Largest = %d", c);
return 0;
}

Program 8 – Factorial of a Number


#include <stdio.h>
int main() {
int n=5, fact=1;
for(int i=1;i<=n;i++)
fact *= i;
printf("Factorial = %d", fact);
return 0;
}
Program 9 – Fibonacci Series
#include <stdio.h>
int main(){
int a=0,b=1,n=10,c;
printf("%d %d",a,b);
for(int i=2;i<n;i++){
c=a+b;
printf(" %d",c);
a=b;
b=c;
}
return 0;
}

Program 10 – Sum of Digits


#include <stdio.h>
int main(){
int n=123,sum=0;
while(n>0){ sum+=n%10; n/=10; }
printf("Sum = %d", sum);
return 0;
}

Program 11 – Reverse Number


#include <stdio.h>
int main(){
int n=123,rev=0;
while(n>0){ rev=rev*10+n%10; n/=10; }
printf("Reverse = %d",rev);
return 0;
}
Program 12 – Palindrome Number
#include <stdio.h>
int main(){
int n=121,rev=0,temp=n;
while(temp>0){ rev=rev*10+temp%10; temp/=10; }
if(rev==n) printf("Palindrome");
else printf("Not Palindrome");
return 0;
}

Program 13 – Simple Calculator


#include <stdio.h>
int main(){
int a=10,b=5;
printf("Add = %d", a+b);
printf("\nSub = %d", a-b);
printf("\nMul = %d", a*b);
printf("\nDiv = %d", a/b);
return 0;
}

Program 14 – Prime Number

#include <stdio.h>
int main(){
int n=7,flag=0;
for(int i=2;i<=n/2;i++)
if(n%i==0) flag=1;
if(flag==0) printf("Prime");
else printf("Not Prime");
return 0;
}
Program 15 – Multiplication Table
#include <stdio.h>
int main(){
int n=5;
for(int i=1;i<=10;i++)
printf("%d x %d = %d\n",n,i,n*i);
return 0;
}

Program 16 – Count Positive, Negative & Zero


#include <stdio.h>
int main(){
int a[5]={-1,0,5,8,-3};
int p=0,n=0,z=0;
for(int i=0;i<5;i++){
if(a[i]>0) p++;
else if(a[i]<0) n++;
else z++;
}
printf("Positive=%d Negative=%d Zero=%d",p,n,z);
return 0;
}

Program 17 – Armstrong Number


#include <stdio.h>
int main(){
int n=153,sum=0,temp=n;
while(temp>0){ int d=temp%10; sum+=d*d*d; temp/=10; }
if(sum==n) printf("Armstrong");
else printf("Not Armstrong");
return 0;
}
Program 18 – Star Pattern
#include <stdio.h>
int main(){
for(int i=1;i<=5;i++){
for(int j=1;j<=i;j++) printf("*");
printf("\n");
}
return 0;
}

Program 19 – Largest Element in Array


#include <stdio.h>
int main(){
int a[5]={10,50,30,70,40},max=a[0];
for(int i=1;i<5;i++)
if(a[i]>max) max=a[i];
printf("Largest = %d", max);
return 0;
}

Program 20 – Structure: Student Details

#include <stdio.h>
struct student { char name[20]; int roll; float marks; };
int main(){
struct student s={"Piyush", 1, 89.5};
printf("Name: %s\nRoll: %d\nMarks: %.2f",[Link],[Link],[Link]);
return 0;
}

You might also like