Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Awsom

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

Government college of Engineering,Aurangabad

MC1102 Computer Programming Assignment No 2


Name : Pratiksha Devidas Bhoyar
Roll No : MC22F14F010

Q3 write a program to check prime number by using function


#include<stdio.h>
#include<conio.h>
int fun(int a);
int main()
{
int a,result;
printf("enter the number:\n");
scanf("%d",&a);
result = fun(a);
if (result == 0)
{
printf("%d is prime number",a);
}
else
{
printf("%d is not prime number",a);
}
getch();
return 0;
}
int fun(int a)
{
for (int i = 2; i < a/2; i++)
{
if (a%i != 0)
{
continue;
}
else
{
return 1;
}
}
return 0;

}
Q 6. Write a program to find all armstrong numbers within the given
range using functions

#include<stdio.h>
#include<conio.h>
void printarmstrong(int a, int b);
int fun(int x);
int main()
{
int a,b;
printf("enter the range of number:");
scanf("%d%d",&a,&b);
printf("ALL AMSTRONG NUMBER BETWEEN %d TO %d are:\
n",a,b);
printarmstrong(a,b);
getch();
return 0;
}
int fun(int x)
{
int num, rem = 0,sum = 0;
num = x;
while(num !=0)
{
rem = num%10;
sum = sum+(rem*rem*rem);
num = num/10;
}
if (sum == x)
{
return 1;
}
else
{
return 0;
}
}
void printarmstrong(int a, int b)
{
while(a <= b)
{
if (fun(a))
{
printf("%d ",a);
}
a++;
}
}
Q 8. Write a program to find power of a number using recursion
#include<stdio.h>
#include<conio.h>
int fun(int a,int b);
int main()
{
int a,b,result;
printf("enter the base number:\n");
scanf("%d",&a);
printf("enter the power number:\n");
scanf("%d",&b);

result = fun(a,b);
printf("%d",result);
getch();
return 0;
}
int fun(int a,int b)
{
if (b == 1)
{
return a;
}
else
{
return a*fun(a,b-1);
}
}
Q10. Write a program to print even first 10 numbers using recursion

#include<stdio.h>
#include<conio.h>
int fun(int num);
int main()
{
printf("FIRST 10 EVEN NUMBER IS:\n");
fun(2);
getch();
return 0;
}
int fun(int num)
{
printf("%d ",num);
if (num == 20)
{
return 0;
}
else
{
return fun(num + 2);
}
Q24.write a program to change string to lower case without strlwr
#include<stdio.h>
#include<conio.h>
int main()
{
char str[40];
int i;
printf("ENTER THE STRING\n");
gets(str);
for (i = 0; str[i]!='\0'; i++)
{
if (str[i]>='A' && str[i]<='Z')
{
str[i] = str[i]+32;
}
}
printf("string in lowercase is : ");
puts(str);
getch();
return 0;
}
Q 16.Write a program to sort an Array using Pointer */

#include<stdio.h>
#include<conio.h>
int main()
{
int a[5],i,j,n,temp;
int *p=&a[0];
clrscr();
scanf("%d",&n);
printf("\nEnter the elements in array");
for(i=0;i<n;i++)
{
scanf("%d",(p+i)); printf("\nEnter the size of array:");

}
printf("\nThe element of an array is:");
for(i=0;i<n;i++)
{
printf(" %d",*(p+i));
}
//Logic to sort an array using pointer
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(*(p+j) > *(p+(j+1)))
{
temp=*(p+j) ;
*(p+j)=*(p+(j+1));
*(p+(j+1))=temp;
}
}
}
printf("\nArray element of an array after sorting :");
for(i=0;i<n;i++)
{
printf(" %d",*(p+i));
}
getch();
}
/* Write a program to sort an Array using Pointer */
#include<stdio.h>
#include<conio.h>
int main()
{
int a[5],i,j,n,temp;
int *p=&a[0];
clrscr();
printf("\nEnter the size of array:");
scanf("%d",&n);
printf("\nEnter the elements in array");
for(i=0;i<n;i++)
{
scanf("%d",(p+i));
}
printf("\nThe element of an array is:");
for(i=0;i<n;i++)
{
printf(" %d",*(p+i));
}
//Logic to sort an array using pointer
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(*(p+j) > *(p+(j+1)))
{
temp=*(p+j) ;
*(p+j)=*(p+(j+1));
*(p+(j+1))=temp;
}
}
}
printf("\nArray element of an array after sorting :");
for(i=0;i<n;i++)
{
printf(" %d",*(p+i));
}
getch();
}
Q 26 Write a program to store information in structure and display it.

#include <stdio.h>
struct student {
char firstName[50];
int roll;
float marks;
} s[5];

int main() {
int i;
printf("Enter information of students:\n");

// storing information
for (i = 0; i < 5; ++i) {
s[i].roll = i + 1;
printf("\nFor roll number%d,\n", s[i].roll);
printf("Enter first name: ");
scanf("%s", s[i].firstName);
printf("Enter marks: ");
scanf("%f", &s[i].marks);
}
printf("Displaying Information:\n\n");

// displaying information
for (i = 0; i < 5; ++i) {
printf("\nRoll number: %d\n", i + 1);
printf("First name: ");
puts(s[i].firstName);
printf("Marks: %.1f", s[i].marks);
printf("\n");
}
return 0
}
j
Q.25 Write a program to count number of alphabets, digit and special character
in string.

#include<stdio.h>
#include<ctype.h>
void main()
{

int number;
char string[50];
int alphabets=0;
int digits=0;
int special=0;

printf("Enter the string :");


gets(string);
for(number=0;string[number]!='\0';number++)
{
if(string[number]>='a'&&string[number]<='z'||string[number]>='A'&&string[number]<='Z')
{
alphabets=alphabets+1;
//alphabets++;
}
else if(string[number]>='0'&&string[number]<='9')
{
digits=digits+1;
//digits++;
}
else
{
special=special+1;
//special++;
}
}
printf("The number of alphabets in the string is : %d\n",alphabets);
printf("The number of digits in the string is : %d\n",digits);
printf("The number of special characters in the string is : %d\n",special);
}
Q23. Write a Program to change string to uppercase without strupr.

#include <stdio.h>
#include <string.h>
int main()
{
char Str1[100];
int i;

printf("\n Please Enter any text : ");


gets(Str1);

for (i = 0; Str1[i]!='\0'; i++)


{
if(Str1[i] >= 97 && Str1[i] <= 122)
{
Str1[i] = Str1[i] -32;
}
}

printf("\n Upper Case = %s", Str1);


return 0;
}
Q.7 Write a program to find all perfect numbers within the given range using
function.

#include<stdio.h>
#include<conio.h>
int main()
{
int no,sum=0,min,max,i;
clrscr();
printf("Enter the minimum and maximum value\n");
scanf("%d%d",&min,&max);
printf("\nPerfect Number between %d and %d are\n",min,max);
for(no=min;no<=max;no++)
{
for(i=1,sum=0;i<no;i++)
{
if(no%i==0)
sum=sum+i;
}
if(sum==no)
printf("%d\t",no);
}
getch();
return 0;
}

You might also like