1.
BASIC C PROGRAMS I/O STATEMENTS
1a. PRINTING STUDENT NAME AND COLLEGE NAME
PROGRAM:
#include<stdio.h>
#include<coinio.h>
void main()
{
printf(“RAVI\n”);
printf(“JSREC”);
getch();
}
OUTPUT:
RAVI
JSREC
1b. AREA CALCULATION OF VARIOUS SHAPES
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
float r,l,b,s,h;
float circle,rectangle,square,triangle;
printf("enter the numbers:");
scanf("%f%f%f%f%f",&r,&l,&b,&s,&h);
circle=(22*r*r)/7;
printf("\narea of the circle is:%f",circle);
rectangle=l*b;
printf("\narea of the rectangle is:%f",rectangle);
square=s*s;
printf("\narea of the square is:%f",square);
triangle=(b*h)/2;
printf("\narea of the triangle is:%f",triangle);
getch();
}
OUTPUT:
Enter the numbers: 4 6 4 5 7
area of the circle is: 50.281754
area of the rectangle is : 24
area of the square is:25
area of the triangle is:14
1c. FIND THE ROOTS OF A QUADRATIC EQUATION
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,d,r1,r2;
clrscr();
printf(“\nEnter the values of a,b and c”);
scanf(“%f%f%f”,&a,&b,&c);
d=b*b-4*a*c;
r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
printf(“The root1 and root2 of a=%f b=%f c=%f is \n %f \n %f“,a,b,c,r1,r2);
getch();
}
Output:
Enter the values of a,b and c
7
35
9
The root1 and root2 of a=7 b=35 c=9 is
-0.271932
-4.728068
1d. FIND THE GIVEN NUMBER IS ODD OR EVEN
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int number;
clrscr();
printf(“\nEnter the number”);
scanf(“%d”,&number);
if(number%2==0)
{
printf(“\nThe number is even number”);
}
else
{
printf(“\nThe number is odd number”);
}
getch();
}
OUTPUT:
Enter the number
5
The number is odd number
Enter the number
4
The number is even number
2.DECISION MAKING CONSTRUCTS
2a FIND THE LARGEST OF THREE NUMBERS
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(“\nEnter the numbers a,b and c”);
scanf(“%d %d%d”,&a,&b,&c);
if(a>b)
{
if(a>c)
printf(“\n%d is largest number”,a);
else
printf(“\n%d is largest number”,c);
}
else
{
if(b>c)
printf(“\n%d is largest number”,b);
else
printf(“\n%d is largest number”,c);
}
getch();
}
OUTPUT:
Enter the numbers a b and c
5 10 15
15 is largest number
2b. MULTIPLICATION TABLE FOR THE GIVEN NUMBER
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int table;
int i,count;
clrscr();
printf("Enter the number of table you want to print\n");
scanf("%d",&table);
printf("Result:\n");
for(i=1;i<=10;i++)
{
printf("%d * %d =%d\n",i,table,i*table);
}
getch();
}
OUTPUT:
Enter the number of tables you want to print
2
Result:
1* 2=2
2*2=4
3*2=6
4*2=8
5*2=10
6*2=12
7*2=14
8*2=16
9*2=18
10*2=20
2c. FIRST N NUMBERS DIVISIBLE BY 5
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
printf("\n Enter the number of terms:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%5==0)
{
printf("\t%d",i);
}
}
getch( );
}
OUTPUT:
Enter the number of terms: 50
5 10 15 20 25 30 35 40 45 50
2d. PALINDROME USING INTEGERS
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,result=0,sum=0,e;
clrscr();
printf("Enter no:");
scanf("%d",&n);
e=n;
while(n>0) {
result =n%10;
sum =sum *10+result;
n=n/10;
} if(e==sum)
printf("It ia a Palindrome");
else
printf("It is not a palindrome");
getch();
}
Output:
Enter no:121
It is a Palindrome
Enter no: 345
It is not a palindrome
2e. CHECKING PRIME NUMBER
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,c=0,i,n;
clrscr();
printf(" Enter the no to be checked:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
a=n%i;
if(a==0)
{
c=c+1;
}
}
if(c==2)
{
printf(" It is a prime number");
}
else
{
printf(" It is not a prime number");
}
getch( );
}
OUTPUT:
Enter a no to be checked: 6
It is not a prime number
Enter a no to be checked: 5
It is a prime number
2f. FIBONACCI NUMBER
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int f,f1=-1,f2=1,x,a;
clrscr();
printf("Enter a no:");
scanf("%d",&n);
for(a=0;a<n;a++)
{
f=f1+f2;
f1=f2;
f2=f;
printf("\n%d",f);
}
getch();
}
OUTPUT:
Enter a no: 5
01123
3. IMPLEMENTATION OF LOOPS
3a. DISPLAY FIRST ‘N’ NATURAL NUMBERS USING FOR LOOP
PROGRAM
#include <stdio.h>
int main()
{
int i, n;
printf("Enter any number: ");
scanf("%d", &n);
printf("Natural numbers from 1 to %d : \n", n);
for(i=1; i<=n; i++)
{
printf("%d\t", i);
return 0;
3b. DISPLAY FIRST ‘N’ NATURAL NUMBERS USING WHILE & DO-WHILE LOOP
PROGRAM
#include <stdio.h>
int main()
{
int i=1,j=1,n;
printf("Enter the Limit: ");
scanf("%d", &n);
printf("Natural numbers from 1 to %d using while loop: \n", n);
while(i<=n)
{
printf("%d\t", i);
i++;
printf("\nNatural numbers from 1 to %d using do-while loop: \n"n);do
printf("%d\t", j);
j++;
while(j<=n);
return 0;
4. Arrays(One-Dimensional Array in C)
#include <stdio.h>
int main()
{
int numbers[5] = {10, 20, 30, 40, 50};
for(int i=0; i<5; i++) {
printf("numbers[%d] = %d\n", i, numbers[i]);
}
return 0;
}
Output:
numbers[0] = 10
numbers[1] = 20
numbers[2] = 30
numbers[3] = 40
numbers[4] = 50