Assignment - 1: Code
Assignment - 1: Code
Assignment - 1: Code
Code:
#include<stdio.h>
int main()
{
int n, i = 3, count, c;
printf("Enter the number of prime numbers required\n");
scanf("%d",&n);
if ( n >= 1 )
{
printf("First %d prime numbers are :\n",n);
printf("2\n");
}
for ( count = 2 ; count <= n ; )
{
for ( c = 2 ; c <= i - 1 ; c++ )
{
if ( i%c == 0 )
break;
}
if ( c == i )
{
printf("%d\n", i);
count++;
}
i++;
}
return 0;
}
Ques 2 Write a c program to calculate average of 3 subjects for 10 students
Code:
# include <stdio.h>
int main()
{
int arr[10],arr1[10],arr2[10];
float avg1,avg2,avg3;
int i;
printf("enter the first subject marks");
for(i=0;i<10;i++)
{scanf("%d",&arr[i]);
}
printf("enter second subject marks");
for(i=0;i<10;i++)
{
scanf("%d",&arr1[i]);
}
printf("enter third subject marks");
for(i=0;i<10;i++)
{
scanf("%d",&arr2[i]);
}
avg1=0;
avg2=0;
avg3=0;
for(i=0;i<10;i++)
{
avg1=avg1+arr[i];
avg2=avg2+arr1[i];
avg3=avg3+arr2[i];
}
avg1=avg1/10;
avg2=avg2/10;
avg3=avg3/10;
printf("average of sub1=%f\naverage of sub2=%f\n",avg1,avg2);
printf("average of sub3=%f",avg3);
}
Ques3:Write a c program to check whether a number is perfect number or not.
Code:
# include <stdio.h>
int main()
{
int n,i,sum;
sum=0;
printf("enter the number");
scanf("%d",&n);
for (i=1;i<n;i++)
{
if(n%i==0)
{sum=sum + i;
}
}
if(sum==n)
{
printf("%d is a perfect number",n);
}
else
{
printf("%d is not a perfect number",n);
Ques4 Write s shell script to print the first 50 natura numbers and
their sum
Code:
#!/bash/sh
for x in {1..50}; do
echo $x
done
n=50
num=$(($n/2))
num1=$(($num*$(($n +1))))
echo "sum=" $num1
Ques5 : Print “welcome to VIT” using system call in such a way that for each
iteration it has to wait at least 10ms
Code:
# include <stdio.h>
# include <unistd.h>
int main()
{
int i=0;
for(i;i<10;i++)
{
printf("Welcome to VIT\n");
sleep(1);
}
return 0;
}
Ques 6.Write a shell script to compare two strings
Code:
#!/bash/sh
echo "enter string 1"
read string
echo "enter string 2"
read strin
if [ $string = $strin ];
then
echo "both are same"
else
echo "both are not same"
fi