D. H. B. Soni College, Solapur - 1
D. H. B. Soni College, Solapur - 1
D. H. B. Soni College, Solapur - 1
/* Program to accept 3 nos. add them and find out average. avg.c */
# include<stdio.h> // including header file
main()
{
int no1=0, no2=0,no3=0, total=0; // variables declaration and initialization
float average=1.0;
clrscr(); // function to clear the output screen
printf("Enter 3 numbers:");
scanf("%d %d %d", &no1, &no2,&no3 ); // accepting 3 numbers using single scanf.
total=no1+no2+no3
average=total/3; // Calculating average
printf("\n The total is %d and average is ", total, average); // Printing result
getch(); // function for the quick output screen
}
// Program to calculate the interest amount when interest is 11 % per year. intcal.c
# include<stdio.h> // including header file
# define int_rate 10
main()
{
float principle=1.0 , int_amt=1.0 ; // variables are initially 1.0
int period=1;
clrscr(); // function to clear the output screen
printf("Enter Principle amount and period(years)");
scanf("%f %d ", &principle,& period ); // accepting principle and period
int_amt= (principle * int_rate/100)* period; // formula
printf("\n The Interest amount is %f", int_amt); // Printing result
getch(); // function for the quick output screen
}
/* Calculate total sales and & commission for the sales man as 7% of sales Sales.c */
# include<stdio.h>
const int comm=7;
main()
{
float tot_sale=1.0, comm_amt=1.0, rate=1.0;
int qty=1;
clrscr();
printf("Enter quantity and item rate:");
scanf("%d %f ", &qty, &rate);
tot_sale=qty*rate;
comm_amt=tot_sale* comm/100;
printf("\n Items=%d \t rate=%f \t Total sales=%f commission =%f", qty, rate,
tot_sale,comm_amt);
getch();
}
/* To display the entered number with trailing zeros and leading zeros prnzero.c
*/
# include<stdio.h>
void main()
{
long int num=0;
clrscr();
printf("Enter number:");
scanf("%d",&num);
printf("\n %05d",num);
printf("\n %5d",num*1000);
getch();
}
D. H. B. Soni College, Solapur - 3 -
Programming in C
/* To display entered numbers with right justification and left justification justify.c */
# include<stdio.h>
void main()
{
int num=0;
printf("Enter number");
scanf("%d", &num);
printf("The number(R): %d",num);
printf("\nThe number(L):%-d",num);
getch();
}
/* Accept character form user and check whether it is vowel or consonant. char.c
*/
# include<stdio.h>
void main()
{
char code;
clrscr();
printf("Enter a single character:");
scanf("%c", &code);
D. H. B. Soni College, Solapur - 5 -
Programming in C
/* Accept first character of designation form user and display the post or designation.
desig.c */
# include<stdio.h>
void main()
{
char code;
clrscr();
printf("\n\t Designation\t character");
printf("\n\t Manager\t -m");
printf("\n\t supervisor\t-s");
printf("\n\t Clerk\t -c");
printf("\n\t Worker\t -w");
printf("\n Enter designation code:" );
scanf("%c", &code);
if (code=='m') // checking for manager designation
{
printf("\n The designation is manager");
}
else if (code=='s') // else checking for supervisor designation
{
printf("\n The designation is supervisor");
}
else if (code=='c') // else checking for clerk designation
{
printf("\n The designation is clerk");
}
else if (code=='w') // else checking for worker designation
{
printf("\n The designation is worker");
}
else //else the character entered is invalid
{
printf("\n Invalid character entered");
}
getch();
}
/* display menu for operation and accept choice number from user dispmenu.c */
# include<stdio.h>
void main()
{
int num=0, choice=0;
clrscr();
printf("\n\t 1.Square");
printf("\n\t 2. Cube");
printf("\n\t 3. Octal");
printf("\n\t 4.Hexadecimal");
printf("\n Enter number 1/2/3/4 for your choice");
scanf("%d", &choice);
printf("\n Enter number:" );
scanf("%d", &num);
if (choice==1) // checking for square of number operation
{
printf("\n The square of a number is %d", num*num);
}
else if (choice==2) // else checking for cube of a number operation
{
printf("\n The cube is %d", num*num*num);
}
else if (choice==3) // else checking for printing equivalent octal
number
{
printf("\n Equivalent octal of %d is %o",num ,num);
}
else if (choice==4) // else checking for printing equivalent hexadecimal
number
{
printf("\n Equivalent hexadecimal of %d is %x ",num, num);
}
else //else the character entered is invalid
{
printf("\n Choice is Invalid ");
}
getch();
}
/* Accept 3 numbers from user and find out the greatest number gcomp3.c */
# include<stdio.h>
void main()
{
int n1=0, n2=0,n3=0;
clrscr();
printf("\n Enter 3 numbers to compare");
scanf("%d %d %d", &n1,&n2,&n3);
/* Accept 3 numbers from user and find out the smallest number scomp3.c */
# include<stdio.h>
void main()
{
int n1=0, n2=0,n3=0;
clrscr();
printf("\n Enter 3 numbers to compare");
scanf("%d %d %d", &n1,&n2,&n3);
}
else
{
printf("\n Entered marks are invalid"); //checking for wrong input
}
getch();
}
{
case 'a':
case 'i':
case 'o':
case 'u':
printf("%c is vowel", code);
break;
default:
printf("The %c is not vowel", code);
break;
}
getch();
}
/* Program to accept the type of travel and display fare for that type. Fare.c*/
# include<stdio.h>
main()
{
char type;
printf("\n\t Traveling details:");
printf("\n Bus (b)");
printf("\n Train(t)");
break;
case 9:
printf("\n Nine");
break;
case 10:
printf("\n Ten");
break;
default:
printf("\n Invalid number");
break;
}
getch();
}
while (i<=10)
{
printf("\n %d ",i); //printing value of i
i++; // value of i will be increased by 1.
}
getch();
}
/* program to count number of digits and print the reverse of a number revno.c */
# include<stdio.h>
void main()
{
int no=0, rem=0,cnt=0;
printf("Enter number:");
scanf("%d", &no);
printf("\n Reverse of a number"); // Printing message
while (no!=0) // While number does not become zero
{
rem=no%10; // rem is remainder of no/10 due to this we get last digit.
cnt++; // when last digit is received the counter counts it
printf("%d",rem); // printing the remainder i.e. last digit.
no=no/10; // dividing the last digits to get one by digits
}
printf("\n The number of digits are %d", cnt);
getch();
}
/* accept 5 numbers from user and sum them, if user enters 100 exit the program */
# include<stdio.h>
void main()
{
int no=0, i=1,sum=0;
clrscr();
while(i<=5) // condition for accepting five numbers i.e. till I becomes 5
{
printf("\n Enter number:");
scanf("%d", &no);
if (no==100) // if accepted number is 100 .
{
break; // the control gets break and it comes out of loop
}
sum=sum + no;
i++;
}
printf("The sum of is %d", sum); // the final sum of all the numbers.
getch();
}
/* Accept characters from the user, if user enters ‘x ‘ stop accepting characters
wchar.c */
# include<stdio.h>
main()
{
char code='a';
clrscr();
while (code!='x')
{
printf("\nEnter character (to exit type x):");
code= getch();
printf("\n You have entered %c", code);
}
printf("\nYou have exited");
getch();
}
# include<stdio.h>
void main()
{
int i=1;
do // beginning of the do loop
{
if (i%7==0) // finding multiple of 7
{
printf("\n %d", i); // printing only multiples of 7
}
i++;
}while(i<=50);
getch();
}
/*printing series of 2,6,12,20,30,42 using for loop. Accept position from user.
fseries .c */
# include<stdio.h>
void main()
{
int i=0, pos=0;
clrscr();
printf("\n Enter last position ");
scanf("%d", &pos);
for(i=1;i<=pos; i++) // The loop starts with 1 and iterates till the position entered by user
{
printf("\t %d",(i*i) + i);
}
getch();
}
for(j=1;j<=i;j++)
{
printf("*");
}
}
getch();
}
for(j=1;j<=i;j++)
{
printf("%d",cnt );
cnt++;
}
}
getch();
}
/* Print the series of factorial numbers e.g. 1, 2, 6, 2 4,120, 720 …..n factser1.c */
# include<stdio.h>
void main()
{
int i=0, j=0, pos=0;
long int fact=1;
clrscr();
printf("Enter last position");
scanf("%d", &pos);
for(i=1;i<=pos ; i++)
{
printf("\t");
for(j=1;j<=i;j++)
{
fact=fact*j;
}
printf("%ld" ,fact );
fact=1;
}
getch();
}
}
if (number[i]>0)
{
cnt3++;
}
}
printf("\n Number of Negative numbers= %d", cnt1);
printf("\n Number of zeros=%d", cnt2);
printf("\n Number of Positive numbers= %d",cnt3);
getch();
}
clrscr();
for(i=0;i<=1;i++)
{
printf("\n");
for(j=0;j<=1;j++)
{
printf("Enter arr{%d][%d]:\t", i,j);
scanf("%d", &arr1[i][j]);
}
}
for(i=0;i<=1;i++)
{
printf("\n");
for(j=0;j<=1;j++)
{
printf("Enter arr{%d][%d]:\t", i,j);
scanf("%d", &arr2[i][j]);
}
}
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
arradd[i][j]=arr1[i][j]+arr2[i][j];
arrsub[i][j]=arr1[i][j]-arr2[i][j];
}
}
printf("\n Addition of two matrices\n\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf("arradd[%d[%d]=%d\t",i,j,arradd[i][j]);
}
printf("\n");
}
printf("\nAddition of two matrices\n\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf("arradd[%d[%d]=%d\t",i,j,arrsub[i][j]);
}
printf("\n");
}
getch();
}
{
printf("\n Enter element");
scanf("%d", &arr1[i][j]);
}
printf("\n");
}
printf("\n Enter elements for second array:");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf("\n Enter element");
scanf("%d", &arr2[i][j]);
}
printf("\n");
}
arr3[0][0]=arr1[0][0]*arr2[0][0]+arr1[0][1]*arr2[1][0];
arr3[0][1]=arr1[0][0]*arr2[0][1]+arr1[0][1]*arr2[1][1];
arr3[1][0]=arr1[1][0]*arr2[0][0]+arr1[1][1]*arr2[1][0];
arr3[1][1]=arr1[1][0]*arr2[0][1]+arr1[1][1]*arr2[1][1];
printf("\n Matrix multiplication is:");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf("\n%d\t", arr3[i][j]);
}
printf("\n");
}
getch();
}
else
{
printf("%s is not palindrom",str);
}
getch();
}
for(i=0;i<=4;i++)
{
printf("\n Enter string:");
scanf("%s", fname[i]);
}
printf("\n List of students");
for (i=0;i<=4;i++)
{
printf("\n %s", fname[i]);
}
getch();
}
void fun2()
{
int i=50;
printf("\n %d");
}
# include<stdio.h>
void demo(void);
main()
{
int i=1;
clrscr();
for (i=1;i<=3;i++)
{
demo();
}
getch();
}
void demo()
{
int static s=0;
s=s+1;
printf("\n %d",s);
}
# include<stdio.h>
void main()
{
register int i=1;
clrscr();
for (i=1;i<=3;i++)
{
printf("\n Welcome to Tech-Max Publications");
}
getch();
}
number=number/2;
}
i--;
for(j=i;j>=0;j--)
{
printf("%d",arr[j]);
}
}
//progrm to print the square of numbers till the position entered by user.
# include<stdio.h>
void sqr_prn(int);
main()
{
int pos=0;
printf("\nEnter last position");
scanf("%d", &pos);
sqr_prn(pos);
getch();
}
void sqr_prn(int x)
{
int i=1;
for(i=1;i<=x;i++)
{
printf("%d\t", i*i);
}
}
scanf("%s",str1);
printf("\n Enter second string");
scanf("%s",str2);
printf("\nBefore exchange first string= %s & second string=%s", str1, str2);
exchange(str1,str2);
getch();
}
void exchange(char s1[], char s2[])
{
char temp[10];
int i=0;
for(i=1;i<=9;i++)
{
temp[i]=s1[i];
}
for(i=1;i<=9;i++)
{
s1[i]=s2[i];
}
for(i=1;i<=9;i++)
{
s2[i]=temp[i];
}
printf("\nAfter exchange first string= %s & second string=%s", s1, s2);
}
// program to find out greates number from given matrix using function.
# include<stdio.h>
int matgreat();
void main()
{
clrscr();
printf("\n Greatest number in matrix=%d", matgreat());
getch();
}
int matgreat()
{
int i=0,j=0,mat[2][2],gno=0;
for(i=0;i<=1;i++)
{
printf("\n");
for(j=0;j<=1;j++)
{
printf("Enter[%d][%d]element:",i,j);
scanf("%d", &mat[i][j]);
}
}
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
if (gno<mat[i][j])
{
gno=mat[i][j];
}
}
}
return gno;
}
scanf("%d", &no2);
pt1=&no1;
pt2=&no2;
printf("\n %d * %d=%d ",*pt1,*pt2, (*pt1)*(*pt2));
getch();
}
// Pointer arithmetic
# include<stdio.h>
main()
{
int a=0, b=0;
int *p1, *p2;
clrscr();
printf("\n Enter first number:");
scanf("%d", &a);
printf("\n Enter second number:");
scanf("%d", &b);
p1=&a;
p2=&b;
printf("\n %d is at %u and %d is at %u", a,p1,b,p2);
printf("\n p1-p2= %d " , p1-p2);
printf("\n *p1 + *p2=%d ", *p1 + *p2);
printf("\n *p1*5= %d", *p1*5);
printf("\n*p1/*p2= %d", *p1/ *p2);
printf("\n *p1/5= %d", *p1/5);
printf("\n p1++ =%u",++p1);
getch();
}
clrscr();
for(i=0;i<=4;i++)
{
printf("Enter numbers:");
scanf("%d", &arr[i]);
}
ptr=&arr[0];
i=1;
while (i<=5)
{
printf("\n%d is stored at %u", *ptr, ptr);
ptr++;
i++;
}
getch();
}
// demo of structure
# include<stdio.h>
void main()
{
struct movie
{
char movie_nm[20];
char actor[15];
int rel_year;
};
struct movie *ptr;
clrscr();
printf("\n Enter movie_nm:");
scanf("%s", ptr->movie_nm);
printf("\n Enter movie actor:");
scanf("%s", ptr->actor);
printf("\n Enter release year:");
scanf("%d",&ptr->rel_year);
printf("\n Movie name - %s", ptr->movie_nm);
printf("\n Actor-%s",ptr->actor);
printf("\n Release year-%d",ptr->rel_year);
getch();
}