CPPM

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

F.Y.B.C.A.

(Sem I)
Name=paresh suthar
104- Computer Programming & Programming Methodology Journal Program

 Write a program to take input of name, rollno and marks obtained by a student in
4 subjects of 100 marks each and display the name, rollno with percentage score
secured.
solution--->
#include<stdio.h>
#include<conio.h>
int main(){
char name;
int rollno,sub1,sub2,sub3,sub4,sum;
float per;
printf("Student Full Name : ");
scanf("%s",&name);
printf("\nStudent Rollno. : ");
scanf("%d",&rollno);
//entering marks of subject
printf("CPPM : ");
scanf("%d",&sub1);
printf("MATHS : ");
scanf("%d",&sub2);
printf("DMA : ");
scanf("%d",&sub3);
printf("IC : ");
scanf("%d",&sub4);
sum=sub1+sub2+sub3+sub4;
per=sum*100/400;
printf("YOUR TOTAL MARKS IS : %d",sum);
printf("\nYOUR PERCENTAGE IS : %.2f",per);
return 0;
}

Output-
Student Full Name : paresh
Student Rollno. : 657
CPPM : 82
MATHS : 71
DMA : 80
IC : 77
YOUR TOTAL MARKS IS : 310
YOUR PERCENTAGE IS : 77.50

 Write a program to find GCD (greatest common divisor or HCF) and LCM (least
common multiple) of two numbers.
Solution-
#include<stdio.h>
#include<conio.h>

int main()
{
int num1, num2, gcd, lcm, i;
clrscr();
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
for(i=1; i<=num1; i++)
{
if(num1%i==0 && num2%i==0)
{
gcd = i;
}
}

lcm = (num1 * num2)/gcd;


printf("GCD = %d and LCM = %d", gcd, lcm);
getch();
return0;
}
Output-
Enter first number: 4
Enter second number: 6
GCD = 2 and LCM = 6

 Write a Program to Check Whether a Number is Prime or not.


Input: n=7
Output: 7 is Prime No.
Solution-
#include<stdio.h>
#include<conio.h>
void main()
{
int a,i,b,flag=0;
printf("Enter number to check: ");
scanf("%d",&a);
b=a/2;
for(i=2;i<=b;i++)
{
if(a%i==0)
{
printf("Not Prime");
flag=1;
break;
}
}
if(flag==0)
{
printf(" Prime number.");
}
getch();
}

Output-
Enter number to check: 7
Prime number.
 Write a program to check whether the entered year is leap year or not (a year is
leap if it is divisible by 4 and divisible by 100 or 400.)
Solution
#include<stdio.h>
#include<conio.h>
int main()
{
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (year % 400 == 0) {
printf("%d is a leap year.", year);
}

else if (year % 100 == 0) {


printf("%d is a leap year.", year);
}
else if (year % 4 == 0) {
printf("%d is a leap year.", year);
}
else {
printf("%d is not a leap year.", year);
}
getch();
return 0;
}

Output-
Enter a year: 2012
2012 is a leap year.

 Write a program to find the factorial of a number.


Solution
#include<stdio.h>
#include<conio.h>
int main()
{
int i,fact=1,number;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=number;i++){
fact=fact*i;
}
printf("Factorial of %d is: %d",number,fact);
getch();
return 0;
}

Output-
Enter a number: 6
Factorial of 6 is: 720

 Write a program to check number is Armstrong or not.


Hint: 153=1^3+5^3+3^3
Solution

#include<stdio.h>
#include<conio.h>
int main()
{
int n, r, sum = 0, temp;
printf("enter the number=");
scanf("%d", &n);
temp = n;
while (n > 0)
{
r = n % 10;
sum = sum + (r * r * r);
n = n / 10;
}
if (temp == sum)
printf("armstrong number ");
else
printf("not armstrong number");
getch();
return 0;
}

Output-
enter the number=153
armstrong number

 Write a program to take the value from user as input any character and check
whether it is the alphabet, digit or special character. Using the switch statement.
Solution
#include<stdio.h>
#include<conio.h>
int main(void)
{
char i;
printf("Enter a character: \n");
scanf("%c",&i);
switch(i)
{
case 'A'...'Z':
printf("Upper case alphabet\n");
break;
case 'a'...'z':
printf("Lower case alphabet\n");
break;
case '0'...'9':
printf("Digit \n" );
break;
default:
printf("Special character\n");
}
getch();
return 0;
}

Output-
Enter a character: D
Upper case alphabet

 Write a program to display Characters from A to Z using Loop.


Solution

#include<stdio.h>
#include<conio.h>
int main()
{
char c;
for (c = 'A'; c <= 'Z'; ++c)
{
printf("%c ", c);
}
getch();
return 0;
}

Output-
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

 Write a program to generate following Fibonacci series:


Input n=5
Output: 0,1,1,2,3,5
Solution
#include<stdio.h>
#include<conio.h>
int main()
{

int i, n;
int t1 = 0, t2 = 1;
int nextterm = t1 + t2;
printf("Enter the number : ");
scanf("%d", &n);
printf("Fibonacci Series: %d, %d, ", t1, t2);
for (i = 3; i <= n; ++i)
{
printf("%d, ", nextterm);
t1 = t2;
t2 = nextterm;
nextterm = t1 + t2;
}
getch();
return 0;
}

Output-
Enter the number : 5
Fibonacci Series: 0, 1, 1, 2, 3,5

 Write a program to reverse a number.


Input n = 1345
Output = 5431
Solution
#include<stdio.h>
#include<conio.h>
int main()
{
int n, reverse=0, rem;
printf("Enter a number: ");
scanf("%d", &n);
while(n!=0)
{
rem=n%10;
reverse=reverse*10+rem;
n/=10;
}
printf("Reversed Number: %d",reverse);
getch();
return 0;
}
Output-
Enter a number: 1345
Reversed Number: 5431

11. Write a program that will display large & small number from 1-D array.
Solution

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

int main()
{
int a[50],i,n,large,small;
printf("\nEnter the number of elements :");
scanf("%d",&n);
printf("\nInput the array elements : ");
for(i=0;i<n;++i)
scanf("%d",&a[i]);

large=small=a[0];

for(i=1;i<n;++i)
{
if(a[i]>large)
large=a[i];

if(a[i]<small)
small=a[i];
}

printf("\nThe smallest number is %d\n",small);


printf("\nThe largest number is %d\n",large);

getch();
return 0;
}

Output-
Enter the number of elements :5
Input the array elements : 5 4 2 3 1
The smallest element is 1
The largest element is 5

12. Write a program that will search an element in 1- D array & if it is found than
display the position of it in the array.

Solution
#include<stdio.h>
#include<conio.h>
int main()
{
int a[100], n, element, pos=0;
int i;

printf("Enter array size [1-100]: ");


scanf("%d", &n);

printf("Enter array elements: ");


for(i=0; i<n; i++)scanf("%d", &a[i]);

printf("Enter element to search: ");


scanf("%d",&element);

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


{
if(a[i]==element)
{
printf("%d found at position %d", element, i+1);
}
}

printf("%d not found.", element);


getch();
return 0;
}
Output-
Enter array size [1-100]: 5
Enter array elements: 12 34 56 34 23
Enter element to search: 56
56 found at position 3

13. Write a program that will merge two 1-D arrays into single & display the
resultant array in ascending order.
Solution
#include<stdio.h>
#include<conio.h>
int main()
{
int n1,n2,n3;
int a[10000], b[10000], c[20000];
printf("Enter the size of first array: ");
scanf("%d",&n1);
printf("Enter the array elements: ");
for(int i = 0; i < n1; i++)
scanf("%d", &a[i]);
printf("Enter the size of second array: ");
scanf("%d",&n2);
printf("Enter the array elements: ");
for(int i = 0; i < n2; i++)
scanf("%d", &b[i]);
n3 = n1 + n2;
for(int i = 0; i < n1; i++)
c[i] = a[i];
for(int i = 0; i < n2; i++)
c[i + n1] = b[i];

printf("The merged array: ");


for(int i = 0; i < n3; i++)
printf("%d ", c[i]);
printf("\nFinal array after sorting: ");
for(int i = 0; i < n3; i++){
int temp;
for(int j = i + 1; j < n3; j++) {
if(c[i] > c[j]) {
temp = c[i];
c[i] = c[j];
c[j] = temp;
}
}
}
for(int i = 0; i < n3 ; i++)
printf(" %d ",c[i]);

getch();
return 0;
}

Output-
Enter the size of first array: 3
Enter the array elements: 12 43 54
Enter the size of second array: 4
Enter the array elements: 43 56 78 34
The merged array: 12 43 54 43 56 78 34
Final array after sorting: 12 34 43 43 54 56 78

14. Write a program that will check whether inputted string is palindrome or not.
For example, Input: str="madam” Output: string is palindrome.
Solution
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char a[100],b[100];
int n;

printf("Enter the string : ");


gets(a);

strcpy(b,a);
strrev(a);
n = strcmp(a,b);
printf("The reverse of string : %s\n",a);

if(n==0)
{
printf("\nThe string is palindrome...\n");
}
else
{
printf("\nThe string is not palindrome...\n");
}
getch();
return 0;
}

Output-
Enter the string : madam
The reverse of string : madam
The string is palindrome...
15. Write a program to print Inverted half Pyramid of * Pattern
Solution
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, rows;
printf (" Enter a number to define the rows: \n ");
scanf("%d", &rows);
printf("\n");
for (i = rows; i > 0; i--)
{
for (j = i; j > 0; j--)
{
printf ("* ");
}
printf ("\n");
}
getch();
}
Output-
Enter a number to define the rows: 5

* * * * *
* * * *
* * *
* *
*

16. Write a program to print full Pyramid of * Pattern.

Solution
#include<stdio.h>
#include<conio.h>
void main()
{

int i, j, rows, k = 0;
printf (" Enter a number to define the rows: \n");
scanf ("%d", &rows);

for ( i =1; i <= rows; i++)


{
for ( j = 1; j <= rows - i; j++)
{
printf (" ");
}

for ( k = 1; k <= ( 2 * i - 1); k++)


{
printf ("* ");
}
printf ("\n");
}
getch();
}

Output-
Enter a number to define the rows: 5
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *

17. Write a program to print Floyd’s Triangle.


Solution
#include<stdio.h>
#include<conio.h>
void main()
{
int num, i, j, k = 1;

printf( " Enter a number to define the rows in Floyd's triangle: \n");
scanf( "%d", &num);
for (i = 1; i <= num; i++)
{
for (j = 1; j <= i; j++)
{
printf(" %2d", k++);
}
printf( "\n");
}
getch();
}

Output-
Enter a number to define the rows in Floyd's triangle: 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Divesh Karwasara

You might also like