Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Program
#include<stdio.h>
int main( )
{
int a,b,sum=0;
printf("enter two numbers\n");
scanf("%d%d",&a,&b);
sum=a+b;
printf("The sum of the two numbers is %d", sum);
return 0;
}
2. Write a C program to check the number is odd or even.
Algorithm
Step 1: Start
Step2: Read a number into n
Step 3: If (n mod 2=0 )then goto step 4
Else goto step 5
Step 4: Print the number n is even
Step 5: Print the number n is odd
Step 6: Stop
Program
#include<stdio.h>
#include<conio.h>
int main( )
{
int n;
printf("enter the number\n");
scanf("%d",&n);
if(n%2==0)
{
printf("The number %d is even",n);
}
else
{
printf("The number %d is odd",n);
}
return 0;
}
Program
#include<stdio.h>
#include<conio.h>
int main( )
{
int n,i=1;
printf("enter the number n\n");
scanf("%d",&n);
while(i<=n)
{
printf("%d\t",i);
i++;
}
return 0;
}
4. Write a C program to print the numbers 1 to n using for loop
(same algorithm)
Program
#include<stdio.h>
#include<conio.h>
int main( )
{
int n,i;
printf("enter the number n\n");
scanf("%d",&n);
for (i=1; i<=n; i++)
{
printf("%d\t",i);
}
return 0;
}
5. Write a C program to find the area of the triangle
Algorithm
Step 1: Start
Step2: Read the sides of the triangle a,b,c
Step 3: Calculate S=(a+b+c)/2
Step 4: Calculate A=√𝑠(𝑠 − 𝑎)(𝑠 − 𝑏)(𝑠 − 𝑐)
Step 5: Print area A
Step 6: Stop
Program
#include<stdio.h>
#include<math.h>
void main()
{
int a,b,c;
float s, A;
printf("enter the sides of the triangle\n");
scanf("%d%d%d",&a,&b,&c);
s=(a+b+c)/(float)2;
A=sqrt(s*(s-a)*(s-b)*(s-c));
printf("The area of the triangle is \n");
printf("%f", A);
Program
#include<stdio.h>
int main ( )
{
int a[10],i,n;
printf("enter the size of the array a\n");
scanf("%d",&n);
printf("enter the elements of the array\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
return 0;
}
Algorithm
Step 1: Start
Step2: Read the total number of integers into n
Step 3: Set i=1, sum=0
Step 4: Repeat Steps 5-6 while i<=n
Step 5: Set sum=sum+i
Step 6: Set i=i+1
Step 7: Print sum
Step 8:Stop
Program
#include<stdio.h>
void main()
{
int n,i=1,sum=0;
printf("enter the number\n");
scanf("%d",&n);
while(i<=n)
{
sum=sum+i;
i=i+1;
}
printf("%d",sum);
}
Program
#include<stdio.h>
void main()
{
int A[10][10],m,n,i,j;
printf("enter the no of rows and columns in the matrix\n");
scanf("%d%d",&m,&n);
printf("enter the elements of the matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&A[i][j]);
}
}
printf("The elements in the matrix are\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",A[i][j]);
}
printf("\n");
}
}
Algorithm
Step 1: Start
Step2: Read the number into n
Step 3: Set flag=0, i=2
Step 4: Repeat steps 5-7 while i<n/2
Step 5: If n mod i =0 then go to step 6
Else go to step 7
Step 6: Set flag=1
Step 7: Set i=i+1
Step 8: If flag=0 then go to step 9
Else go to step 10
Step 9: Print the number is a prime number
Step 10: Print the number is not a prime number
Step 11: Stop
Program
#include<stdio.h>
void main()
{
int num,r,sum,temp;
printf("Enter a nonnegative number to test whether amstrong or not\n ");
scanf("%d",&num);
temp=num;
sum = 0;
while(temp!=0)
{
r=temp % 10;
temp=temp/10;
sum=sum+(r*r*r);
}
if(sum==num)
printf("The number %d is an amstrong number ",num);
else
printf("The number %d is not an amstrong number",num);
}