0% found this document useful (0 votes)
7 views12 pages

C Programming Basics: Sample Programs

The document contains a collection of C programming examples demonstrating various concepts such as calculating averages, finding areas, checking number properties, using loops, arrays, functions, structures, and pointers. Each program is presented with code snippets and includes basic input/output operations. The examples serve as educational material for beginners learning C programming.

Uploaded by

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

C Programming Basics: Sample Programs

The document contains a collection of C programming examples demonstrating various concepts such as calculating averages, finding areas, checking number properties, using loops, arrays, functions, structures, and pointers. Each program is presented with code snippets and includes basic input/output operations. The examples serve as educational material for beginners learning C programming.

Uploaded by

hirekartik243
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

C programming

I. Program to find average of three numbers


#include <stdio.h>
#include<conio.h>
Main void()
{
Float num1, num2, num3, average;

Printf(“Enter three numbers: “);


Scanf(“%f %f %f”, &num1, &num2, &num3);
Clrscr();
Average = (num1 + num2 + num3) / 3;

Printf(“The average of the three numbers is: %.2f\n”,


average);

Return 0;
}

[Link] to find area of circle


#include <stdio.h>
#include<conio.h>
Main void()
{
Float pI= 3.14;
Int r=5;
Float= 3.14×5×5
Printf(“%f”,aoc)
Float = 78.5
Getch ();
}

[Link] to Find largest number from two numbers


#include<stdio.h>
#include<conio.h>
Int max(int a,int b);
Main()
{
Int x,y;
Printf(“Enter any two number:”);
Scanf(“%d%d”,&x,&y);
Max(x,y);
}
Int max(int a,int b)
{
If(a>b)
Printf(“A is greater”);
Else
Printf(“B is greater”);
Getch();
}

[Link] to check entered number is positive, negative


or zero
#include <iostream>
#include <conio.h>
Using namespace std;
Int main()
{
Int n;
Cout<<”Enter a number “;
Cin>>n;
If(n>0)
{
Cout<<”Positive number”;
}
Else if(n<0)
{
Cout<<”Negative number”;
}
Else
{
Cout<<”Zero”;
}
Return 0;
}

[Link] to demonstrate switch case statement


#include <stdio.h>

Int main() {
Int num;

Printf(“Enter an integer: “);


Scanf(“%d”, &num);

If (num > 0) {
Printf(“%d is a positive number.\n”, num);
} else if (num < 0) {
Printf(“%d is a negative number.\n”, num);
} else {
Printf(“%d is zero.\n”, num);
}

Return 0;
}
[Link] to print odd numbers from 1 to N
#include <stdio.h>
Int main()
{
Int I, n;
Printf(“Print odd numbers till: “);
Scanf(“%d”, &n);
Printf(“All odd numbers from 1 to %d are: \n”, n);
For(i=1; i<=n; i++)
{
If(i%2!=0)
{
Printf(“%d\n”, i);
}
}
Return 0;
}
[Link] to demonstrate nested loop

#include <stdio.h>
#include <unistd.h>
Int main() {
Char message[] = “A long time ago in a galaxy far,
far away…”;
For (int I = 0; I < 5; i++) { // Outer loop for scrolling
For (int j = 0; j < I; j++) { // Inner loop for spaces
Printf(“ “);
}
Printf(“%s\n”, message);
Usleep(500000); // Delay for animation
}
Return 0;
}

[Link] to check entered number is even or odd

#include<stdio.h>
#include<conio.h>
Void main()
{
Int num=21;
Clrscr();
If (num2==0)
Printf(“It is an even number=>%d”,num);
Else
Printf(“It is an odd number=>%d”, num);
Getch();
}

[Link] to print the Fibonacci Series


#include <stdio.h>
Int main()
{
Int n, first = 0, second = 1, next;
Printf(“Enter the number of terms: “);
Scanf(“%d”, &n);
Printf(“Fibonacci Series: “);
For (int I = 0; I < n; i++) {
If (I <= 1)
Next = I;
Else {
Next = first + second;
First = second;
Second = next;
}
Printf(“%d “, next);
}
Return 0;
}

10. Program to demonstrate one dimensional array


#include<stdio.h>
#include<conio.h>
Void main ()
{
Int a[5], I;
Printf (“Enter Array Elements:\n”);
For (I = 0; I < 5; i++)
{
Scanf (“%d”, &a[i]); //Run time array initialization
}
Printf (“Entered Array Elements are : “);
For (I = 0; I < 5; i++)
{
Printf (“%d “, a[i]);
}
Getch ();
}

11. Program to demonstrate two dimensional array


#include<stdio.h>
#include<conio.h>
Void main()
{
Int a[10],I;
Clrscr();
Printf(“Enter 10 integer values: \n”);
For(i=0;i<=9;i++) scanf(“%d”,&a[i]); { } printf(“Entered
Array values are: \n”); for(i=0;i<=9;i++) }
Printf(“%d \t”,a[i]); {
Getch();
}
12. Program to find factorial of given number using
function
#include<stdio.h>
#include<conio.h>
Void main()
{
Int I,n,f=1;
Clrscr();
Printf(“\n Enter number”);
Scanf(“%d”, &n);
For(i=1;i<=n;i++)
{
F=f*I;
}
Printf(“Factorial of given number is %d”,f);
Getch();
}

13. Program for Swapping of numbers by using call by


reference

#include <stdio.h>
Void swap(int x, int y) {
Int temp = x;
X = y;
Y = temp;
}
Int main() {
Int a = 40, b = 50;
Printf(“Before swap, a = %d and b = %d\n”, a, b);
Swap(a, b);
Printf(“After swap, a = %d and b = %d\n”, a, b);
Return 0;
}

14. Program to create structure student

#include<conio.h>
#include<stdio.h>
Struct stud_record
{
Char name [25];
Char cls[5];
Int roll_no:
};
Void main()
{
Struct stud_record student,s*;
Crlscr()
S=&student;
Printf(enter name : “);
Scanf(,s->name);
Printf(“Enter class: “);
scanf(,s->cls);
printf(Enter roll no.: ");
scanf(“%d”,&s->roll_no);
Printf(“\n\n”);
Printf(“RECORD ENTERED\n NAME:%s\n”,s->name);
Printf(“Class: %s” ,s->cls);
Printf(“\nRoll no. %d”,s->roll_no);
Getch();
}

15. Program to demonstrate pointers


#include<stdio.h>
#include <conio.h>
#include <stdlib.h>
Int *largestnumber(int x, int *y)
{
If (x > *y)
Return x;
Else
Return y;
}
Void main()
{
Clrscr();
int i,j,*large;
printf (“Please enter two values\n”);
scanf("%d%d", &i,&j);
large=largestnumber(&i,&j);
printf (“Largest of two numbers(%d,%d)=%d\n”, I,j,
*large);
Getch()
}

You might also like