Sei sulla pagina 1di 7

C : (FIRST LEVEL- INTERNAL LAB EXAM PROGRAMS)

1. Write a C program to add two numbers:


Algorithm
Step 1: Start
Step2: Read a and b
Step3: Set sum=0
Step 4: Calculate sum=a + b
Step5: Print Sum
Step 6: Stop

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;
}

3. Write a C program to print 1 to n numbers using while loop.


Algorithm
Step 1: Start
Step2: Read a number into n
Step 3: Set i=1
Step 4: Repeat step 4-6 while i<=n
Step 5: Print the number i
Step 6: Set i=i+1
Step 7: Stop

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);

6. Write a C program to read and print an array of n numbers


Algorithm
Step 1: Start
Step2: Read the size of the array A into n
Step 3: Set i=0
Step 4: Repeat Steps 5-7 while i<=n
Step 5: Print A[i]
Step 6: Set i=i+1
Step 7: Stop

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;
}

7. Write a C program to print the sum of integers from 1 to n

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);
}

8. Write a C program to read and print a matrix


Algorithm
Step 1: Start
Step2: Read the total number of rows and columns of the matrix A into m and n
Step 3: Set i=j=0
Step 4: Repeat step 5-8 while i<m
Step 5: Repeat step 6- 7 while j<n
Step 6: Read each element of matrix A[i][j]
Step 7: Set j=j+1
Step 8: Set i=i+1
Step 9: Stop

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");
}
}

9. Write a C program to check whether a number is prime or not


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>
int main()
{
int n,i,flag=0;
printf("enter a positive number greater than 1 \n");
scanf("%d",&n);
for(i=2;i<n/2;i++)
{
if(n%i==0)
{ flag=1;
break;
}
}
if(flag==1)
{
printf("The number %d is a prime number\n",n);
}
else
{
printf("The number %d is not a prime number\n",n);
}
return 0;
}
10. Write a C program to check whether a number is Armstrong or not

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);
}

Potrebbero piacerti anche