0% found this document useful (0 votes)
184 views50 pages

1) - Program To Find The Sum and Average of 10 Numbers: Output 1

The document contains 22 C programming code examples with outputs, including programs to: 1) Find the sum and average of 10 numbers 2) Find the greatest of 10 numbers 3) Calculate simple interest 4) Draw a pattern of stars 5) Check if a number is prime

Uploaded by

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

1) - Program To Find The Sum and Average of 10 Numbers: Output 1

The document contains 22 C programming code examples with outputs, including programs to: 1) Find the sum and average of 10 numbers 2) Find the greatest of 10 numbers 3) Calculate simple interest 4) Draw a pattern of stars 5) Check if a number is prime

Uploaded by

Ashish Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

1).

Program to find the sum and average of 10 numbers

#include<stdio.h>
#include<conio.h>
void main()
{
int a,i,sum=0;
float average=0;
clrscr();
for(i=0;i<10;i++)
{
printf(“Enter A Number[%d] : ”,i);
scanf(“%d”,&a);
sum = sum+a;
}
printf(“sum =%d\n”,sum);
average=sum/10.0;
printf(“Average =%f\n”,average);

getch();

OUTPUT 1 :
2). Program to find the greatest of 10 numbers

#include <stdio.h>
#include<conio.h>
void main()
{
int a[10],i,greatest;
clrscr();
printf("Enter Ten Values : ");
for (i = 0; i < 10; i++)
{ scanf("%d", &a[i]); }
greatest = a[0];
for (i = 0; i < 10; i++)
{
if (a[i] > greatest)
{ greatest = a[i]; }
}
printf(" Greatest Of Ten Numbers Is : %d", greatest);
getch();
}

OUTPUT 2 :
3). Program to find Simple Interest

#include<stdio.h>
#include<conio.h>
void main()
{
int p,n;
float r,si;
clrscr();
printf("\nEnter Values of P,N, and R : ");
scanf("%d%d%f",&p,&n,&r);
si=p*n*r/100;
printf("Simple Interest = Rs. %f",si);
getch();
}

OUTPUT 3 :
4). Program to draw the pattern of stars
#include<stdio.h>
#include<conio.h>
void main()
{
int r,s,i,j,k;
clrscr();
printf("Enter The Number Of Rows : ");
scanf("%d",&r);
s=r;
for(i=0;i<r;i++)
{
for( j=0;j<s;j++)
{ printf(" "); }
for(k=0;k<=(2*i);k++)
{ printf("*"); }
s--;
printf("\n");
}
getch();
}

OUTPUT 4 :
5). Program to find whether the entered number is prime

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i=2,temp=0;
clrscr();
printf("Enter A Number : ");
scanf("%d",&n);
for(i=2;i<n;i++)
{
if(n%i==0)
{ temp=1; }
}
if(temp==1)
{ printf("The Number Is Not Prime !!"); }
else
{ printf("The Number Is Prime "); }
getch();
}

OUTPUT 5 :
6). Program to find the sum of a 5 digit number

#include<stdio.h>
#include<conio.h>
void main()
{
int a,rem,q,sum=0;
clrscr();
printf(“Enter A Number : ”);
scanf(“%d”,&a);
while(a>0)
{ rem=a%10;
sum=sum+rem;
a=a/10; }
printf(“Sum Of Digits = %d”,sum);
getch();
}

OUTPUT 6 :
7). Program to reverse a 5 digit number

#include<stdio.h>
#include<conio.h>
void main()
{
int n,reverse=0;
clrscr();
printf("Enter A Number = ");
scanf("%d" , &n);
while(n!=0)
{ reverse=reverse*10;
reverse=reverse+n%10;
n=n/10; }
printf("Reverse Of Entered Number = %d",reverse);
getch();
}

OUTPUT 7 :
8). Program to convert decimal to binary and vice versa

#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
int n,i,a[20],k=0,ch,dec=1;
long int j;
clrscr();
printf("MENU : \n1. For Decimal To Binary and \n2. For Binary To Decimal\n");
scanf("%d",&ch);
if(ch==1)
{
printf("\nEnter The Decimal Number : \n");
scanf("%d",&n);
for(i=n;i>0;i=i/2) a[k++]=i%2;
printf("\nThe Binary Form Of The Number Is : ");
for(i=k;i>0;i--)
printf("%d",a[i]);
}
else if(ch==2)
{ printf("\nEnter The Binary Number : \n");
scanf("%d",&j);
printf("\nThe Decimal Form Of The Number Is : \t"); for(i=j;i>0;i=i/10)
dec=dec+pow(2,(i%10));
printf("%d",dec);
}
else
{ printf("\nWrong choice!!\n"); }
getch();
}

OUTPUT 8 :
9). Write a program to implement switch case statement

#include<conio.h>
#include<stdio.h>
void main()
{
int i;
clrscr();
printf("Enter The Value Of I (1/2/3) : ");
scanf("%d" , &i);
switch (i)
{
case 1: printf("I Am Case 1 !! \n");
break;
case 2: printf("I Am Case 2 !!\n");
break;
case 3: printf("I Am Case 3 !!\n");
break;
default: printf("I Am Default!!");
}
getch();
}

OUTPUT 9 :
10). Program to generate the Fibonacci Sequence
#include<stdio.h>
#include<conio.h>
void main()
{
int n,first=0,second=1,next,c;
clrscr();
printf("Enter The Number Of Terms : ");
scanf("%d" , &n);
printf("First %d Terms Of Fibonacci Series Are : \n",n);
for(c=0;c<n;c++)
{ if(c<=1)
{ next=c; }
else
{ next=first+second;
first=second;
second=next; }
printf("%d\n" , next);
}
getch();
}

OUTPUT 10 :
11). Program to find exponential function
# include <stdio.h>
# include <conio.h>
void main()
{
int i, n, exp;
float x, sum = 1, t = 1 ;
clrscr() ;
printf("Enter The Value For x : ") ;
scanf("%f", &x) ;
printf("\nEnter The Value For n : ") ;
scanf("%d", &n) ;
for(i = 1 ; i < n + 1 ; i++)
{ exp = i ;
t = t * x / exp ;
sum = sum + t ; }
printf("\nExponential Value of %f Is : %f", x, sum) ;
getch() ;
}

OUTPUT 11 :
12). Program to search a number from an array using Linear
Search

#include<stdio.h>
#include<conio.h>

void main()
{
int array[100], search, c, number;
clrscr();
printf("Enter The number Of Elements In Array : ");
scanf("%d",&number);
printf("Enter %d Numbers : ", number);
for ( c = 0 ; c < number ; c++ )
scanf("%d",&array[c]);
printf("Enter The Number To Search : ");
scanf("%d",&search);
for ( c = 0 ; c < number ; c++ )
{
if ( array[c] == search )
{ printf("%d Is Present At Location %d.\n",
search, c+1);
break; }
}
if ( c == number )
printf("%d Is Not Present In Array.\n", search);
getch();
}
OUTPUT 12 :
13). Program to search a number from an array using Binary
Search
#include <stdio.h>
#include<conio.h>
void main()
{
int c, first, last, middle, n, search, array[100];
clrscr();
printf("Enter Number Of Elements : ");
scanf("%d",&n);
printf("Enter %d Integers : ", n);
for (c = 0; c < n; c++)
scanf("%d",&array[c]);
printf("Enter Value To Find : ");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search)
{
printf("%d Found At Location %d.\n", search, middle+1);
break; }
else last = middle - 1;
middle = (first + last)/2;}
if (first > last)
printf("Not Found! %d Isn't Present In The List.\n", search);
getch();
}

OUTPUT 13 :
14). Write a program to sort an array using bubble sort
#include<stdio.h>
#include<conio.h>
void main()
{ int a[10], i, j, t;
clrscr();
printf("\nEnter 10 Numbers : ");
for(i=0;i<10;i++)
{ scanf("%d", &a[i]); }
for(i=0;i<10;i++)
{ for(j=0;j<9;j++)
{ if(a[j]>a[j+1])
{ t=a[j];
a[j]=a[j+1];
a[j+1]=t; }
}
}
printf("\nSorted List Is : ");
for(i=0;i<10;i++)
{ printf("%d ", a[i]); }
getch();
}

OUTPUT 14 :
15). Write a program to sort an array using selection sort

#include<stdio.h>
#include<conio.h>
void main()
{ int a[10], i, j, t;
clrscr();
printf("\nEnter 10 Numbers : ");
for(i=0;i<10;i++)
{ scanf("%d", &a[i]); }
for(i=0;i<10;i++)
{ for(j=i+1;j<10;j++)
{ if(a[i]>a[j])
{ t=a[i];
a[i]=a[j];
a[j]=t; }
}
}
printf("\nSorted List Is : ");
for(i=0;i<10;i++)
{ printf("%d ", a[i]); }
getch();
}

OUTPUT 15 :
16). Program to sort an array using Insertion Sort

#include<stdio.h>
#include<conio.h>
void main()
{
int data[100],n,temp,i,j;
clrscr();
printf("Enter Number Of Terms(Should Be Less Than 100) : ");
scanf("%d",&n);
printf("Enter Elements : ");
for(i=0;i<n;i++)
{ scanf("%d",&data[i]); }
for(i=1;i<n;i++)
{ temp = data[i];
j=i-1;
while(temp<data[j] && j>=0)
{ data[j+1] = data[j];
--j;
}
data[j+1]=temp;
}
printf("In Ascending Order : ");
for(i=0; i<n; i++)
{ printf("%d\t",data[i]); }
getch();
}
OUTPUT 16 :
17). Program to find the factorial of a number using
recursion

#include<conio.h>
#include<stdio.h>
long fact(int i)
{
if(i==1)
return 1;
else
return(i*fact(i-1));
}
void main()
{
int i;
clrscr();
printf("\nEnter The Number To Find The Factorial : ");
scanf("%d",&i);
printf("\nThe Factorial Of The Given Number Is : %d",fact(i));
getch();
}

OUTPUT 17 :
18). Program to find the length of a string without using
strlen and then pass the string to characters.

#include<conio.h>
#include<stdio.h>
int slen(char b[25])
{
int i;
for(i=0;b[i]!='\0';i++);
return i;
}
void main()
{
char a[25];
int len;
clrscr();
printf("\nEnter A String : ");
scanf("%s",a);
len=slen(a);
printf("\nThe Length Of The String Is : %d",len);
getch();
}

OUTPUT 18 :
19).Program to count the number of vowels in a given string

#include<stdio.h>
#include<conio.h>
void main()
{
int i,ctr=0;
char a[50];
clrscr();
printf("Enter String : ");
gets(a);
for(i=0;a[i]!='\0';i++)
{
if(a[i]=='a'||a[i]=='e'||a[i]=='i'||a[i]=='o'||a[i]=='u'||a[i]=='A'||a[i]=='E'||a[i]=='I'||a[i]=='O'
||a[i]=='U')
ctr++;
}
printf("\nNumber Of Vowels : %d ",ctr);
getch();
}

OUTPUT 19 :
20). Program to check if a given string is a palindrome or not
#include <stdio.h>
#include<conio.h>
void main()
{
char text[100];
int begin, middle, end, length = 0;
clrscr();
printf("Enter The String : ");
gets(text);
while (text[length] != '\0')
{ length++; }
end = length - 1;
middle = length/2;
for (begin = 0; begin < middle; begin++)
{ if (text[begin] != text[end])
{ printf("Not A Palindrome \n");
break; }
end--; }
if(begin == middle)
{ printf("Palindrome \n"); }
getch();
}

OUTPUT 20 :
21). Program for String Concatenation

#include <stdio.h>
#include<conio.h>
void main()
{
char s1[100], s2[100], i, j;
clrscr();
printf("Enter First String : ");
scanf("%s", s1);
printf("Enter Second String : ");
scanf("%s", s2);
for(i = 0; s1[i] != '\0'; ++i);
for(j = 0; s2[j] != '\0'; ++j, ++i)
{
s1[i] = s2[j];
}
s1[i] = '\0';
printf("After Concatenation : %s", s1);
getch();
}

OUTPUT 21 :
22). Program to string comparison

#include<stdio.h>
#include<conio.h>
void main()
{
char str1[100],str2[100];
int i,j,k,sum=0,cmp=0;
clrscr();
printf("Enter String1 : ");
gets(str1);
printf("Enter String2 : ");
gets(str2);
for(j=0;str1[j]!='\0';++j) {}
for(k=0;str2[k]!='\0';++k) { }
if(j==k)
{ for(i=0;str1[i]!='\0';++i)
{ if(str1[i]==str2[i])
{ cmp=1; }
else { cmp=0; }
sum=sum+cmp;
}
}
if(sum==j)
{ puts("\nSame Strings !!"); }
else
{ puts("\nDifferent Strings !!"); }
getch();
}
OUTPUT 22 :
23). Program to string reverse

#include <stdio.h>
#include<conio.h>
void main()
{ char s[100], r[100];
int i,j,count=0;
clrscr();
printf("Enter A String :\n");
gets(s);
while (s[count] != '\0')
{ count++; }
j=count-1;
for(i= 0;i<count;i++)
{ r[i] = s[j];
j--; }
r[i] = '\0';
printf("The Reversed String Is : %s\n", r);
getch();
}

OUTPUT 23:
24). Program to convert a string from lower case to upper
case and vice versa

#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{ int i;
char str[20];
clrscr();
printf("Enter Any String : ");
scanf("%s",str);
printf("The String Is : %s",str);
for(i=0;i<=strlen(str);i++)
{ if(str[i]>=97&&str[i]<=122)
str[i]=str[i]-32; }
printf("\nThe String In Uppercase Is : %s",str);
getch();
}

OUTPUT 24 :
25). Write a program for the addition of two 3*3 matrices

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,p[3][3],q[3][3],r[3][3];
clrscr();
printf("Enter The Elements Of The First Matrix : \n"); for(i=0;i<3;i++)
for(j=0;j<3;j++)
{ scanf("%d",&p[i][j]); }
printf("Enter The Elements Of The Second Matrix : \n");
for(i=0;i<3;i++) for(j=0;j<3;j++)
{ scanf("%d",&q[i][j]); }
for(i=0;i<3;i++) for(j=0;j<3;j++)
{ r[i][j]=p[i][j]+q[i][j]; }
printf("Sum Of Entered Matrices :\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%d\t",r[i][j]);
printf("\n");
}
getch();
}
OUTPUT 25 :
26). Program to multiply two 3 by 3 matrices

#include<stdio.h>
#include<conio.h>
void main()
{
int p[3][3],q[3][3],r[3][3],i,j,k=0;
clrscr();
printf("\nEnter The Elements Of Matrix1 : \n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&p[i][j]);
printf("\nEnter The Elements Of Matrix2 :\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&q[i][j]);
for(i=0;i<3;i++)
{ for(j=0;j<3;j++)
{ r[i][j]=0;
for(k=0;k<3;k++)
{ r[i][j]=r[i][j]+p[i][j]*q[k][j]; }
}
printf("\n The Multiplication Of The Two Matrices Is : \n");
for(i=0;i<3;i++)
{ for(j=0;j<3;j++)
printf("%d\t",r[i][j]);
printf("\n"); }
getch();
}
OUTPUT 26 :
27). Program to swap two numbers using pointers
#include<stdio.h>
#include<conio.h>
void swap(int *,int *);
void main()
{
int a,b;
clrscr();
printf("Enter A : ");
scanf("%d",&a);
printf("Enter B : ");
scanf("%d",&b);
swap(&a,&b);
printf("\nAfter Swap :-\nA : %d",a);
printf("\nB : %d",b);
getch();
}
void swap(int *x,int *y)
{ int *temp;
temp=*x;
*x=*y;
*y=temp;
}

OUTPUT 27 :
28). Program to generate the employee details using
structure

#include<conio.h>
#include<stdio.h>
struct employee
{
char name[25],dept[25];
int id;
float salary;
};
void main()
{
struct employee emp;
clrscr();
printf("\nEnter The Name Of Employee : "); scanf("%s",emp.name);
printf("\nEnter The Department Of Employee : "); scanf("%s",emp.dept);
rintf("\nEnter The ID Of Employee : ");
scanf("%d",&emp.id);
printf("\nEnter The Salary Of Employee : "); scanf("%f",&emp.salary);
printf("\nThe Details Are As Follows :- ");
printf("\nName : %s",emp.name); printf("\nDepartment : %s",emp.dept);
printf("\nEmplyee ID No. : %d",emp.id);
printf("\nSalary : %f",emp.salary);
getch();
}
OUTPUT 28 :
29). Program to find the area and perimeter of a circle,
rectangle, square and triangle using functions

#include<conio.h>
#include<stdio.h>
#include<math.h>
float ar,per;
void circle()
{
float r;
printf("\nEnter The Radius Of Circle :");
scanf("%d",&r);
ar=3.14*r*r;
per=2*3.14*r;
printf("\nThe Area Of Circle Is : %f and \nIts Perimeter Is %f",ar,per);
}

void rectangle()
{
float a,b;
printf("\nEnter The Sides Of Rectangle : ");
scanf("%f %f", &a, &b);
ar=a*b; per=2*(a+b);
printf("\nThe Area Of Rectangle Is : %f and \nIts Perimeter Is : %f",ar,per);
}
void square()
{
float a;
printf("\nEnter The Side Of Square : ");
scanf("%f",&a);
ar=a*a; per=4*a;
printf("\nThe Area Of Square Is : %fand \nIts Perimeter Is : %f",ar,per);
}
void triangle()
{
float a,b,c,s;
printf("\nEnter The Sides Of Triangle : "); scanf("%f%f%f",&a,&b,&c);
per=a+b+c;
s=per/2;
ar=sqrt(s*(s-a)*(s-b)*(s-c));
printf("\nThe Area Of Triangle Is : %fand \n Its Perimeter Is : %f",ar,per);
}
void main()
{
int i;
clrscr();
printf("\nMENU : \n1.Circle\n2.Rectangle\n3.Square\n4.Triangle\n\n");
scanf("%d",&i);
switch(i)
{
case 1: circle();
break;
case 2: rectangle();
break;
case 3: square();
break;
case 4: triangle();
break;
default: printf("\nWrong Choice !!!\n");
}
getch();
}

OUTPUT 29 :
30). Program to pass and return pointer to function hence
calculate average of an array

#include<stdio.h>
#include<conio.h>
void addp(float *);
void main()
{
float a[5],i,*avg=0;
clrscr();
printf("\nEnter 5 Elements of Array : ");
for(i=0;i<5;i++)
{
scanf("%f",&a[i]);
}
addp(&a);
printf("\nThe Average Is : %f",*avg);
getch();
}
void addp(float *ptr)
{
float i,*avg;
for(i=0;i<5;i++)
{ *avg = *avg + *ptr;
ptr++; }
*avg=*avg/5;
return avg;
}
OUTPUT 30 :
31). Program to pass an array as pointer to a function that
calculates the sum of all elements of the array

#include<conio.h>
#include<stdio.h>
void call(int arr[10],int n)
{
int *ptr,sum=0,i=0;
ptr=arr;
for(i=0;i<n;i++)
{ sum+=*ptr;
ptr++;
}
printf("\n Sum Of All The Elements Of The Array Is : %d",sum);
}
void main()
{
int a[10],size,i;
clrscr();
printf("Enter The Size Of The Array : ");
scanf("%d",&size);
printf("\nEnter Your Array : \n");
for(i=0;i<size;i++)
scanf("%d",&a[i]);
call(a,size);
getch();
}
OUTPUT 31 :
32). Program to demonstrate the example of array of
pointers

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,list[5],*ptr[5];
clrscr();
printf("Enter Five Numbers : ");
for(i=0;i<5;i++)
{
scanf("%d",&list[i]);
ptr[i]=&list[i];
}
printf("The Entered Numbers Are : ");
for(j=0;j<5;j++)
{ printf("%d\n",*ptr[j]); }
getch();
}

OUTPUT 32 :
33). Program to create a file called emp.txt and store
information about a person , in terms of his name , age
and salary

#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fptr;
char name[20];
int age;
float salary;
clrscr();
fptr = fopen("Emp.txt", "w");
if (fptr == NULL)
{ printf("File Does Not Exist!! \n"); }
printf("Enter The Name:\n");
scanf("%s", name);
fprintf(fptr, "Name = %s\n", name);
printf("Enter The Age:\n");
scanf("%d", &age);
fprintf(fptr, "Age = %d\n", age);
printf("Enter The Salary:\n");
scanf("%f", &salary);
fprintf(fptr, "Salary = %f\n", salary);
fclose(fptr);
getch();
}
OUTPUT 33 :
34). Program which copies one file contents to another file

#include<stdio.h>
#include<conio.h>
void main( )
{ FILE *fs,*ft ;
char ch;
clrscr();
fs = fopen( "pr1.txt", "r" ) ;
if (fs == NULL)
{ puts ( "Cannot open source file") ;
exit() ; }
ft = fopen ( "pr2.txt", "w" ) ;
if ( ft == NULL )
{ puts("Cannot open target file");
fclose(fs);
exit();
}
while(1)
{ ch=fgetc(fs);
if(ch==EOF)
break;
else
fputc(ch,ft); }
fclose (fs) ;
fclose (ft) ;
printf(“Program Successful !! ”);
getch();
}
OUTPUT 34 :
35). Program to read a file and after converting all lower
case to upper case letters write it to another file

#include<conio.h>
#include<stdio.h>
void main()
{ FILE *fp,*fr;
fp=fopen("pr1.txt","r");
fr=fopen("pr2.txt","w");
while(1)
{ ch=fgetc(fp);
if(ch==EOF)
break;
if(ch>97||ch<=122)
ch=ch-32;
fputc(ch,fr);
}
fclose(fp);
fclose(fr);
printf(“Program Successful !!”);
getch();
}

OUTPUT 35 :
36). Program to find the size of a file

include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
long size;
clrscr();
fp=fopen("a.txt", "r");
if (fp == NULL)
{ printf("File Not Found!\n"); }
else
{
fseek(fp, 0,SEEK_END);
size=ftell(fp);
printf("The Given File's Size Is : %ld Bytes",size);
}
fclose(fp);
getch();
}

OUTPUT 36 :

You might also like