C – LANGUAGE (ASSIGNMENT – 1)
Experiment – 1
Aim: Install Dev C/C++ bundle or Codeblocks along with Cygwin gcc
compiler from https://sourceforge.net/project/orwelldevcp/
https://www.codeblocks.org/downloads
Name: Sakshi Singh
Enrollment No.: 02306142020
Program – 1
Aim: Write a C program to find the area of a circle.
Source code:
#include<stdio.h>
#include<conio.h>
#define Pi 3.14
int main()
{
float radius, area;
printf("Enter the radius\n");
scanf("%f", &radius);
area=Pi*radius;
printf("Area of circle:%f\n",area);
getch();
return 0;
}
Output:
Name: Sakshi Singh
Enrollment No.: 02306142020
Program – 2
Aim: Write a C program to find the temperature in Celsius from
Fahrenheit.
Source Code:
#include<stdio.h>
#include<conio.h>
int main ()
{
float c,f;
printf("Enter the temperature in fahrenhiet : ");
scanf("%f",&f);
c=((f-32)*5/9);
printf("\n The temperature in celsius is :%f ", c);
getch();
}
Output:
Name: Sakshi Singh
Enrollment No.: 02306142020
Program – 3
Aim: Write a C program to find out whether the character pressed through
the keyboard is a digit, alphabet or special character using ASCII values
(using conditional operator).
Source Code:
#include<stdio.h>
#include <conio.h>
int main()
{
char ch;
printf("ENTER THE CHARACTER: ");
scanf("%c", &ch);
if(ch >= 65 && ch <= 90)
{
printf("'%c', IS A CAPITAL ALPHABET", ch);
}
else if ((ch >= 97 && ch <= 122))
{
printf("'%c', IS A SMALL ALPHABET", ch);
}
else if(ch >= 48 && ch <= 57)
{
printf("'%c', IS A DIGIT", ch);
}
else
{
printf("'%c' IS A SPECIAL CHARACTER", ch);
}
return 0;
getch();
}
Output:
Name: Sakshi Singh
Enrollment No.: 02306142020
Program – 4
Aim: Write a C program that reads in three integers and then determines
and prints the largest and the smallest in the group.
Source Code:
#include <stdio.h>
#include <conio.h>
int main()
{
int a, b, c;
printf("ENTER THE THREE NUMBERS: ");
scanf("%d %d %d", &a, &b, &c);
//for the largest number
if (a >= b && a >= c)
{
printf("'%d' IS THE LARGEST NUMBER.\n", a);
}
else if (b >= a && b >= c)
{
printf("'%d' IS THE LARGEST NUMBER.\n", b);
}
else if (c >= a && c >= b)
{
printf("'%d' IS THE LARGEST NUMBER.\n", c);
}
//for the smallest number
if (a <= b && a <= c)
{
printf("'%d' IS THE SMALLEST NUMBER.\n", a);
}
else if (b <= a && b <= c)
{
printf("'%d' IS THE SMALLEST NUMBER.\n", b);
}
else if (c <= a && c <= b)
{
printf("'%d' IS THE SMALLEST NUMBER.\n", c);
}
return 0;
getch();
}
Output:
Name: Sakshi Singh
Enrollment No.: 02306142020
Program – 5
Aim: Write a C program to print lowercase and uppercase alphabets after
taking user’s choice. (Use if else and for loop).
Source code:
#include <stdio.h>
#include <conio.h>
int main()
{
char c;
printf("ENTER U/u TO DISPLAY UPPERCASE ALPHABETS.\n");
printf("ENTER L/l TO DISPLAY LOWERCASE ALPHABETS. \n");
scanf("%c", &c);
if (c == 'U' || c == 'u')
{
for (c = 'A'; c <= 'Z'; ++c)
printf("%c ", c);
}
else if (c == 'L' || c == 'l')
{
for (c = 'a'; c <= 'z'; ++c)
printf("%c ", c);
}
else
{
printf("INVALID CHARACTER! ");
}
return 0;
getch();
}
Output:
Name: Sakshi Singh
Enrollment No.: 02306142020
Program – 6
Aim: Write a C program that reads a number N and print the sum and
average of all numbers 1 to N.
Source code:
#include<stdio.h>
#include<conio.h>
int main()
{
int i, n, sum=0;
float avg;
printf("enter the value for N : ");
scanf("%d", &n);
for(i=1;i<n+1;i++)
{
sum = sum+i;
}
avg = sum/n;
printf(" sum: %d",sum);
printf(" average: %f", avg);
getch();
}
Output:
Name: Sakshi Singh
Enrollment No.: 02306142020
Program – 7
Aim: Write a C program that reads in two integers and determines and
prints whether the first is a multiple of the second. [use % operator.]
Source code:
#include<stdio.h>
#include<conio.h>
int main()
{
int a, b;
printf("ENTER THE FIRST NUMBER ");
scanf("%d", &a);
printf("ENTER THE SECOND NUMBER ");
scanf("%d", &b);
if(a%b==0)
{
printf("%d IS A MULTIPLE OF %d", a, b);
}
else
{
printf("IT IS NOT A MULTIPLE");
}
getch();
}
Output:
Name: Sakshi Singh
Enrollment No.: 02306142020
Program – 8
Aim: Any year is input through the keyboard. Write a C program to
determine whether the year is leap year or not. [use if-else and % operator]
Source code:
#include<stdio.h>
#include<conio.h>
int main ()
{
int y;
printf("ENTER THE YEAR (yyyy): ");
scanf("%d",&y);
if((y%4==0)&& (y%100!=0)|| (y%400==0))
{
printf("%d IS A LEAP YEAR", y);
}
else
{
printf("%d IS NOT A LEAP YEAR", y);
}
getch();
}
Output:
Name: Sakshi Singh
Enrollment No.: 02306142020
Program – 9
Aim: Given the length and breadth of a rectangle, write a C program to
find whether the area of the rectangle is greater than its perimeter.
Source code:
#include<stdio.h>
#include<conio.h>
int main()
{
float L,B,A,P;
printf("Enter the Length of Rectangle ");
scanf("%f", &L);
printf("Enter the Breadth of Rectangle ");
scanf("%f", &B);
P=2*(L+B);
printf("The perimeter of Rectangle is = %0.2f\n" ,P);
A=L*B;
printf("The Area of Rectangle is = %0.2f\n" ,A);
if (A>P)
printf("Area of Rectangle is greater than it's Perimeter");
else
printf("Perimeter of rectangle is greater than it's Area");
getch();
return 0;
}
Output:
Name: Sakshi Singh
Enrollment No.: 02306142020
Program – 10
Aim: Write a program to generate and print all even numbers from 1 to N.
Source code:
#include<stdio.h>
#include<conio.h>
int main()
{
int i, n;
printf("enter the terms: \n");
scanf("%d",&n);
printf("THE EVEN NUMBERS BETWEEN 1 AND %d \n",n);
for(i=1;i<n;i++)
{
if(i%2==0)
{
printf("%d \n", i);
}
}
getch();
}
Output:
Name: Sakshi Singh
Enrollment No.: 02306142020
Program – 11
Aim: Write a program to print Fibonacci series.
Source code:
#include<stdio.h>
#include<conio.h>
int main()
{
int i, a=0, b=1, c, n;
printf("THE NUMBER OF TERMS: \n");
scanf("%d", &n);
printf("THE FIRST %d TERMS OF FIBONACCI SERIES ARE: \n", n);
for(i=0;i<n;i++)
{
if(i<=1)
c=i;
else
{
c=a+b;
a=b;
b=c;
}
printf("%d \n",c);
}
getch();
}
Output:
Name: Sakshi Singh
Enrollment No.: 02306142020
Program – 12
Aim: Write a C program to compute grade of students using if else ladder.
The grades are assigned as followed: Marks Grade
Source code: Marks < 50 F
#include<stdio.h> 50 <= marks < 60 C
60 <= marks < 70 B
#include<conio.h>
70 <= marks < 80 B+
int main()
80 <= marks < 90 A
{ 90 <= marks <= 100 A+
float marks;
printf("Enter the marks = ");
scanf("%f", &marks);
if (marks<50)
{
printf("Grade F");
}
else if (marks>=50 && marks<60)
{
printf("Grade C");
}
else if (marks>=60 && marks<70)
{
printf("Grade B");
}
else if (marks>=70 && marks<80)
{
printf("Grade B+");
}
else if (marks>=80 && marks<90)
{
printf("Grade A");
}
else if (marks>=90 && marks<=100)
{
printf("Grade A+");
}
else
{
printf("CANNNOT COMPUTE GRADE");
}
getch();
}
Name: Sakshi Singh
Enrollment No.: 02306142020
Output:
Name: Sakshi Singh
Enrollment No.: 02306142020
Program – 13
Aim: Write a C program to find number of days in a month using switch
statement.
Source code:
#include<stdio.h>
#include<conio.h>
int main()
{
int m;
printf("Enter month number(1-12): ");
scanf("%d", &m);
switch(m)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
printf("31 days");
break;
case 2:
printf("28/29 days");
break;
case 4:
case 6:
case 9:
case 11:
printf("30 days");
break;
default:
printf("Invalid input!");
}
getch();
}
Output:
Name: Sakshi Singh
Enrollment No.: 02306142020
Program – 14
Aim: Write a C program using loop to find the factorial of a given number
entered by user.
Source code:
#include<stdio.h>
#include<conio.h>
int main()
{
int c, n, f = 1;
printf("ENTER THE NUMBER\n");
scanf("%d", &n);
for (c = 1; c <= n; c++)
f = f * c;
printf("Factorial of %d = %d\n", n, f);
return 0;
getch();
}
Output:
Name: Sakshi Singh
Enrollment No.: 02306142020
Program – 15
Aim: Write a C program that accepts a positive integer from the keyboard
and determines the first and the last digit of the number.
Source code:
#include <stdio.h>
#include <conio.h>
int main()
{
int n, fd, ld;
printf("ENTER THE NUMBER = ");
scanf("%d", &n);
ld = n%10;
while(n >= 10)
{
n=n/10;
}
fd = n;
printf("FIRST DIGIT = %d AND LAST DIGIT = %d\n\n", fd,ld);
return 0;
getch();
}
Output:
Name: Sakshi Singh
Enrollment No.: 02306142020
Program – 16
Aim: Write a program which reads integers representing date, for example
(18,5) and prints the date in full as: 18 May, note that the date should be
followed by an appropriate suffix: ‘st’, ‘nd’, ‘ed’ or ‘th’.
Source code:
#include<stdio.h>
#include<conio.h>
int main()
{
int d, m, y;
printf("ENTER THE DATE (DD/MM) \n");
scanf("%d/%d", &d, &m);
printf("%d",d);
if(d == 1 || d == 21 || d == 31)
{
printf("st ");
}
else if(d == 2 || d == 22)
{
printf("nd ");
}
else if(d == 3 || d == 23)
{
printf("rd ");
}
else
{
printf("th ");
}
switch(m)
{
case 1:
printf("JANUARY",m);
break;
case 2:
printf("FEBRUARY",m);
break;
case 3:
printf("MARCH",m);
break;
case 4:
printf("APRIL",m);
break;
case 5:
printf("MAY",m);
break;
Name: Sakshi Singh
Enrollment No.: 02306142020
case 6: printf("JUNE",m);
break;
case 7:
printf("JULY",m);
break;
case 8:
printf("AUGUST",m);
break;
case 9:
printf("SEPTEMBER",m);
break;
case 10:
printf("OCTOBER",m);
break;
case 11:
printf("NOVEMBER",m);
break;
case 12:
printf("DECEMBER",m);
break;
default:
printf("INVALID");
break;
}
getch();
}
Output:
Name: Sakshi Singh
Enrollment No.: 02306142020
Program – 17
Aim: Write a C program to find whether the given number is an
Armstrong Number or not.
Source code:
#include <stdio.h>
#include <conio.h>
int main()
{
int n, on, r, result = 0;
printf("ENTER A THREE-DIGIT INTEGER: ");
scanf("%d", &n);
on = n;
while (on != 0)
{
r = on % 10;
result+=r*r*r;
on/= 10;
}
if (result==n)
printf("%d IS AN ARMSTRONG NUMBER.", n);
else
printf("%d IS NOT AN ARMSTRONG NUMBER.", n);
return 0;
getch();
Output:
Name: Sakshi Singh
Enrollment No.: 02306142020
Program – 18
Aim: Write a C program to find whether the given number is a Palindrome
Number or not.
Source code:
#include<stdio.h>
#include<conio.h>
int main()
{
int n, rn = 0, r, on;
printf("ENTER AN INTEGER: ");
scanf("%d", &n);
on = n;
while (n != 0)
{
r= n%10;
rn= rn*10+r;
n/=10;
}
if (on==rn)
printf("%d IS A PALINDROME.", on);
else
printf("%d IS NOT A PALINDROME.", on);
return 0;
getch();
}
Output:
Name: Sakshi Singh
Enrollment No.: 02306142020
Program – 19
Aim: Write a C program to print the following patterns:
(a) Half pyramid of star
Source code:
#include <stdio.h>
#include <conio.h>
int main()
{
int i, j, rows;
printf (" ENTER THE NUMBER OF ROWS: \n ");
scanf("%d", &rows);
printf("\n");
for (i = 1; i <= rows; ++i)
{
for (j = 1; j <= i; ++j)
{
printf ("* ");
}
printf ("\n");
}
getch();
}
Output:
Name: Sakshi Singh
Enrollment No.: 02306142020
(b) Half pyramid of alphabets
Source code:
#include <stdio.h>
#include <conio.h>
int main()
{
int i, j;
char c, alpha='A';
printf("ENTER AN UPPERCASE CHARACTER YOU WANT TO PRINT IN THE LAST
ROW: \n");
scanf("%c",&c);
for(i=1;i<= (c-'A'+1); ++i)
{
for(j=1;j<=i;++j)
{
printf("%c ", alpha);
}
++alpha;
printf("\n");
}
getch();
}
Output:
Name: Sakshi Singh
Enrollment No.: 02306142020
(C)Inverted pyramid of numbers
Source code:
#include <stdio.h>
#include <conio.h>
int main()
{
int i, j,rows;
printf("PRINT THE NUMBER OF ROWS: ");
scanf("%d",&rows);
for(i = rows; i>=1; --i)
{
for(j = 1; j <= i; ++j)
{
printf("%d ",j);
}
printf("\n");
}
getch();
return 0;
}
Output:
Name: Sakshi Singh
Enrollment No.: 02306142020
(d)Full pyramid of numbers
Source code:
#include <stdio.h>
#include <conio.h>
int main()
{
int i,j=0,rows,num=0,a=0,b;
printf("Enter the number of rows you want:");
scanf("%d",&rows);
for(i=1; i<=rows; ++i)
{
for(b=1; b<=rows-i; b++)
{
printf(" ");
a++;
}
while(j!=2*i-1)
{
if(a<=rows-1)
{
printf(" %d ", i+j);
++a;
}
else
{
num++;
printf(" %d ",(i+j-2*num));
}
++j;
}
num=a=j=0;
printf("\n");
}
getch();
return 0;
}
Output:
Name: Sakshi Singh
Enrollment No.: 02306142020
Program – 20
Aim: Write a program to find the diameter, circumference and area of a
circle using functions.
Source code:
#include <stdio.h>
#include <math.h>
#include <conio.h>
float find_Diameter(float radius)
{
return 2 * radius;
}
float find_Circumference(float radius)
{
return 2* M_PI * radius;
}
float find_Area(float radius)
{
return M_PI * radius * radius;
}
int main()
{
float radius, area, circumference, diameter;
int op;
printf("PLEASE ENTER THE RADIUS OF A CIRCLE : \n");
scanf("%f",&radius);
printf("FOR DIAMETER = 1 \nFOR CIRCUMFERENCE = 2 \nFOR AREA = 3 \n");
printf("ENTER THE CHOICE : ");
scanf("%d", &op);
diameter = find_Diameter(radius);
circumference = find_Circumference(radius);
area = find_Area(radius);
switch(op)
{
case 1:
{
printf("\n DIAMETER OF A CIRCLE = %.2f\n", diameter);
break;
}
case 2:
Name: Sakshi Singh
Enrollment No.: 02306142020
{
printf(" CIRCUMFERENCE OF A CIRCLE = %.2f\n", circumference);
break;
}
case 3:
{
printf(" AREA OF A CIRCLE = %.2f\n", area);
break;
}
default:
{
printf("INVALID");
break;
}
}
return 0;
getch();
}
Output:
Name: Sakshi Singh
Enrollment No.: 02306142020
Program – 21
Aim: Write a program to create the calculator using function.
Source code:
#include <stdio.h>
#include <conio.h>
int main()
{
int a, b, r;
char op;
printf("ENTER AN EXPRESSION: ");
scanf("%d%c%d", &a, &op, &b);
switch(op)
{
case '+':
r = a + b;
break;
case '-':
r = a - b;
break;
case '*':
r = a * b;
break;
case '/':
r = a / b;
break;
}
printf("RESULT = %d", r);
getch();
}
Output:
Name: Sakshi Singh
Enrollment No.: 02306142020
Program – 22
Aim: Write a program to create a structure named company which has
employee information (ID, Name, City and Phone No. of employee) as
member variables. The program will take values separately for each
employee (ID, Name, City and Phone No.). Finally display these member’s
values.
Source code:
#include <stdio.h>
#include <conio.h>
struct employee
{
char name[100], city[50], num[11];
int eId;
};
int main()
{
int i, n;
struct employee emp[n];
printf("ENTER THE NUMBER OF RECORDS: \n");
scanf("%d", &n);
for(i=0; i<n; i++)
{
printf("ENTER THE RECORD OF EMPLOYEE= %d \n", i+1);
printf("ENTER THE ID OF THE EMPLOYEE: \n");
scanf("%d",&emp[i+1].eId);
printf("ENTER THE NAME OF THE EMPLOYEE: \n");
scanf("%s",&emp[i+1].name);
printf("ENTER THE CITY OF THE EMPLOYEE: \n");
scanf("%s",&emp[i+1].city);
printf("ENTER THE PHONE NUMBER OF THE EMPLOYEE: \n");
scanf("%s",&emp[i+1].num);
printf("\n");
}
printf("DETAILS OF THE EMPLOYEES: ");
for(i=0; i<n; i++)
{
printf("ID OF THE EMPLOYEE: %d \n", emp[i+1].eId);
printf("NAME OF THE EMPLOYEE: %s \n", emp[i+1].name);
printf("CITY OF AN EMPLOYEE: %s \n", emp[i+1].city);
printf("PHONE NUMBER THE EMPLOYEE: %s \n", emp[i+1].num);
}
Name: Sakshi Singh
Enrollment No.: 02306142020
return 0;
getch();
}
Output:
Name: Sakshi Singh
Enrollment No.: 02306142020