Digital Learning

Showing posts with label C Language. Show all posts
Showing posts with label C Language. Show all posts

Friday, April 17, 2020

'C' Programs related to String & Matrix:

1. Write a C program to enter a string and print the same.
2. Write a C program to find out number of letters in a string.
3. Write a C program to check that you entered alphabet, digit or special character and print the same.
4. Write a C program to convert small letters into capital letters.
5. Write a C program to check that the given string is palindrome or not.
6. Write a C program for 3*3 matrix, enter the values into matrix and print the same.
7. Write a C program to enter values in a 4*4 matrix and print matrix that divisible by 2.
8. Write a C program to find out maximum and minimum matrix.

Program: Write a C program to enter a string and print the same.
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0;
char name[10];
clrscr();
printf("\n Enter name");
scanf("%s", &name);
while(name[i]!='\0')
{
printf("%c", name[i]);
i++;
}
getch();
}
Output:
Enter name FARAZ
FARAZ

     
Program: Write a C program to find out number of letters in a string.
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0, a=0;
char name[10];
clrscr();
printf("\n Enter name");
scanf("%s", &name);
while(name[i]!='\0')
{
a=a+1;
i++;
}
printf("No. of letters=%d", a);
getch();
}
Output:
Enter name 
FARAZ
Result=5
     
Program: Write a C program to check that you entered alphabet, digit or special character and print the same.
#include<stdio.h>
#include<conio.h>
void main()
{
int alpha=0, digit=0, sc=0, i=0;
char str[20];
clrscr();
printf("\n Enter string");
scanf("%s", &str[i]);
while(str[i]!='\0')
{
if(str[i]>='a' && str[i]<='z')
alpha++;
else
if(str[i]>='0' && str[i]<='9')
digit++;
else
sc++;
i++;
}
printf("\n Alphabet=%d", alpha);
printf("\n Digit=%d", digit);
printf("\n Special character=%d", sc);
getch();
}
Outptu:
Enter string
a
Alphabet=a
Enter string 
5
Digit=5
Enter string
@
Special character=@

Program: Write a C program to convert small letters into capital letters.
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0;
char str[20];
clrscr();
printf('\n Enter string");
scanf("%s", &str[i]);
while(str[i]!='\0')
{
str[i]=str[i]-32;
i++;
}
printf("Converted String=%s", str);
getch();
}
Output:
Enter string faraz
Converted String=FARAZ

   
Program: Write a C program to check that the given string is palindrome or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0, j=0;
char str[20];
clrscr();
printf("\n Enter string");
scanf("%s", &str[i]);
while(str[i]!='\0')
{
i++;
}
i--;
while(i>=0)
{
if(str[i]!=str[j])
break;
i--;
j++;
}
if(i>=0)
printf("Not Palindrome");
else
printf('Palindrome");
getch();
}
Output:
Enter string nitin
Palindrome

Program: Write a C program for 3*3 matrix, enter the values into matrix and print the same.
#include<stdio.h>
#include<conio.h>
void main()
{
int mat[3][3], i,j;
clrscr();
for(i=0;i<3; i++)
{
for(j=0; j<3; j++)
{
printf("\n enter values");
scanf("%d", &mat[i][j]);
}
}
for(i=0;i<3; i++)
{
for(j=0; j<3; j++)
{
printf("%d", mat[i][j]);
printf("   ");
}
printf("\n");
}
getch();
}
Output:
Enter values 1
2
3
4
5
6
7
8
9
1  2  3
4  5  6
7  8  9

 
             
Program: Write a C program to enter values in a 4*4 matrix and print matrix that divisible by 2.
#include<stdio.h>
#include<conio.h>
void main()
{
int mat[4][4], i,j;
clrscr();
for(i=0;i<4; i++)
{
for(j=0; j<4; j++)
{
printf("\n enter values");
scanf("%d", &mat[i][j]);
}
}
for(i=0;i<4; i++)
{
for(j=0; j<4; j++)
{
if(mat[i][j]%2==0)
printf("%d", mat[i][j]);
}
printf("\n");
}
getch();
}
Output:
Enter values 2 2 2 2 4 4 4 4 6 6 6 6 8 8 8 8
1 1 1 1 
2 2 2 2
3 3 3 3
4 4 4 4

Program: Write a C program to find out maximum and minimum matrix.
#include<stdio.h>
#include<conio.h>
void main()
{
int mat[3][3], i,j,max,min;
clrscr();
for(i=0;i<3; i++)
{
for(j=0; j<3; j++)
{
printf("\n enter values");
scanf("%d", &mat[i][j]);
}
}
max=min=mat[0][0];
for(i=0;i<3; i++)
{
for(j=0; j<3; j++)
{
if(max<mat[i][j])
max=mat[i][j];
if(min>mat[i][j])
min=mat[i][j];
}
}
printf("%d", max);
printf("%d", min);
printf("\n");
getch();
}

Tuesday, April 14, 2020

'C' Programs Using While Loop & Array:

C-Programs:
1.Write a C program to find maximum and minimum value using while loop.
2.Write a C program to perform mathematical operations using while loop and switch case.
3.Write a C program to enter values in an array and print the same values.
4.Write a C program to enter values in an array and find even numbers.
5.Write a C program to enter values in an array and find out odd numbers.
6.Write a C program to find out maximum and minimum value using for loop.
7.Write a C program to find a value in an array.
8.Write a C program to find the product of ten given numbers.

Write a C program to find maximum and minimum value using while loop.
#include<stdio.h>
#include<conio.h>
void main()
{
int n, max, min;
clrscr();
printf("\n Enter number");
scanf("%d", &n);
min=n;
max=n;
while(n!=99)
{
printf("\n Enter no");
scanf("%d",&n);
if(min>n)
min=n;
if(max<n)
max=n;
}
printf("\n Max=%d", max);
printf("\n Min=%d", min);
getch();
}
Output:
Enter number 10
Enter no 15
Max=15
Min=10
  
Write a C program to perform mathematical operations using while loop and switch case.
#include<stdio.h>
#include<conio.h>
void main()
{
int n1, n2, n3;
char op;
clrscr();
printf("\n Enter n1, n2");
scanf("%d%d", &n1,&n2);
while(op!='?')
{
printf("\n Enter operator");
op=getche();
switch(op)
{
case '+':
n3=n1+n2;
break;
case '-':
n3=n1-n2;
break;
case '/':
n3=n1/n2;
break;
case '*':
n3=n1*n2;
break;
}
printf("\n Result N3=%d",n3);
}
getch();
}
Output:
Enter n1, n2 10
15
Enter operator +
Result N3=25

Write a C program to enter values in an array and print the same values.
#include<stdio.h>
#include<conio.h>
void main()
{
int value[3],;
clrscr();
for(i=0; i<3; i++)
{
printf("\n Enter value");
scanf("%d", &value[i]);
}
for(i=0; i<3; i++)
{
printf("\n Result=%d", value[i]);
}
getch();
}
Output:
Enter value 1
2
3
Result=1
2
3

Write a C program to enter values in an array and find even numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int value[10]. i;
clrscr();
for(i=0; i<=9; i++)
{
printf("\n Enter value");
scanf("%d", &value[i]);
}
for(i=0; i<=9; i++)
{
if(value[i]%2==0)
printf("\n Even=%d", value[i]);
}
getch();
}
Output:
Enter value 1 2 3 4 5 6 7 8 9 10
Even=2
4
6
8
10

Write a C program to enter values in an array and find out odd numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, c=0, value[10];
clrscr();
for(i=0; i<=9; i++)
{
printf("\n enter values");
scanf("\t %d", &value[i]);
}
for(i=0; i<=9; i++)
{
if(value[i]%2!=0)
c=c+1;
}
printf("Odd number=\t %d", c);
getch();
}
Output:
enter values 1 2 3 4 5 6 7 8 9 10
Odd number= 1 3 5 7 9

Write a C program to find out maximum and minimum value using for loop.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, max, min, value[10];
clrscr();
printf("\n Enter value");
for(i=0; i<=9; i++)
{
scanf("\t %d", &value[i]);
}
for(i=0; i<=9; i++)
{
max=min=value[0];
}
for(i=0; i<=9; i++)
{
if(max<value[i])
max=value[i];
if(min>value[i])
min=value[i];
}
printf("\n Maximum=%d", max);
printf("\n Minimum=%d", min);
getch();
}
Output:
Enter value 1
2
3
4
5
6
7
8
9
10
Maximum=10
Minimum=1
   
Write a C program to find a value in an array.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, n, value[10];
clrscr();
for(i=0; i<=9; i++)
{
printf("\n Enter value");
scanf("%d", &value[i]);
}
printf("\n Enter No. to be search");
scanf("%d", &n);
for(i=0; i<=9; i++)
{
if(value[i]==n)
break;
}
if(i==10)
printf(" not found");
else
printf("found");
getch();
}

Output:
Enter value 5
2
3
6
4
1
7
8
9
10
Enter No. to be search 5
found

Write a C program to find the product of ten given numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, value[10], a=1;
clrscr();
for(i=0; i<=9; i++)
{
printf("\n Enter value");
scanf("%d", value[i]);
}
for(i=0; i<=9; i++)
{
a=1;
for(j=1; j<=value[i]; j++)
{
a=a*j;
}
printf("%d\n ", a);
}
getch();
}

Tuesday, April 7, 2020

Factorial, Even number, Print Triangle & Pyramid of numbers:

Programs:
Write a C program to find the factorial of N numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, f=1,n;
clrscr();
printf(“\n enter any number”);
scanf(“%d”, &n);
for(i=1; i<=n; i++)
{
f=f*i;
}
printf(“Factorial of given number is %d”,f);
getch();
}

Output:
enter any number 
5
Factorial of given number is 120

 

Write a C program to print the following triangle of numbers.
1
12
123
1234
12345

#include<stdio.h>
#include<conio.h>
void main()
{
int i, j;
clrscr();
for(i=1; i<=5; i++)
{
for(j=1; j<=i; j++)
{
printf(“%d”, j);
}
printf(“\n”);
}
getch();
}

Output:
1
12
123
1234
12345
  
Write a C program to print the following triangle of numbers.
55555
4444
333
22
1

#include<stdio.h>
#include<conio.h>
void main()
{
int i, j;
clrscr();
for(i=5; i>=1; i--)
{
for(j=i; j>=1; j--)
{
printf(“%d”, i);
}
printf(“\n”);
}
getch();
}
Output:
55555
4444
333
22
1

     
Write a C program to print the following triangle.
*
* *
* * *
* * * *
* * * * *

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1; i<=5; i++)
{
for(j=1; j<=i; j++)
{
printf(“ * “, j);
}
printf(“\n”);
}
getch();
}

Output:
*
* *
* * *
* * * *
* * * * *
Write a C program to print the following triangle of numbers.
54321
4321
321
21
1

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=5; i>=1; i--)
{
for(j=i; j>=1; j--)
{
printf(“%d”, j);
printf(“\n”);
}
}
getch();
}
Output:
54321
4321
321
21
1

Write a C program to print the following triangle of numbers.
1
22
333
4444
55555

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1; i<=5; i++)
{
for(j=1; j<=i; j++)
{
printf(“%d”, i);
}
printf(“\n”);
}
getch();
}
Output:
1
22
333
4444
55555

Write a C program to print the following triangle of numbers.
1
2 3
4 5 6
7 8 9 10

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j, k=1;
clrscr();
for(i=1; i<=4; i++)
{
printf(“\n”);
for(j=1; j<=i; j++)
{
printf(“%d”, k);
k++;
}
}
getch();
}
Output:
1
2 3
4 5 6
7 8 9 10

Write a C program to print the following pyramid of numbers.
                1
             2   3
         4    5    6
      7     8    9  10

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j, k=1, l;
clrscr();
for(i=1; i<=4; i++)
{
for(l=4; l>=i; l--)
{
printf(“        “);
for(j=1; j<=i; j++)
{
printf(“%d”, k);
k++;
}
printf(“\n”);
}
getch();
}
Output:
      1
             2   3
         4    5    6
      7     8    9  10

Write a C program to find even number using while loop.

#include<stdio.h>
#include<conio.h>
void main()
{
int n, a=0;
clrscr();
while(n!=99)
{
printf(“\n enter  number”);
scanf(“%d”, &n);
if(n%2==0)
a=a+1;
}
printf(“Even Number=%d”, a);
getch();
}
Output:
enter number 6
Even Number=6

Write a C program to input name and age and print the same.
#include<stdio.h>
#include<conio.h>
void main()
{
int age;
char name[10], ch;
clrscr();
while(ch!=’n’)
{
printf(“\n enter name and age”);
scanf(“%s%d”, &name, &age);
printf(“\n Name=%s”, name);
printf(“\n Age=%d”, age);
printf(“\n do you want to continue”);
scanf(“%c”, ch);
}
getch();
}