0% found this document useful (0 votes)
5 views31 pages

Program

The document contains a series of C programming exercises with corresponding code implementations and outputs. Each program addresses a specific task, such as printing 'Hello World', checking for positive or negative numbers, calculating simple interest, and more. The document serves as a practical guide for learning various programming concepts in C.

Uploaded by

sfdcritik2
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)
5 views31 pages

Program

The document contains a series of C programming exercises with corresponding code implementations and outputs. Each program addresses a specific task, such as printing 'Hello World', checking for positive or negative numbers, calculating simple interest, and more. The document serves as a practical guide for learning various programming concepts in C.

Uploaded by

sfdcritik2
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

Program-1

Object :- Write a c program to print hello world.


#include<stdio.h>
#include<conio.h> void
main() { printf("\nHello
World");
printf("\nThe output produced by : Ritik Rathi");
getch ();
}

OUTPUT :

Hello World
The output produced by : Ritik Rathi
Program-2

Object :- Write a c program to check whether the given number is


positive or negative.
#include<stdio.h>
#include<conio.h> void
main()
{ int num; scanf("%d",&num); if(num
== 0) printf("Neither positive nor
negative"); else if(num < 0)
printf("Negative"); else
printf("Positive");
printf("\nThe output produced by Ritik Rathi");
getch(); }

OUTPUT :
Program-3

Write a c program to find the simple interest.


#include<stdio.h>
#include<conio.h>
void main() { float
p,r,t,si;
p=34000; r=30; t=5; si=(p*r*t)/100;
printf("\nSimple interest is :%f",si);
printf("\nThe output produced by SAKSHAM TYAGI");
getch(); }

OUTPUT :
Program-4

Write a c program to check the leap year.

#include<conio.h> void
main()
{ int year; year = 2016; if (((year % 4 == 0) && (year % 100!
= 0)) || (year%400 == 0)) printf("%d is a leap year", year); else
printf("%d is not a leap year", year);
printf("\nThe output produced by SAKSHAM TYAGI");
getch(); }

OUTPUT :

Program-5

Write a c program to swap two numbers without using third


variable.
#include<stdio.h>
#include<conio.h> void
main()
{ int x,
y;
printf("Input value for x & y: \n"); scanf("%d%d",&x,&y);
printf("Before swapping the value of x & y: %d %d",x,y);
x=x+y; y=x-y; x=x-y;
printf("\nAfter swapping the value of x & y: %d %d",x,y);
printf("\nThe output produced by SAKSHAM TYAGI");
getch(); }

OUTPUT :

Program-6

Write a c program to find the area of a circle.

#include<conio.h>
void main() {
float radius, area;
printf("\nEnter the radius of Circle : ");
scanf("%f",&radius); area =
3.14*radius*radius; printf("\nArea of
Circle : %f", area);
printf("\nThe output produced by SAKSHAM TYAGI");
getch();
}

OUTPUT :

Program-7

Write a c program to find the greatest number between three


numbers.
#include<stdio.h>
#include<conio.h> void
main()
{
int A, B, C;
printf("Enter the numbers A, B and C: ");
scanf("%d %d %d", &A, &B, &C); if
(A >= B && A >= C) printf("%d is the
largest number.", A); if (B >= A && B
>= C) printf("%d is the largest
number.", B); if (C >= A && C >= B)
printf("%d is the largest number.", C);
printf("\nThe output produced by SAKSHAM TYAGI");
getch(); }

OUTPUT :

Program-8

Write a c program to check prime number.

#include<conio.h> void main(){ int


n,i,m=0,flag=0; printf("Enter the
number to check prime:");
scanf("%d",&n); m=n/2;
for(i=2;i<=m;i++)
{
if(n%i==0)
{
printf("Number is not prime");
flag=1; break;
}
} if(flag==0)
printf("Number is prime");
printf("\nThe output produced by SAKSHAM TYAGI");
getch(); }

OUTPUT :
Program-9

Object :- Write a c program to check palindrome number.


#include<stdio.h>
#include<conio.h> void
main ()
{ int num, reverse = 0, rem,
temp; num=11211;
printf("The number is :%d\n",num);
temp = num; while(temp != 0)
{ rem = temp % 10; reverse
= reverse * 10 + rem;
temp /= 10;
};
if (num == reverse) printf("%d is
Palindrome\n", num); else
printf("%d is Not Palindrome\n", num); printf("The
output produced by SAKSHAM TYAGI"); getch();
}

OUTPUT :

Program-10

Write a c program to check armstrong number.


#include<conio.h> void
main() {
int num, originalNum, remainder, result = 0;
printf("Enter a three-digit integer: ");
scanf("%d", &num); originalNum = num;
while (originalNum != 0) { remainder =
originalNum % 10; result += remainder *
remainder * remainder; originalNum /= 10;
} if (result == num) printf("%d is an
Armstrong number.", num); else
printf("%d is not an Armstrong number.", num); printf("\nThe
output produced by SAKSHAM TYAGI");
getch();
}

OUTPUT :

Progra
m-11

Write a c program to print sum of digits.

#include<conio.h> void
main()
{
int n,sum=0,m; printf("Enter
a numberS:");
scanf("%d",&n); while(n>0)
{
m=n%10;
sum=sum+m; n=n/10;
}
printf("Sum is=%d",sum);
printf("\nThe output produced by SAKSHAM TYAGI"); getch();
}

OUTPUT :

Program-12

Write a c program to reverse given number.

#include<CONIO.H>
void main()
{ int n, reverse=0, rem;
printf("Enter a number: ");
scanf("%d", &n);
while(n!=0)
{
rem=n%10;
reverse=reverse*10+rem; n/=10;
}
printf("Reversed Number: %d",reverse); printf("\nThe
output produced by SAKSHAM TYAGI"); getch();
}

OUTPUT :
Program-13

Write a c program to print factorial of a number with using


recursion.
#include<stdio.h>
#include<conio.h> long int
multiplyNumbers(int n); void
main() { int n; printf("Enter a
positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
printf("\nThe output produced by SAKSHAM TYAGI");
getch(); } long int
multiplyNumbers(int n)
{ if
(n>=1)
return n*multiplyNumbers(n-1);
else return 1;
}

OUTPUT :

Program-14
Write a c program to print fibonacci series without using
recursion.
#include<stdio.h>
#include<conio.h> void
main()
{
int n1=0,n2=1,n3,i,num;
printf("Number of elements:");
scanf("%d",&num); printf("\n%d
%d",n1,n2); for(i=2; i < num; +
+i)
{
n3=n1+n2;
printf(" %d",n3);
n1=n2; n2=n3;
}
printf("\nThe output produced by SAKSHAM TYAGI");
getch();
}

OUTPUT :

Program-15
Write a c program to print week days using switch case.
#include<stdio.h>
#include<conio.h>
void main() { int
day;
printf("Enter Day Number\n");
scanf("%d", &day); switch(day){
case 1 : printf("Monday\n"); break;
case 2 : printf("Tuesday\n"); break;
case 3 : printf("Wednesday\n"); break;
case 4 : printf("Thursday\n"); break;
case 5 : printf("Friday\n"); break;
case 6 : printf("Saturday\n"); break;
case 7 : printf("Sunday\n"); break;
default: printf("Invalid Input !!!!\n");
}
printf("\nThe output produced by SAKSHAM TYAGI");
getch();
}
OUTPUT :
Program-16

Object :- Write a c program to print addition of 2 matrices.


#include<stdio.h>
#include<conio.h> void
main(){
int r, c, mat1[100][100], mat2[100][100], sum[100][100], i, j;
printf("\nEnter the number of rows and columns : ");
scanf("%d%d", &r, &c); printf("\nInput Matrix 1 elements :
"); for(i=0; i<r; ++i) for(j=0; j<c; ++j)
{ scanf("%d",&mat1[i][j]);
} printf("\nMatrix 1\n");
for(i=0;i<r;i++)
{ for(j=0;j<c;j++)
{ printf("%d",mat1[i][j]);
} printf("\n"); }
printf("\nInput Matrix 2 elements : ");
for(i=0; i<r; ++i) for(j=0; j<c; ++j)
{ scanf("%d", &mat2[i][j]);
} printf("\nMatrix 2\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{ printf("%d",mat1[i][j]);
} printf("\n"); }
printf("\nAdded Matrix\n");
for(i=0;i<r;++i) for(j=0;j<c;++j)
{ sum[i][j]=mat1[i][j]+mat2[i][j];
} for(i=0;i<r;++i) for(j=0;j<c;++j)
{
printf("%d",sum[i][j]);
if(j==c-1) {
printf("\n"); }}
printf("\nThe output produced by SAKSHAM TYAGI");
getch();
}

OUTPUT :

Program-17

Object :- Write a c program to print multiplication of 2 matrices.


#include<stdio.h>
#include<conio.h> void
main()
{ int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
printf("enter the number of row and column=");
scanf("%d%d",&r,&c);
printf("enter the first matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]); } }
printf("enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]); } } printf("multiply
of the matrix=\n"); for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{ mul[i][j]=0;
for(k=0;k<c;k++)
{

mul[i][j]+=a[i][k]*b[k][j]; } } } for(i=0;i<r;i+
+)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]); } printf("\n");
}
printf("\nThe output produced by SAKSHAM TYAGI");
getch();
}

OUTPUT :
Program-18

Write a c program to print number triangle.


#include<stdio.h>
#include<conio.h> void
main() { int i,j,k,l,n;
system("cls");
printf("enter the range=");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{ printf(" ");
}
for(k=1;k<=i;k++)
{
printf("%d",k); } for(l=i-1;l>=1;l--)
{ printf("%d",l);
} printf("\n");
}
printf("\nThe output produced by SAKSHAM TYAGI");
getch();
}

OUTPUT :
Program-19

Write a c program to convert string from upper case to lower


case.
#include<stdio.h>
#include<conio.h> void
main ()
{ char
str[30]; int
i;
printf (" Enter the string: ");
scanf (" %s", &str); for ( i =
0; i <= strlen (str); i++)
{ if (str[i] >= 65 && str[i] <=
90) str[i] = str[i] + 32;
} printf (" \n Upper Case to Lower case string is: %s",
str); printf("\nThe output produced by SAKSHAM
TYAGI"); getch();
}

OUTPUT :
Program-20

Write a c program to Find length of a string without using


strlen() function.
#include<stdio.h>
#include<conio.h> void
main()
{ char s[] = "Programming is
fun"; int i;
for (i = 0; s[i] != '\0'; ++i); printf("Length
of the string: %d", i);
printf("\nThe output produced by SAKSHAM TYAGI");
getch();
}

OUTPUT :
Program-21

Object :- Write a c program to find the address of any given variable


using pointer.
#include<stdio.h>
#include<conio.h>
int main() { int a;
int *pt;
printf("Pointer Example Program : Print Pointer Address\n");
a = 10; pt = &a;
printf("\n[a ]:Value of A = %d", a); printf("\
n[*pt]:Value of A = %d", *pt); printf("\
n[&a ]:Address of A = %p", &a); printf("\
n[pt ]:Address of A = %p", pt); printf("\
n[&pt]:Address of pt = %p", &pt); printf("\
n[pt ]:Value of pt = %p", pt);
printf("\nThe output produced by SAKSHAM TYAGI");
getch();
}

OUTPUT :
Program-22

Object :- Write a c program to find the addition and multiplication of


any two-number using function.
#include<stdio.h> #include<conio.h>
int addition(int a, int b)
{ return a +
b;
} int multiplication(int a, int b)
{ return a * b;
} void main()
{ int num1,
num2;
printf("Enter the first and second number: ");
scanf("%d%d", &num1, &num2); int sum =
addition(num1, num2); int product =
multiplication(num1, num2); printf("Sum:
%d\n", sum); printf("Product: %d\n",
product);
printf("\nThe output produced by SAKSHAM TYAGI");
getch();
}

OUTPUT :
Program-23

Object :- Write a c program to insert five students record (roll_no,


name, address & total number) using structure.
#include<stdio.h>
#include<conio.h>
#define MAX_STUDENTS 5
struct Student
{ int roll_no; char
name[50]; char
address[100]; int
total_marks;
}; void
main() {
struct Student students[MAX_STUDENTS];
printf("Enter details for %d students:\n", MAX_STUDENTS);
for (int i = 0; i < MAX_STUDENTS; i++) { printf("\nStudent
%d\n", i + 1); printf("Enter roll number: "); scanf("%d",
&students[i].roll_no); printf("Enter name: "); scanf(" %[^\n]",
students[i].name); printf("Enter address: "); scanf(" %[^\n]",
students[i].address); printf("Enter total marks: "); scanf("%d",
&students[i].total_marks);
} printf("\nStudent records:\n"); for (int i
= 0; i < MAX_STUDENTS; i++)
{ printf("\nStudent %d\n", i + 1); printf("Roll
number: %d\n", students[i].roll_no);
printf("Name: %s\n", students[i].name);
printf("Address: %s\n", students[i].address);
printf("Total marks: %d\n",
students[i].total_marks);
}
printf("\nThe output produced by SAKSHAM TYAGI");
getch();
}

OUTPUT :
Program-24

Object :- Write a program to insert five books record (id, book


name, author name, price) using union.
#include<stdio.h>
#include<conio.h>
#define MAX_BOOKS 5
union BookInfo {
int id;
char book_name[50];
char
author_name[50];
float price;
}; void
main() {
union BookInfo books[MAX_BOOKS];
printf("Enter details for %d books:\n",
MAX_BOOKS); for (int i = 0; i < MAX_BOOKS;
i++) { printf("\nBook %d\n", i + 1); printf("Enter
book ID: "); scanf("%d", &books[i].id);
printf("Enter book name: "); scanf(" %[^\n]",
books[i].book_name); printf("Enter author name:
"); scanf(" %[^\n]", books[i].author_name);
printf("Enter book price: "); scanf("%f",
&books[i].price);
} printf("\nBook records:\n"); for
(int i = 0; i < MAX_BOOKS; i++)
{ printf("\nBook %d\n", i + 1);
printf("Book ID: %d\n",
books[i].id); printf("Book name:
%s\n", books[i].book_name);
printf("Author name: %s\n",
books[i].author_name);
printf("Book price: %.2f\n",
books[i].price);
}
printf("\nThe output produced by SAKSHAM TYAGI");
getch();
}

OUTPUT :
Program-25

Object :- Write a c program to open a file in write mode and


insert some text and display them.
#include<stdio
.h>
#include<coni
o.h> void
main() {
FILE*p;
char c;
p=fopen("[Link]","w");
char s[1000];
printf("Enter the string you want to write in file\
n"); gets(s); fprintf(p,"%s","r"); fclose(p);
p=fopen("[Link]","r");
while(!feof(p))
{ fscanf(p,"%c",&c);
printf("%c",c);
c='\0'; }
fclose(p);
printf("\nThe output produced by SAKSHAM TYAGI");
getch();
}
OUTPUT :

You might also like