What is 5 % 3=?
That is 5/3.If we modify the above program like this the output will be 5 is NOT dividable by 3,
Because the reminder is not Zero.
Problem 3
Write a program to print the square and cube of given integer number.
/******************
*Problem Number 3*
******************
#include<stdio.h>
void main(void)
{
int N,sq,cube;
clrscr();
printf("Enter an integer Number: \n");
scanf("%d",&N);
sq=N*N;
cube=N*N*N;
printf("%d * %d= %d\n",N,N,sq);
printf("%d * %d * %d= %d\n",N,N,N,cube);
getch();
}
Output of the program
When the user enter an integer number 12 the sq is 144 and the cube is 1728.
Problem 4
Write a program to determine the following sum
S=1+2+3+4+5+6+7+8+9+10=?
/******************
*Problem Number 4*
******************
#include<stdio.h>
void main()
{
int i;
int s=0;
clrscr();
i=1;
do
{
s=s+i;
i=i+1;
}while(i<=10);
printf("The Sum=%d\n",s);
getch();
}
Output of the program
Modify the program to determine the Sum
S==1+2+……..+25=?
/******************
*Problem Number 4*
******************
#include<stdio.h>
void main()
{
int i;
int s=0;
clrscr();
i=1;
do
{
s=s+i;
i=i+1;
}while(i<=25);
printf("The Sum=%d\n",s);
getch();
}
Output of the program
Modify the program to determine the Sum
S==5+6+………..+20=?
/******************
*Problem Number 4*
******************
#include<stdio.h>
void main()
{
int i;
int s=0;
clrscr();
i=5;
do
{
s=s+i;
i=i+1;
}while(i<=20);
printf("The Sum=%d\n",s);
getch();
}
Output of the program
Modify the program to determine the following sum for a given value of integer N
S=
/******************
*Problem Number 4*
******************
#include<stdio.h>
void main()
{
int I,N;
int s=0;
clrscr();
printf("Enter an integer N: \n");
scanf("%d",&N);
i=1;
do
{
s=s+i;
i=i+1;
}while(i<=N);
printf("The Sum=%d\n",s);
getch();
}
Output of the program
In the above program user enters 34 instead of N,so the sum is 1+2+………..+34=595.
Modify the program to determine the following sum for a given value of integers of N1and N2.
S=
/******************
*Problem Number 4*
******************
#include<stdio.h>
void main()
{
int I,N1,N2;
int s=0;
clrscr();
printf("Enter an integer N1: \n");
scanf("%d",&N1);
printf("Enter an integer N2 greater than N1: \n");
scanf("%d",&N2);
i=N1;
do
{
s=s+i;
i=i+1;
}while(i<=N2);
printf("The Sum=%d\n",s);
getch();
}
Output of the program
In the above program user enters 10 instead of N1 and enters 30 instead of N2,so the sum is
10+11+………..+30=420.
Write a program to continuously calculate the average of two numbers until the user requests to end.
/******************
*Problem Number 5*
******************
#include<stdio.h>
#include<string.h>
#include<graphics.h>
void main()
{
int N1,N2;
float Av;
char c;
do
{
clrscr();
textcolor(CYAN);
cprintf("Enter an inter Number:");
scanf("%d",&N1);
cprintf("Enter another integer Number:");
scanf("%d",&N2);
Av=(float)(N1+N2)/2;
textcolor(GREEN);
cprintf("The Average of Numbers %d and %d=%f",N1,N2,Av);
gotoxy(20,22);
textcolor(YELLOW);
cprintf("Would you like to try again? <Y/N>:");
c=getch();
c=toupper(c);
}
while(c=='Y');
textcolor(LIGHTGRAY);
}
Output of the program
What are the functions of:
It gets the characters on the screen what the program has to execute.
It determines the location of the particular character/sentence on the screen, it means that the space
and line number.
Section B: Writing Simple Programs
Problem 1
Write a program to print integer numbers from 10 down to 1 in steps of 1 on the screen.
#include<stdio.h>
void main(void)
{
int a=10;
clrscr();
do
{
printf("%d\n",a);
a=a-1;
}while(a>=1);
getch();
}
Output of the program
Problem 2
Write a program to print integer numbers from 20 down to 0 in steps of 2 on the screen.
#include<stdio.h>
void main(void)
{
int a=20;
clrscr();
do
{
printf("%d\n",a);
a=a-2;
}while(a>=0);
getch();
}
Output of the program
Problem 3
Write a program to print integer numbers from N1down to N2 in steps of N3 on the screen.
#include<stdio.h>
void main(void)
{
int N1,N2,N3;
clrscr();
printf("Enter an integer Number N1:");
scanf("%d",&N1);
printf("Enter an integer Number N2 less than N1:");
scanf("%d",&N2);
printf("Enter an integer Number N3 less than N2:");
scanf("%d",&N3);
do
{
printf("\n%d\n",N1);
N1=N1-N3;
}while(N1>=N2);
getch();
}
Output of the program
When the user enters 110,30 &10 instead of N1,N2 &N3 the above integer numbers are printed.
Problem 4
Write a program to convert ‘meters ‘ into centimeters.
#include<stdio.h >
void main (void)
{
float m,c;
clrscr();
printf("Enter a number you like to convert m to cm : ");
scanf("%f",&m);
(c=100*m);
printf("\n\n%.2f m (meters) = %.2f cm (centimeters)\n",m,c);
getch();
}
Output of the program
Problem 5
Write a program which determines the factorial of a given integer number.
#include <stdio.h>
void main(void)
{
int N,,F,I;
clrscr();
printf("Enter an inter number:");
scanf("%d",&N);
F=1;
I=1;
do
{
F=F*I;
I=I+1;
}while(I<=N);
printf("\n\nThe factorial of %d=%d \n",N,F );
getch();
}
Output of the program
Problem 6
Consider the following program. Develop a flowchart & briefly explain the functional behavior of
the program.
#include<stdio.h>
#include<stdlib.h>
void main()
{
int N=1,R,I,E,M;
clrscr();
do
{
E=0;
M=1;
do
{
M=M+1;
R=N%M;
if(R==0) E=1;
}while(M<=N/2);
if(E==0) printf("%d\n",N);
N=N+1;
}while(N<=41);
getch();
}
Output of the program
Flowchart for the above program.
R is the reminder ,when R=0 it stores E=1 and do the same loop until the condition (M<=N/2).
If R=0 it prints the N ,then increase the and do the same loop until the condition (N<=41).
Problem 7
The following program genegrates 10 random between 1 and 20.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define MAX 10
int main ()
{
int i=0,N;
clrscr();
randomize();
while(i<MAX)
{
N=random(20)+1;
printf("%d\n",N);
i++;
}
getch();
}
Output of the program
Tasks:
Modify the program to print 5 random numbers between 1 and 20.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define MAX 5
int main ()
{
int i=0,N;
clrscr();
randomize();
while(i<MAX)
{
N=random(20)+1;
printf("%d\n",N);
i++;
}
getch();
}
Output of the program
Modify the program to print 20random numbers between 1 and 49.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define MAX 20
int main ()
{
int i=0,N;
clrscr();
randomize();
while(i<MAX)
{
N=random(49)+1;
printf("%d\n",N);
i++;
}
getch();
}
Output of the program
Problem 8
The program generates 10 random numbers between 1 and 10.If the number is between 1 and 5
then we translate as ‘HEADS’ otherwise ‘TAILS’.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void main(void)
{
int N;
int I=0;
int h=0;
int t=0;
clrscr();
randomize();
do
{
N=rand()%(10-1+1)+1;
printf("%d\n",N);
if(N<5)h++;
else t++;
I++;
}
while(I<10);
printf("\nHEADS=%d\nTAILS=%d\n",h,t);
getch();
}
Output of the program
Problem 9
Write a program to toss a coin 100 times and print the number HEADS and TAILS at the end of the
program.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void main(void)
{
int N;
int I=0;
int h=0;
int t=0;
clrscr();
randomize();
do
{
N=rand()%(100-1+1)+1;
if(N<50)h++;
else t++;
I++;
}
while(I<100);
printf("\nHEADS=%d\nTAILS=%d\n",h,t);
getch();
}
Output of the program
Problem 9
Write a program to generate N random numbers between N1 and N2.The user should be asked for enter N,N1,N2.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define l N1
#define h N2
void main (void)
{
int N1,N2,N,n,i=0;
clrscr();
printf("Enter an integer number N1: ");
scanf("%d",&N1);
printf("\nEnter an integer number N2 greater than N1: ");
scanf("%d",&N2);
printf("\nEnter an integer number you like to print on the screen between N1 and N2: ");
scanf("%d",&N);
randomize();
do
{
n=rand()%(h-l+1)+l ;
printf("\n%d\n",n);
i++;
}
while(i<N);
getch();
}
Output of the program
Problem 10
This program ,we design a simple 4-function calculator.
#include<stdio.h>
void main()
{
int a,b;
float S;
char op;
clrscr();
printf("Enter Expression as <a><op><b>=<Enter>:\n");
scanf("%d%c%d",&a,&op,&b);
switch(op)
{
case '+':
{
S=a+b;
break;
}
case '-':
{
S=a-b;
break;
}
case '/':
{
if(b==0)
{
printf("Divide By Zero Error!\n");
getch();
return;
}
else
{
S=a/b;
}
break;
}
case'*':
{
S=a*b;
break;
}
default:
{
printf("Operation Unknown!\n");
break;
}
}
printf("\n\nResult: %d %c %d = %.2f \n",a,op,b,S);
getch();
}
Output of the program(operation ‘+’)
Output of the program(operation ‘-’)
Output of the program(operation ‘*’)
Problem 11
Write a program to generate 10 random numbers between 1 and 15 and store them in a table.
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
void main(void)
{
int T[10],N;
int j=0;
clrscr();
randomize();
do
{
N=random(15)+1;
T[j]=N;
j=j+1;
}
while(j<10);
j=0;
do
{
printf("%d\n",T[j]);
j=j+1;
}
while(j<10);
getch();
}
Output of the program
Functions/Procedures in C.
Write a program to add two numbers together.
#include<stdio.h>
int ADD(int x,int y);
void PRINT(int x,int y,int z);
void main(void)
{
int A=256,B=483,C;
C=ADD(A,B);
PRINT(A,B,C);
getch();
}
int ADD(int x,int y)
{
int s;
s=x+y;
return(s);
}
void PRINT (int x,int y,int z)
{
printf("The sum of %d and %d=%d\n",x,y,z);
}
Output of the program