0% found this document useful (0 votes)
391 views12 pages

Programming With C (Assignment) MO-19 MCA 1st Semester: Name: Dhiraj Kumar Sah Roll No: MCA/10047/19

This document contains 4 programming assignments completed by Dhiraj Kumar Sah with an MCA roll number of MCA/10047/19. The assignments include: 1) A program to accept a month number and print the month name. 2) Programs to print multiple patterns using for loops. 3) A menu driven program to perform prime factorization, check for a leap year, calculate digit sum, and reverse a number. 4) A program to sort names alphabetically.

Uploaded by

Đhîřåj Šäh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
391 views12 pages

Programming With C (Assignment) MO-19 MCA 1st Semester: Name: Dhiraj Kumar Sah Roll No: MCA/10047/19

This document contains 4 programming assignments completed by Dhiraj Kumar Sah with an MCA roll number of MCA/10047/19. The assignments include: 1) A program to accept a month number and print the month name. 2) Programs to print multiple patterns using for loops. 3) A menu driven program to perform prime factorization, check for a leap year, calculate digit sum, and reverse a number. 4) A program to sort names alphabetically.

Uploaded by

Đhîřåj Šäh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Programming with C(Assignment) MO-19

MCA 1st semester


Name: Dhiraj kumar sah
Roll no: MCA/10047/19

1. Accept a month in digit from the user. Display the month in words. If number
is not between 1 and 12 display message “Invalid Month”. (Use ‘switch’):--
Program:-
#include <stdio.h>
void main()
{
int month;
printf("\nEnter The Month : \n");
scanf("%d",&month);

switch(month)
{
case 1:
printf("January\n");
break;
case 2:
printf("February\n");
break;
case 3:
printf("March\n");
break;
case 4:
printf("April\n");
break;
case 5:
printf("May\n");
break;
case 6:
printf("June\n");
break;
case 7:
printf("July\n");
break;
case 8:
printf("August\n");
break;
case 9:
printf("September\n");
break;
case 10:
printf("October\n");
break;
case 11:
printf("November\n");
break;
case 12:
printf("December\n");
break;
default:
printf("invalid Month number. \nPlease try again ....\n");
break;
}
return 0;
}

Output
2. Write a program to print the following pattern:
1 1
12 22
123 333
1234 4444
12345 55555
Program:--
a.
#include <stdio.h>
int main()
{
int i, j, rows;
printf("Enter number of rows: ");
scanf("%d",&rows);
for(i=1; i<=rows; ++i)
{
for(j=1; j<=i; ++j)
{
printf("%d ",j);
}
printf("\n");
}
return 0;
}
Output:

2.b.
#include <stdio.h>
int main()
{
int i, j, rows;
printf("Enter number of rows: ");
scanf("%d",&rows);
for(i=1; i<=rows; i++)
{
printf("\n");
for(j=1; j<=i; j++)
{
printf("%d ",i);
}

}
return 0;
}

Output:-
3. Following is the menu to be displayed to the user. On selecting a choice display
appropriate result. Number should be accepted from the user.
Menu
1. Prime Factors
2. Leap Year
3. Sum of all digits
4. Number in reverse order

Program:-

#include <stdio.h>
int main()
{
int n,result=0,num=0;
printf("\nChoice is :---\n");
printf("\n1.Prime factorial \n");
printf("\n2.Leap Year \n");
printf("\n3.Sum of all digits \n");
printf("\n4.Number in reverse order \n\n\n");
printf("\nSelect one: \n");
scanf("%d",&n);
switch(n)
{
case 1:
printf("\nYour choice is prime factorial \n");
printf("\nEnter the number \n");
scanf("%d",&num);
fact(num);
break;
case 2:
printf("\nYour choice is Leap Year \n");
printf("\nEnter the year \n");
scanf("%d",&num);
year(num);
break;
case 3:
printf("\nYour choice is Sum of all digits \n");
printf("\nEnter the number \n");
scanf("%d",&num);
sum(num);
break;
case 4:
printf("\nYour choice is Number in reverse order \n");
printf("\nEnter the number \n");
scanf("%d",&num);
rev(num);
break;
default :
printf("\n\nInvalid \n\n");

}
}

void fact(int n)
{

while (n%2 == 0)
{
printf("%d ", 2);
n = n/2;
}
for (int i = 3; i <= sqrt(n); i = i+2)
{
while (n%i == 0)
{
printf("%d ", i);
n = n/i;
}
}
if (n > 2)
printf ("%d ", n);
}

int year (int num)


{
if(num%4 == 0)
{
if( num%100 == 0)
{

if ( num%400 == 0)
printf("\n\n%d is a leap year.\n\n", num);
else
printf("\n\n%d is not a leap year.\n\n", num);
}
else
printf("\n\n%d is a leap year.\n\n", num );
}
else
printf("\n\n%d is not a leap year.\n\n", num);
}

int sum(num)
{
int sum=0,t;
while(num!=0)
{
t=num%10;
sum=sum+t;
num=num/10;
}
printf("\nSum== %d\n",sum);
}

int rev(num)
{
int rem,rev=0;
while(num!=0)
{
rem=num%10;
rev=rev*10+rem;
num=num/10;

}
printf("\nrev== %d\n",rev);
}

Output:-
4. Write a program to arrange the following names in alphabetical order.
George
Albert
Tina
Xavier
Roger
Tim William.

Program:-
#include<stdio.h>
#include<string.h>
int main()
{
int i,j,count;
char str[25][25],temp[25];
puts("How many strings you want to entered : ");
scanf("%d",&count);

puts("Enter Strings one by one: ");


for(i=0;i<=count;i++)
{
gets(str[i]);
}
printf("\nBefore arranging the string\n ");
for(i=0;i<=count;i++)
{
puts(str[i]);
}
for(i=0;i<=count;i++)
for(j=i+1;j<=count;j++){
if(strcmp(str[i],str[j])>0){
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
printf("\nAfter arranging the string:\n");
for(i=0;i<=count;i++)
puts(str[i]);

return 0;
}

Output:-
Name: Dhiraj kumar sah
Roll no: MCA/10047/19

You might also like