0% found this document useful (0 votes)
104 views

Experiment-1: AIM: Write A Program To Print "Hello World"

This file contains some basic C programs which you should know if you are in school or started learning C
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views

Experiment-1: AIM: Write A Program To Print "Hello World"

This file contains some basic C programs which you should know if you are in school or started learning C
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Experiment-1

I. AIM: Write a Program to print “Hello World”.

II. THEORY: To print “Hello World.”

III. CODE:

#include <stdio.h>
int main()
{
printf(“Hello World!\n”);
return 0;
}

IV. OUTPUT:

V. LEARNING:
Through this experiment we have learnt how to print a statement.

VI. FLOW CHART:

START

OUTPUT

Hello World

END

Experiment-2

2
I. AIM: Write a program to find the sum and average of two numbers.

II. THEORY: The aim of the practical is to find the sum and average of two
numbers. The sum of two numbers is defined as the algebraic addition of
two numbers given.
Average is defined as the sum of the numbers divided by the number of
numbers. In this case sum of the numbers is divided by 2 since total
number of numbers are 2. For Example, if the user entered the numbers 3
and 7, then the sum of numbers will be 10 and the average will be 4.5
. sum = 3+7 = 10
. average = (3+7)/2 = 5

III. CODE:

#include<stdio.h>
#include<conio.h>
float main()
{
float x,y,sum=0;
float average=0;
printf("\n Enter the two values \n");
scanf("%f %f",&x,&y);
sum=x+y;
average=sum/2;
printf("Sum of 2 numbers= %f \n",sum);
printf("Average of 2 numbers= %f \n",average);
return 0;
}

IV. OUTPUT:

3
V. LEARNING:

Through this experiment we have learnt how to find the sum and average
of two numbers. Time complexity is the time taken by algorithm to
execute the steps.

VI. FLOWCHART:

4
START

Input x,y

sum=0

average=0

sum = x+y

average=sum/2

Print sum and


average

End

Experiment-3

5
I. AIM: Write a program to print the following pattern. (Triangle of stars).

II. THEORY:
This program is used to print a pattern i.e triangle of stars. The pattern is
printed in such a way that it prints numbers of stars equal to the row number.
(8 Rows)

III. CODE:

#include<stdio.h>
int main()
{
int i;
printf("Printing Star Pattern");
for ( i = 1; i < 10; i++)
{
printf("\n");
for (int j = 1;j<i ;j++)
printf("*");
}
return 0;
}

IV. OUTPUT

6
V. LEARNING:

Through this experiment we have learnt how to print a pattern. This


knowledge can be used in a wider application to build newer pattern of
different types.

VI. FLOW CHART:

7
Experiment-4

8
I. AIM: Write a program to print the following pattern. (Isosceles Triangle of
stars).

II. THEORY:

This program is used to print a pattern i.e. triangle of stars. It takes one input
for rows.

III. CODE:

#include<stdio.h>
int main()
{
int i;
int num = 0;
printf("Enter the number of rows\n");
scanf("%d",&num);
printf("Printing Star Pattern\n");
for ( i = 1; i <= num; i++)
{
for (int j = num;j>i ;j--)
printf(" ");
for (int k =0;k<2*i-1;k++)
printf("*");
printf("\n");
}
}
IV. Output

9
V. Learning:

Through this experiment we have learnt how to print a pattern. This


knowledge can be used in a wider application to build newer pattern of
different types.

VI. Flowchart:

10
11
Experiment-5

I. AIM: Write a Program to print the following pattern. (Diamond Pattern)

II. THEORY:

This program is used to print a pattern i.e. Diamond of stars. It takes one
input for rows.

III. CODE:

#include<stdio.h>
int main()
{
int i;
int num = 0;
printf("Enter the number of rows\n");
scanf("%d",&num);
printf("Printing Star Pattern\n");
for ( i = 1; i <= num; i++)
{
for (int j = num;j>i ;j--)
printf(" ");
for (int k =0;k<2*i-1;k++)
printf("*");
printf("\n");
}
for ( i = num-1; i >= 1; i--)
{
for (int j =num;j>i ;j--)
printf(" ");

12
for (int k =0;k<2*i-1;k++)
printf("*");
printf("\n");

}
return 0;
}

IV. OUTPUT:

V. LEARNING:

Through this experiment we have learnt how to print a pattern. This


knowledge can be used in a wider application to build newer pattern of
different types

VI. FLOWCHART:

13
14
Experiment-6

I. AIM: Write a Program to find sum of digits of a 5-digit number.

II. THEORY:

To find the sum of the digits of a 5-digit number we have to divide the
number by 10 to get the units place and store it and now divide the number
by 10 store it as integer and repeat the steps again and add to stored units
place every time till the number is less than zero.

III. CODE:

#include<stdio.h>
int main()
{
int a=0,rem=0,sum=0;
printf("Enter the number for Digit Sum\n");
scanf("%d",&a);
while(a!=0)
{
rem=(a%10);
a=(a/10);
sum+=rem;
}
printf("Sum of digits =%d",sum);
return 0;
}

15
IV. OUTPUT:

V. LEARNING:

Through this experiment we have learnt how to find the sum of digits of a
five-digit number. Time complexity is the time taken by algorithm to
execute the steps.

• Time Complexity: O(n), where n is the no of entries.

16
VI. FLOW CHART:

Experiment-7

17
I. AIM: Write a program to convert decimal to binary number.

II. THEORY:

This program is for conversion of decimal number entered by the user


into its equivalent binary number.
The method for converting a decimal number to binary is one that can be
used to convert from decimal to any number base. It involves using
successive division by the radix until the dividend reaches 0. At each
division, the remainder provides a digit of the converted number, starting
with the least significant digit.
An example of the process:
3710 to binary.
From the given steps: The resulting binary number is 111001111110.

III. CODE:

#include<stdio.h>
int toBin(int);
int main()
{
int dno;
printf(" Input any decimal number : ");
scanf("%d",&dno);
toBin(dno);
return 0;
}
int toBin(int dno)
{
int i=0,j,bno[32];
while(dno>0)
{

18
bno[i]=dno%2;
dno=dno/2;
i++;
}
printf("\n The Binary value is ");
for(j=i-1;j>0;j--)
{
printf("d",bno[j]);
}
}

IV. OUTPUT:

V. LEARNING:

19
Through this experiment we have learnt how to convert a decimal
number into its binary form. Time complexity is the time taken by
algorithm to execute the steps and depends on the number.
Time Complexity: O(n).

VI. FLOW CHART:

20
Experiment-8

I. AIM: Write a program to convert binary to decimal number.

II. THEORY:

This program is for conversion of binary number entered by the user into
its equivalent decimal number.
The method for converting a binary number to decimal is one that can be
used to convert from binary to any number base. It involves using
successive multiplication by the two raises to power of that unit place. At
each multiplication the gets added and the resultant number is submission
of all numbers.

An example of the process:


111001 to decimal:
From the given steps.
The resulting decimal number is:57

III. CODE:

#include<math.h>
#include<stdio.h>
#include<conio.h>
int main()
{
long int n, dec=0, i=0, rem=0;
clrscr();
printf("Enter a Binary Number: ");
scanf("%ld",&n);
while(n!=0)
{

21
rem=n%10;
dec+=rem*pow(2,i);
n/=10;
i++;
}
printf("The Decimal Number is: %ld",dec);
getch();
}

IV. OUTPUT:

V. LEARNING:

Through this experiment we have learnt how to convert a binary number


into its decimal form. Time complexity is the time taken by algorithm to
execute the steps and depends on the number.
Time Complexity: O(n).

22
VI. FLOW CHART:

23
24

You might also like