You are on page 1of 18

P.K.

ROY MEMORIAL COLLEGE,


DHANBAD [BBMKU]
ASSIGNMENT

PROGRAMMING
IN C/C++

HARUN RASHID ANSARI


Roll No: VS210637014

Semester: 1ST semester


Session: 2021-24

Course: BCA [Bachelor Of


Computer Application]

Faculty: Dr. Aditi


Upadhyay
1. WAP to compute the sum of the first n-terms of the
following series:
Solution:-

#include<stdio.h>
int main(){
int n,i;
int sum=0;
printf("enter the n i.e. max value of series: ");
scanf("%d",&n);
sum = (n*(n+1))/2;
printf("sum of the series: ");
for(i =1; i<=n; i++){
if (i!=n)
printf("%d + ",i); else
printf("%d = %d",i,sum);
}
return 0;
}

OUTPUT:-

enter the n i.e. max value of series: 9


sum of the series: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45
2. WAP to find whether a given number is prime or
not. Use the same number to generate the prime
numbers less 100.
SOLUTION:-

#include <stdio.h>
int main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
if (n == 0 || n == 1)
flag = 1;
for (i = 2; i <= n / 2; ++i) {
if (n % i == 0) {
flag = 1;
break; }
}
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
return 0;
}
OUTPUT:-
Enter a positive integer: 20
20 is not a prime number.

→NOW, Using 20 as starting point to generate the prime


numbers less 100.
#include <stdio.h>
Int main() {

int i, a = 20, count;

while(a <= 100)


{
count = 0;
i = 2;
while(i <= a/2)
{
if(a%i == 0)
{
count++;
break;
}
i++;
}
if(count == 0 && a != 1 )
{
printf(" %d ", a);
}

a++; }
return 0;
}
OUTPUT:-

23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

3. WAP to print a triangle of star as follows (take


number of lines from user
*
***
*****
*******
*********
SOLUTION:-
Code:-

#include <stdio.h>
int main(){

int row, col, space=5,s;


for( row=1; row<= 9; row+=2 )
{
for(s=1; s<=space;s++)
printf(" ");
for(col=1; col<=row; col++)
printf("*");
printf("\n");
space--;
}
return 0;
}
OUTPUT:-
*
***
*****
*******
*********

4. WAP that swaps two numbers using pointers.


Solution:- CODE

#include <stdio.h>

int main()
{
int x, y, *a, *b, temp;

printf("Enter the value of x and y\n");


scanf("%d%d", &x, &y);

printf("Before Swapping\nx = %d\ny = %d\n", x, y);

a = &x;
b = &y;

temp = *b;
*b = *a;
*a = temp;

printf("After Swapping\nx = %d\ny = %d\n", x, y);

return 0;
}
OUTPUT:-

Enter the value of x and y


27
Before Swapping
x=2
y=7
After Swapping
x=7
y=2

5. WAP to calculate and print the sum and average of


the element of array entered by the user.
SOLUTION:-
# include<stdio.h>
int main( )
{
int a[25], n, i ;
float avg = 0, sum = 0 ;
printf(" Enter the Numbers of element in Array: ") ;
scanf("%d ",& n) ;
printf("\n Enter the Array of Element : \n") ;
for ( i = 1 ; i <= n ; i++)
{
scanf("%d ",& a[i]) ;
}
for ( i = 1 ; i <= n ; i++)
{
sum = sum + a[i] ;
avg = sum / n ;}
printf("\n Sum of Element of Array is : %f ",sum) ;
printf("\n Average of Element of Array are : %f ",avg) ;
return ( 0 ) ;
}

OUTPUT:-
Enter the Numbers of element in Array: 3
Enter the Array of Element :
21
33
12

Sum of Element of Array is : 56.000000


Average of Element of Array are : 18.666666

6. WAP to print the maximum and minimum elements of an


array
SOLUTION:-

#include<stdio.h>
#include<conio.h>

int main()
{
int a[1000],i,n,min,max;

printf("Enter size of the array : ");


scanf("%d",&n);
printf("Enter elements in array : ");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
min=max=a[0];
for(i=1; i<n; i++)
{
if(min>a[i])
min=a[i];
if(max<a[i])
max=a[i];
}
printf("minimum of array is : %d",min);
printf("\nmaximum of array is : %d",max);

return 0;
}

OUTPUT:-
Enter size of the array: 5
Enter elements in array: 1
2
3
4
5
minimum of an array is: 1
maximum of an array is: 5
7. WAP to calculate Fibonacci series using recursion
Method.
SOLUTION:-

#include<stdio.h>
int Fibonacci(int);
int main() {

int n, i = 0, c;

printf("Enter the number of element you want in series :\n");


scanf("%d",&n);
printf("Fibonacci series\n");

for ( c = 1 ; c <= n ; c++ )


{
printf("%d\n", Fibonacci(i));
i++;
}
return 0;
}
int Fibonacci(int n)
{

if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return ( Fibonacci(n-1) + Fibonacci(n-2) );
}
OUTPUT:-
Enter the number of element you want in series: 6
Fibonacci series is: 0
1
2
3
4
5

8. Write a program to find sum of ‘n’ elements entered by


the user. To write this program, allocate memory
dynamically using malloc ( ) / calloc ( ) function.
Solution:-
#include<stdio.h>
#include<stdlib.h>

int main(){
int i,n,sum=0;
int *arr;
printf("Number of elements you want to enter: \t");
scanf("%d", &n);
arr=(int*)malloc(n*sizeof(int));
for(i=0; i<n; i++)
scanf("%d", &arr[i]);
for(i=0; i<n; i++)
sum=sum+arr[i];

printf("sum is %d", sum);


return 0;
}

OUTPUT:-
Number of elements you want to enter: 3
3
2
4
sum is 9

9. WAP to calculate Fibonacci series using iteration Method.


SOLUTION:-

#include <stdio.h>
#include <conio.h>
int main() {

int n, first = 0, second = 1, result, i;

printf("Please give an input upto you want to print series :


\n");
scanf("%d", &n);
printf("Fibonacci Series is:\n");
for (i = 0; i < n; i++)
{
if (i <= 1)
result = i;
else
{
result = first + second;
first = second;
second = result;
}
printf("%d\n", result);
}
return 0;
}

OUTPUT:-
Please give a input upto you want to print series:
5
Fibonacci Series is: 0
1
1
2
3

10. Create a structure student containing fields, for Roll


No., Name, Class, Year and Total marks. Create 10
students and store them in a file.
SOLUTION:-

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Creating the student structure


struct Student {

char* name;
int roll_number;
int year;
double total_marks; };

int main() {

int i = 0, n = 10;

// Create the student's structure variable with n Student's records

struct Student student[n];

student[0].roll_number = 1;
student[0].name = "Harun";
student[0].year = 2020;
student[0].total_marks = 78.50;

student[1].roll_number = 5;
student[1].name = "Rashid";
student[1].year = 2020;
student[1].total_marks = 56.84;

student[2].roll_number = 2;
student[2].name = "Ansari";
student[2].year = 2019;
student[2].total_marks = 87.94;

student[3].roll_number = 4;
student[3].name = "Rohit";
student[3].year = 2022;
student[3].total_marks = 89.78;

student[4].roll_number = 3;
student[4].name = "Siri";
student[4].year = 2019;
student[4].total_marks = 78.55;

student[5].roll_number = 6;
student[5].name = "Soori";
student[5].year = 2019;
student[5].total_marks = 78.55;

student[6].roll_number = 7;
student[6].name = "Atal";
student[6].year = 2019;
student[6].total_marks = 76.86;

student[7].roll_number = 8;
student[7].name = "Raheem";
student[7].year = 2020;
student[7].total_marks = 96.86;

student[8].roll_number = 9;
student[8].name = "Ramsey";
student[8].year = 2020;
student[8].total_marks = 90.86;

student[9].roll_number = 10;
student[9].name = "Messi";
student[9].year = 2021;
student[9].total_marks = 97.96;

printf("Student Records:\n\n");

for (i = 0; i < n; i++) {

printf("\tName = %s\n", student[i].name);

printf("\tRoll Number = %d\n",


student[i].roll_number);

printf("\tYear = %d\n", student[i].year);

printf("\tTotal Marks = %0.2f\n\n",


student[i].total_marks);

}
return 0;
}

OUTPUT:- Student records:


Name = Harun
Roll Number = 1
Year= 2020
Total Marks = 78.50

Name = Rashid
Roll Number = 5
Year = 2020
Total Marks = 56.84

Name = Ansari
Roll Number = 2
Year = 2019
Total Marks = 87.94

Name = Rohit
Roll Number = 4
Year = 2022
Total Marks = 89.78

Name = Siri
Roll Number = 3
Year = 2019
Total Marks = 78.55

Name = Soori
Roll Number = 6
Year = 2019
Total Marks = 78.55
Name = Atal
Roll Number = 7
Year = 2019
Total Marks = 76.86

Name = Raheem
Roll Number = 8
Year = 2020
Total Marks = 96.86

Name = Ramsey
Roll Number = 9
Year = 2020
Total Marks = 90.86

Name = Messi
Roll Number = 10
Year = 2021
Total Marks = 97.96

You might also like