0% found this document useful (0 votes)
50 views

Afraz Naginger Mca - (A) Rollno:59 C-Assignment

This document contains 10 C programming assignments submitted by Afraz Naginger for their MCA program. Each assignment includes the name, roll number, and program code to complete the task which includes: calculating array sums and averages, string length functions, reversing strings, prime number checks, rearranging arrays, exponent calculations, smallest divisors, factorials, Fibonacci sequences, recursion problems, and a function to swap variable values.

Uploaded by

Afraz Naginger
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Afraz Naginger Mca - (A) Rollno:59 C-Assignment

This document contains 10 C programming assignments submitted by Afraz Naginger for their MCA program. Each assignment includes the name, roll number, and program code to complete the task which includes: calculating array sums and averages, string length functions, reversing strings, prime number checks, rearranging arrays, exponent calculations, smallest divisors, factorials, Fibonacci sequences, recursion problems, and a function to swap variable values.

Uploaded by

Afraz Naginger
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

AFRAZ NAGINGER

MCA -[A] RollNo:59


C-ASSIGNMENT.
1)Write a program to accept n and n input values to be stored in an array. Find sum
and average (avg) of n values. Print input values followed by sum, avg.
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[50],n,i,sum=0;
float avg;
clrscr();
printf("\n Name:Afraz Naginger MCA-[A] Rollno:59");
printf("\nEnter size of array within 50: ");
scanf("%d", &n);
for(i=0; i<n; i++)
{
scanf("%d",&arr[i]);
}
for(i=0; i<n; i++)
{
sum+=arr[i];
}
avg = sum/n;
printf("\nsum is: %d\navg is: %0.2f", sum,avg);
getch();
}

2)Write a program to find string length by using function for finding string
length.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[20];
clrscr();
printf("\n Name:Afraz Naginger MCA-[A] Rollno:59");
printf("\nEnter String: ");
gets(str1);
printf("\nString Length is: %d", strlen(str1));
getch();
}

3)Write a program to print a given string in reverse order.#include<stdio.h>


#include<conio.h>
#include<string.h>
void main()
{
char str[10];
clrscr();
printf("\n Name:Afraz Naginger MCA-[A] Rollno:59");
printf("\nEnter String: ");
gets(str);
printf("\nString in Reverse Order: %s", strrev(str));
getch();
}

4)Write a program in C to print all primes in the first n (n > 1)


integers.#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n, flag=0, c=0;
clrscr();
printf("\n Name:Afraz Naginger MCA-[A] Rollno:59");
printf("\nEnter Range: ");
scanf("%d", &n);

for(i=2; i<=n; i++)


{
for(j=2; j<i; j++)
{
if(i%j==0)
{
flag=1;
break;
}
else
{
flag=0;
}
}
if(flag==0)
{
printf("\t%d",i);
c++;
}
}
printf("\nCount: %d",c);
getch();
}

5)Write a program in C to rearrange the elements in an array so that they appear in


reverse order.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,j,temp,n=5;
clrscr();

printf("\n Name:Afraz Naginger MCA-[A] Rollno:59");


printf("\nEnter 5 Elements: ");
for(i=0;i<n;i++)
{
printf("\nEnter element %d: ",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\nElements in Reverse Order: ");
for(i=n-1;i>=0;i--)
{
printf("\t%d", a[i]);
}
getch();
}

6)Given some integer x, develop an algorithm and write a program to compute


the value of x^n where n is considerably larger than 1.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int temp, x,y;
clrscr();
printf("\n Name:Afraz Naginger MCA-[A] Rolno:59");
printf("\nEnter x: ");
scanf("%d", &x);
printf("\nEnter y: ");
scanf("%d", &y);
temp = pow(x,y);
printf("\nx^y = %d^%d is: %d",x,y,temp);
getch();
}

7)Given an integer n >= 1, develop an algorithm and write a program to find the
smallest exact divisor of n other than one.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,no;
clrscr();
printf("\n Name:Afraz Naginger MCA-[A] Rollno:59");
printf("\nEnter no: ");
scanf("%d",&no);
if(no>1)
{
for(i=2; i<=no; i++)
{
if(no%i==0)
{
printf("\nSmallest divisor is: %d", i);
break;
}
}
}
else
{
printf("\nEnter valid number");
}
getch();
}

8)Write a program in C (a) to find factorial of n (n!), and (b) first n terms of the
Fibonacci sequence using an iterative algorithm.
#include<stdio.h>
#include<conio.h>
int fibonacci(int);
void main()
{
int no, fact=1, temp, i;
clrscr();
printf("\n Name:Afraz Naginger MCA-[A] Rollno:59");
printf("\n Enter number for Factorial: ");
scanf("%d", &no);
temp = no;
for(i=0; i<no; i++)
{
fact = fact * temp;
temp--;
}
printf("\nFactorial of %d is %d",no,fact);
printf("\n\nEnter number for Fibonaaci: ");
scanf("%d", &no);
for(i=0; i<no; i++)
{
printf("%d\t", fibonacci(i));
}
getch();
}
int fibonacci(int i)
{
if(i==0)
return 0;
else if(i==1)
return 1;
else
return( fibonacci(i-1)+fibonacci(i-2));
}

9)Using Recursion find: (a) n! ; (b) fibo(n); and (c) sum of a0 + a1 + … + an-1 +
an .
#include<stdio.h>
#include<conio.h>
int fact(int );
int fibo(int );
int addnum(int );
void main()
{
int no, i, result, a=0, b=1, c;
clrscr();
printf("\n Nmae:Afraz Naginger MCA-[A] Rollno:59");
printf("\nEnter no for Factorial: ");
scanf("%d", &no);
result = fact(no);
printf("\nFactorial of %d is %d",no,result);
printf("\nEnter no for Fibonacci: ");
scanf("%d", &no);
for(i=0; i<no; i++)
{
printf("%d\t",a);
c = a + b;
a = b;
b = c;
}
printf("\nEnter a positive number: ");
scanf("%d", &no);
printf("\nSum= %d", addnum(no));
getch();
}
int fact(int no)
{
if(no==1 || no==0)
return 1;
else
return no*fact(no-1);
}
int fibo(int i)
{
if(i==0)
return 0;
else if(i==1)
return 1;
else
return ( fibo(i-1) + fibo(i-2) );
}
int addnum(int no)
{
if(no!=0)
return no+addnum(no-1);
else
return no;
}

10)Im
plement the program in C for “exchanging the values of two variables” using
function (which will require use of pointers for function arguments in C).
#include<stdio.h>
#include<conio.h>

void swap(int *,int *);


void main()
{
int no1,no2;
clrscr();
printf("\n Name:Afraz Naginger MCA-[A] Rollno:59");
printf("\nEnter no1 and no2: ");
scanf("%d %d", &no1, &no2);
printf("\nBefore Swapping values are:\n%d %d",no1,no2);
swap(&no1, &no2);
printf("\nAfter Swapping values are:\n%d %d",no1,no2);
getch();
}
void swap(int *p1, int *p2)
{
*p1 = *p1 + *p2;
*p2 = *p1 - *p2;
*p1 = *p1 - *p2;
}

You might also like