0% found this document useful (0 votes)
8 views18 pages

C Programming Examples for Beginners

The document contains a series of C programming exercises that cover various topics including displaying hardware components, converting distances, calculating currency notes, determining height categories, generating Pascal's triangle, finding factors, searching arrays, summing elements, performing matrix multiplication, demonstrating string functions, reversing strings, generating Fibonacci series, using mathematical functions, swapping numbers, printing array elements with pointers, managing student information with structures, and reading file contents.

Uploaded by

sagarking049
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)
8 views18 pages

C Programming Examples for Beginners

The document contains a series of C programming exercises that cover various topics including displaying hardware components, converting distances, calculating currency notes, determining height categories, generating Pascal's triangle, finding factors, searching arrays, summing elements, performing matrix multiplication, demonstrating string functions, reversing strings, generating Fibonacci series, using mathematical functions, swapping numbers, printing array elements with pointers, managing student information with structures, and reading file contents.

Uploaded by

sagarking049
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

1.

Write a C program to display different


hardware components of a computer.

#include <stdio.h>

int main() {
printf("Computer Hardware Components:\n");
printf("1. CPU\n");
printf("2. RAM\n");
printf("3. Hard Disk\n");
printf("4. Motherboard\n");
printf("5. Power Supply\n");
printf("6. Keyboard\n");
printf("7. Mouse\n");
return 0;
}
2. Write a program that takes the distance
between two cities (in kilometers) as input and
converts it into meters, feet, inches, and
centimeters.
#include <stdio.h>

int main() {
float km;
printf("Enter distance in km: ");
scanf("%f", &km);

printf("Meters = %f\n", km * 1000);


printf("Feet = %f\n", km * 3280.84);
printf("Inches = %f\n", km * 39370.1);
printf("Centimeters = %f\n", km * 100000);
return 0;
}
3. Given an amount N, write a program to find
the minimum number of currency notes
required. Available denominations:
100, 50, 10, 5, 2, 1

#include <stdio.h>

int main() {
int N;
printf("Enter amount: ");
scanf("%d", &N);

int notes[] = {100, 50, 10, 5, 2, 1};

for(int i=0; i<6; i++) {


int count = N / notes[i];
if(count > 0) {
printf("%d x %d\n", notes[i], count);
}
N %= notes[i];
}
return 0;
}
4. Write a program to read the height of a person in feet and then
print the person’s category according to the following conditions:
Height (in feet) Category
Less than 2.5 Short
2.5 to 4.0 Normal
4.0 to 5.5 Tall

#include <stdio.h>

int main() {
float h;
printf("Enter height in feet: ");
scanf("%f", &h);

if(h < 2.5)


printf("Short");
else if(h <= 4)
printf("Normal");
else if(h <= 5.5)
printf("Tall");
return 0;
}
[Link] a program to display the Pascal triangle.

#include <stdio.h>

int main() {
int n = 5;

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


int val = 1;
for(int j=0; j<=i; j++) {
printf("%d ", val);
val = val * (i - j) / (j + 1);
}
printf("\n");
}
return 0;
}
6. Write a program to find and print all factors of
a given integer.

#include <stdio.h>

int main() {
int n;
printf("Enter number: ");
scanf("%d", &n);

printf("Factors: ");
for(int i=1; i<=n; i++) {
if(n % i == 0)
printf("%d ", i);
}
return 0;
}
7. Write a program to search an element in an array using
linear search. The program must be written using a
function that returns the index of the element.
#include <stdio.h>

int search(int A[], int n, int key) {


for(int i=0; i<n; i++) {
if(A[i] == key)
return i;
}
return -1;
}

int main() {
int A[] = {1,5,6,8,3,2,1,0};
int key;
printf("Enter number to search: ");
scanf("%d", &key);

int index = search(A, 8, key);


printf("Index = %d", index);
return 0;
}
8. Write a program to read an array of N integers
and calculate the sum of all elements.

#include <stdio.h>

int main() {
int n, sum=0;
printf("Enter size: ");
scanf("%d", &n);

int a[n];
for(int i=0; i<n; i++) {
scanf("%d", &a[i]);
sum += a[i];
}

printf("Sum = %d", sum);


return 0;
}
9. Write a program to perform matrix
multiplication.

#include <stdio.h>

int main() {
int a[10][10], b[10][10], c[10][10];
int r1, c1, r2, c2;

printf("Enter rows & cols of matrix 1: ");


scanf("%d %d", &r1, &c1);
printf("Enter rows & cols of matrix 2: ");
scanf("%d %d", &r2, &c2);

if(c1 != r2) {
printf("Multiplication not possible!");
return 0;
}

printf("Enter matrix 1:\n");


for(int i=0;i<r1;i++)
for(int j=0;j<c1;j++)
scanf("%d",&a[i][j]);

printf("Enter matrix 2:\n");


for(int i=0;i<r2;i++)
for(int j=0;j<c2;j++)
scanf("%d",&b[i][j]);
for(int i=0;i<r1;i++)
for(int j=0;j<c2;j++) {
c[i][j]=0;
for(int k=0;k<c1;k++)
c[i][j]+=a[i][k]*b[k][j];
}

printf("Result:\n");
for(int i=0;i<r1;i++){
for(int j=0;j<c2;j++)
printf("%d ",c[i][j]);
printf("\n");
}
return 0;
}
[Link] a program to demonstrate any six
string functions.

#include <stdio.h>
#include <string.h>
int main() {
char s1[50] = "Hello";
char s2[50];
printf("Length: %lu\n", strlen(s1));

strcpy(s2, s1);
printf("Copy: %s\n", s2);

strcat(s1, " World");


printf("Concat: %s\n", s1);

printf("Compare (Hello, World): %d\n", strcmp("Hello",


"World"));

if(strchr(s1, 'o'))
printf("'o' found using strchr\n");

if(strstr(s1, "World"))
printf("\"World\" found using strstr\n");

return 0;
}
11. Write a program to reverse a given string
without using in-built functions.

#include <stdio.h>

int main() {
char s[100];
int i, len = 0;

printf("Enter a string: ");


fgets(s, 100, stdin);

// find length manually


while(s[len] != '\0' && s[len] != '\n')
len++;

printf("Reversed: ");
for(i = len - 1; i >= 0; i--)
printf("%c", s[i]);

return 0;
}
12. Write a program to generate the Fibonacci
series using recursion.

#include <stdio.h>

int fib(int n) {
if(n <= 1) return n;
return fib(n-1) + fib(n-2);
}

int main() {
int n;
printf("Enter n: ");
scanf("%d", &n);

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


printf("%d ", fib(i));
return 0;
}
13. Write a program to demonstrate the mathematical
functions:
sqrt(), log(), and pow().

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

int main() {
double x;
printf("Enter number: ");
scanf("%lf", &x);

printf("Square root = %lf\n", sqrt(x));


printf("Log value = %lf\n", log(x));
printf("Power (x^2) = %lf\n", pow(x,2));

return 0;
}
14. Write a program to swap two numbers using:
(a) Call by value
(b) Call by reference
#include <stdio.h>

void swapValue(int a, int b){


int temp = a;
a = b;
b = temp;
printf("After swap (call by value): %d %d\n", a, b);
}

void swapRef(int *a, int *b){


int temp = *a;
*a = *b;
*b = temp;
}

int main() {
int x=10, y=20;

swapValue(x, y);
swapRef(&x, &y);

printf("After swap (call by ref): %d %d", x, y);


return 0;
}
15. Write a program to print the elements of an
array using pointers.

#include <stdio.h>

int main() {
int a[5] = {1,2,3,4,5};
int *p = a;

for(int i=0;i<5;i++)
printf("%d ", *(p+i));

return 0;
}
16. Write a program using structures to store and display
student information.
#include <stdio.h>

struct student {
char name[50];
int roll;
float marks;
};

int main() {
struct student s;

printf("Enter name: ");


scanf("%s", [Link]);
printf("Enter roll: ");
scanf("%d", &[Link]);
printf("Enter marks: ");
scanf("%f", &[Link]);

printf("\nStudent Details:\n");
printf("Name: %s\nRoll: %d\nMarks: %.2f\n", [Link],
[Link], [Link]);

return 0;
}
17. Write a program to read a file and display its
contents using file handling.

#include <stdio.h>

int main() {
FILE *fp;
char ch;

fp = fopen("[Link]", "r");

if(fp == NULL) {
printf("File not found!");
return 0;
}

while((ch = fgetc(fp)) != EOF)


printf("%c", ch);

fclose(fp);
return 0;
}

You might also like