C program Important Question with Answare | CSC Engineering

  1. Tell the basic structure of the c program and explain all of the things Header File # include<stdio.h>
    1. Every C program must have a main() function which is the starting point of the program Main(){}
    2. Variables a,b,c
    3. Data type int, float,char.string

 

  1. What are the types of data in c Int, float? chat.string

 

  1. Which type of language is c? High-Level language

 

  1. Give some examples of high-level languages? C,C++,Java, Python

 

  1. Why c is structured programing language?

C is a structured programming language because it solves a massive problem. C programming language divides the problem into smaller modules called functions.

 

  1. Difference between string and array

 

Array String
The array has fixed length It doesn’t have a fixed length
It creates a block in memory It doesn’t create a block in memory
At the same time, it can hold different values At the same time, it can’t have different values
It is a sequential collection It is not a sequential collection
   

 

  1. Different types of loop

For loop, do-while loop, while loop

 

  1. Different between for and while loop

 

For loop While loop
When number of iteration is known that is called for loop When number of iteration is not known we use while loop
Here is built-in loop control variable Here is no built-in loop control variable
For(i=0;i<n;i++){ While(i==0){
Statement; Statement;
} I++;
  }
   
   

 

  1. Which loop is super fast? Why?

While loop. While loops check for the stopping condition first, and may not execute the body that’s why.

 

  1. Difference between if-else and if-else ladder/(else if )

 

The if statement evaluates the code if the condition is true but what if the condition is not true, here comes the else statement. It tells the code what to

do when if the condition is false. The ifelseif ladder statement executes one condition from multiple statements

  1. Difference between call by value and call by references

In Call by value, a copy of the variable is passed whereas in Call by reference, a variable itself is passed.          Call by Value, variables are passed using a straightforward

method whereas Call by Reference, pointers are required to store the address of variables.

 

 

Problem 1: find out the user input is whether odd number or even number input: 2

output:even number #include <stdio.h>

void main(){ int a;

printf(“please enter a number”); scanf(“%d”,&a);

if(a%2==0){

printf(“Even number”);

}

else

printf(“odd number”);

}

 

Problem 2: PI=3.1416, create a function that will show a=b*PI,, call that function from main function, your main function will do a=b/PI

#include <stdio.h> # define PI 3.1416

int s(){

int a,b;

printf(“please enter the value of a”); scanf(“%d”,&a);

b=a*PI;

printf(“%d\n”,b);

}

void main(){ s();

int a,b;

printf(“please enter the value of a”); scanf(“%d”,&a);

b=a/PI;

printf(“%d\n”,b);

}

Problem 3: find out what is your Grate input: 90

output:A

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

int a;

printf(“Please enter your number”); scanf(“%d”,&a);

if(a>=90 && a<=100){ printf(“A”);

}

else if(a>=80 && a<=89){ printf(“B”);

}

else if(a>=70 && a<=79){ printf(“c”);

}

else if(a>=60 && a<=69){ printf(“D”);

}

else

printf(“Fail”);

}

 

Problem 4: Find out whether the number is prime or not input: 2

output:prime

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

int a;

printf(“please enter a number”); scanf(“%d”,&a);

if(a==1 || a==0 ){

printf(“Not Prime”);

}

else if(a==2 || a==3 || a==5 || a==7 || a==11){

printf(“prime”);

}

else if(a%2==0||a%3==0||a%5==0||a%7==0 ||a%11==0){ printf(“not prime”);

}

else

printf(“prime”);

}

 

Problem 5: FInd out whether the 2 user input what is the number status input: 20,30

output: 20 is grater than 30 #include <stdio.h>

void main(){ int a,b;

printf(“please enter the value of a and b”); scanf(“%d%d”,&a,&b);

if(a>b){

printf(” a is grater than b”);

}

else if(a<b){

printf(“a is smaller than b”);

}

else if(a==b){

printf(“a is equal to b”);

}

}

Problem 6: find out your age #include <stdio.h>

void main(){

int t_d,y_d,t_m,y_m,t_y,y_y,n_d,n_m,n_y,c_t_d,c_y_m,c_t_m; printf(“please enter today’s day”);

scanf(“%d”,&t_d);

printf(“please enter today’s month”); scanf(“%d”,&t_m);

printf(“please enter today’s year”); scanf(“%d”,&t_y);

printf(“please enter your birth day”); scanf(“%d”,&y_d);

printf(“please enter your birth month”); scanf(“%d”,&y_m);

printf(“please enter your birth year”); scanf(“%d”,&y_y);

if(t_d>=y_d){

n_d=t_d-y_d;

}

else if(t_d<y_d){

c_t_d=t_d+30; n_d=c_t_d-y_d;

}

if(t_d<y_d){

c_y_m=y_m+1; if(t_m<y_m){ c_t_m=t_m+12;

n_m= c_t_m-c_y_m;

}

else if(t_m>=y_m){ n_m= t_m-y_m;

}

}

if(t_d>=y_d){

if(t_m<y_m){ c_t_m=t_m+12;

n_m= c_t_m-y_m;

}

else if(t_m>=y_m){ n_m= t_m-y_m;

}

}

if (t_m<y_m){

y_y=y_y+1; n_y=t_y-y_y;

}

else if(t_m>=y_m){

n_y=t_y-y_y;

}

 

printf(“your age is day=%d, month=%d, year=%d”,n_d,n_m,n_y);

 

}

  1. Show the program as the power of 2 up to 10 Output: 2,4,6,———– 1024

 

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

int a,i; for(i=1;i<=10;i++){

a=pow(2,i);

printf(“%d\n”,a);

}

}

 

  1. Do square root of user input #include <math.h>

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

double a=8; double i;

i=sqrt(a);

printf(“%lf”,i);

}

 

  1. 1+2+3+4+……………………….+100,, what will be the summation value #include<stdio.h>

void main(){

int i,sum=0; for(i=1;i<=100;i++){

sum=sum+i;

}

printf(“%d\n”,sum);

}

  1. 2,4,8,16—————————————–,,show square of values up to 1000

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

int i; for(i=1;i<=1000;i=i*2){ printf(“%d\n”,i);

}

}

 

  1. 1 to 1000 how many are even number #include<stdio.h>

void main(){

int i, count=0; for(i=1;i<=1000;i++){ if(i%2==0){

count=count+1;

}

}

printf(“%d”,count);

}

 

6 .         From 1 to 100 how many prime numbers are there

void main(){

int i, count=0,cn=0; for(i=1;i<=100;i++){

if(i==2||i==3||i==5||i==7||i==11){ count=count+1;

printf(“%d\n is prime number”,i);

}

else if (i%2==0||i%3==0||i%5==0||i%7==0||i%11==0||i==1){ cn=cn+1;

printf(“%d\n are not prime”,i);

}

else count=count+1;

printf(“%d\n is prime number”,i);

}

printf(“total prime number within this range is =%d\n”,count); printf(“total not prime number within this range is =%d\n”,cn);

}

 

  1. 1+3+5+7+9+————————-+100,,find out total summation of the value #include<stdio.h>

void main(){ int a,sum;

for(a=1;a<=100;a=a+2){

sum=sum+a;

}

printf(“%d\n”,sum);

}

 

  1. 2,4,6,8,————————–1500,,find out how many numbers are even #include<stdio.h>

void main(){

int a,count=0; for(a=1;a<=1500;a++){

if(a%2==0){

count=count+1;

}

}

printf(“%d\n”,count);

}

Or==

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

int a,count=0; for(a=2;a<=1500;a=a+2){

count=count+1;

}

printf(“%d\n”,count);

}

 

  1. 1,3,5,7,9,————————–2000, find out how many numbers are odd #include<stdio.h>

void main(){

int a,count=0; for(a=1;a<=2000;a=a+2){

count=count+1;

}

printf(“%d\n”,count);

}

OR==

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

int a,count=0; for(a=1;a<=2000;a++){

if(a%2==1){

count=count+1;

}

}

printf(“%d\n”,count);

}

 

  1. 3,9,27,81,,,,,,,,,,,,,,,,,,,,,,,,,,,,show the sequence of number upto 1000 #include<stdio.h>

void main(){ int a;

for(a=3;a<=1000;a=a*3){ printf(“%d\n”,a);

}

}

 

  1. 100,97,94,—————————1,,show the sequence of number #include<stdio.h>

void main(){ int a;

for(a=100;a>=1;a=a-3){ printf(“%d\n”,a);

}

}

 

  1. 100+97+94+——————+1,,, show the total summation of the value #include<stdio.h>

void main(){

int a,sum=0; for(a=100;a>=1;a=a-3){ sum=sum+a;

}

printf(“%d\n”,sum);

}

 

  1. 1,2,3,4,5,6,7,8,——————-2000 show how many numbers are divided by among 2,3,and 5

#include<stdio.h>

void main(){

int a,sum=0; for(a=1;a<=2000;a++){

if(a%2==0 && a%3==0 && a%5==0){ sum=sum+1;

}

}

printf(“%d\n”,sum);

}

 

  1. from 1 to 100 show how many numbers are divided by 2 or 3 but must divided by 5 #include<stdio.h>

void main(){

int a,sum=0; for(a=1;a<=100;a++){

if(a%2==0||a%3==0){ if(a%5==0){

sum=sum+1;

printf(“%d\n”,a);

}

}

}

printf(“%d\n”,sum);

}

 

  1. * * * * *

* * * * *

* * * * *

* * * * *

* * * * *

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

int i,n,j;

printf(“how many row u enter now?”); scanf(“%d”,&n);

for(i=1;i<=n;i++){ for(j=1;j<=n;j++){

printf(“*”);

}

printf(“\n”);

}

}

 

  1. * * * * *

* * * * *

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

int i,r,c,j;

printf(“how many column u enter now?”); scanf(“%d”,&c);

printf(“how many row u enter now?”); scanf(“%d”,&r);

for(i=1;i<=c;i++){ for(j=1;j<=r;j++){

printf(“*”);

}

printf(“\n”);

}

}

 

  1. * * * * *

*              *

*              *

*               *

* * * *   *

 

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

int i,r,j;

printf(“how many row u enter now?”);

 

scanf(“%d”,&r); for(i=1;i<=r;i++){

for(j=1;j<=r;j++){

if(i==1 || i==r || j==1 || j==r){ printf(“*”);

}

else

printf(” “);

}

printf(“\n”);

}

}

 

17.

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

int i,r,j;

printf(“how many row u enter now?”); scanf(“%d”,&r);

for(i=1;i<=r;i++){ for(j=1;j<=r;j++){

if(i==1 || i==2||i==r-1 || i==r || j==1||j==2 || j==r-1|| j==r){ printf(“*”);

}

else

printf(” “);

}

printf(“\n”);

}

}

 

18.

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

int i,r,j;

printf(“how many row u enter now?”); scanf(“%d”,&r);

for(i=1;i<=r;i++){ for(j=1;j<=r;j++){

if(i==1|| i==r || j==1||j==2||j==r-1|| j==r){ printf(“*”);

}

else

printf(” “);

}

printf(“\n”);

}

}

 

19.

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

int i,j,n;

printf(“enter the line number”); scanf(“%d”,&n); for(i=1;i<=n;i++){

for(j=1;j<=n;j++){

if(i==1||i==n||j==2||j==n-1){ printf(“*”);

}

else

printf(” “);

}

printf(“\n”);

}

}

 

20.

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

int i,j,n;

printf(“enter the line number”); scanf(“%d”,&n); for(i=1;i<=n;i++){

for(j=1;j<=n;j++){

if(i==1||j==n/2){ printf(“*”);

}

else

printf(” “);

}

printf(“\n”);

}

}

 

21.

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

int i,j,n; scanf(“%d”,&n);

for(i=n;i>=1;i–){ for(j=1;j<=i;j++){

printf(“*”);

}

 

printf(“\n”);

}

}

22.

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

int i,j,n;

printf(“enter the line number”); scanf(“%d”,&n); for(i=1;i<=n;i++){

for(j=1;j<i;j++){ printf(” “);

}

for(j=i;j<=n;j++){ printf(“*”);

}

printf(“\n”);

}

}

 

23.

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

int i,j,n;

printf(“enter the line number”);

scanf(“%d”,&n); for(i=1;i<=n;i++){

for(j=1;j<=n;j++){

if(i==1||i==n||j==2||j==n-1){ printf(“*”);

}

else

printf(” “);

}

printf(“\n”);

}

}

 

24.

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

int i,j,n;

printf(“enter the line number”); scanf(“%d”,&n); for(i=1;i<=n;i++){

for(j=1;j<=n;j++){

if(i==1||i==n||j==n/2){ printf(“*”);

}

else

printf(” “);

}

printf(“\n”);

}

}

 

25.

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

int i,j,n;

printf(“enter the line number”); scanf(“%d”,&n); for(i=1;i<=n;i++){

for(j=1;j<=i;j++){ printf(“*”);

}

printf(“\n”);

}

}

 

26.

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

int i,j,n;

printf(“enter the line number”); scanf(“%d”,&n); for(i=1;i<=n;i++){

for(j=1;j<=i;j++){

if(i==1||j==2||j==n-1){ printf(“*”);

}

else

printf(” “);

}

printf(“\n”);

}

}

 

27.

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

int i,j,n;

printf(“enter the line number”); scanf(“%d”,&n); for(i=1;i<=n;i++){

for(j=1;j<=i;j++){ if(i==1||j==1){ printf(“*”);

}

else

printf(” “);

}

printf(“\n”);

}

}

 

28.

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

int i,j,n;

printf(“enter the line number”); scanf(“%d”,&n); for(i=1;i<=n;i++){

for(j=1;j<=i;j++){ if(i==n||j==1){ printf(“*”);

}

else

printf(” “);

}

printf(“\n”);

}

}

  1. Search array elements:
  2. #include<stdio.h> // search array elements
  3. void main(){
  4. int v[5],i,a,f=0;
  5. printf(“enter array elements\n”); 6. for(i=0;i<5;i++){
  6. scanf(“%d”,&v[i]); 8.          }
  7. printf(“display the values\n”); 10. for(i=0;i<5;i++){
  8. printf(“%d\n”,v[i]);
  9. }
  10. printf(“which value u want to search???????”);
  11. scanf(“%d”,&a); 15. for(i=0;i<5;i++){ 16. if(v[i]==a){
  12. f=1;
  13. break;
  14. }
  15. }
  16. if(f==1){
  17. printf(“match found”);
  18. }
  1. else
  2. printf(“no match found”);
  3. }

 

 

  1. Array input-output elements
  1. #include<stdio.h> // search array elements
  2. void main(){
  3. int v[5],i,a,f=0;
  4. printf(“enter array elements\n”); 5. for(i=0;i<5;i++){
  5. scanf(“%d”,&v[i]); 7.          }
  6. printf(“display the values\n”); 9. for(i=0;i<5;i++){
  7. printf(“%d\n”,v[i]);
  8. }

 

  1. Find the average number

 

#include<stdio.h>// average mark void main(){

int i,avg,sum=0,n[5]; printf(“Enter the marks”); for(i=0;i<5;i++){

scanf(“%d”,&n[i]); sum=sum+n[i];

}

 

avg=sum/5; 

printf(“the average is %d”,avg); 

}

 

  1. Max Number #include<stdio.h> void main(){

int i,arr[5]; 

printf(“Enter the marks”); for(i=0;i<5;i++){

scanf(“%d”,&arr[i]);

}

for(i=1;i<5;i++){

if(arr[0]<arr[i]){

arr[0]=arr[i];

}

}

printf(“%d”,arr[0]);

 

}

  • Insert element in the array

int main()

{

int array[100], position, i, value; printf(“Enter elements\n” );

for (i = 0; i < 5; i++){ scanf(“%d”, &array[i]);

}

printf(“Enter the location where you wish to insert an element\n”); scanf(“%d”, &position);

printf(“Enter the value to insert\n”); scanf(“%d”, &value);

for (i = 5 – 1; i >= position – 1; i–){ array[i+1] = array[i];

}

array[position-1] = value;

printf(“Resultant array is\n”); for (i = 0; i <= 5; i++){

printf(“%d\n”, array[i]);

}

return 0;

}

 

  1. Delete an element from the array

 

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

int a[5],p,i; 

printf(“Enter the elements in array”); for(i=0;i<5;i++){

scanf(“%d”,&a[i]);

} 

printf(“enter the location where u want to delete”); scanf(“%d”,&p);

if(p>=6){

printf(“deletion is not possible”);

}

else

{

for(i=p-1;i<5-1;i++){

a[i]=a[i+1];

}

printf(“now the result is”); for(i=0;i<4;i++){

printf(“%d\n”,a[i]);

}

}

}

 

  1. #include<stdio.h>// use 2d array to show 2 cities days list const int c=2;// global variable

const int w=7;// global variable void main(){

int t[c][w],i,j;// declare 2d array for( i=0; i<c; i++){

for( j=0;j<w;j++){

printf(“City %d, Day %d::”,i+1,j+1);

scanf(“%d”,&t[i][j]);

}

}

printf(“\n the values we want to display: \n”); for(i=0; i<c; i++){

for( j=0; j<w; j++){

printf(“City %d, Day %d = %d\n”,i+1,j+1,t[i][j]);

}

}

}

 

  1. #include<stdio.h>// enter elements in 3d array void main(){

int i,j,k,t[2][3][2];

printf(“enter elements in 3d array\n”); for(i=0;i<2;i++){

for(j=0;j<3;j++){ for(k=0;k<2;k++){

printf(“row[%d]column[%d]hight[%d]::”,i,j,k);

scanf(“%d”,&t[i][j][k]);

}

}

}

printf(“\nDisplay values\n”); for(i=0;i<2;i++){

for(j=0;j<3;j++){ for(k=0;k<2;k++){

printf(“Row[%d]Column[%d]hight[%d]::%d\n”,i,j,k,t[i][j][k]);

}

}

}

}

  1. Adding matrix in array elements #include <stdio.h>

void main() {

int a[2][2], b[2][2], sum[2][2], i, j; printf(“\nEnter elements of 1st matrix:\n”); for (i = 0; i < 2; ++i)

for (j = 0; j < 2; ++j) {

printf(“Enter element a%d%d: “, i + 1, j + 1); scanf(“%d”, &a[i][j]);

}

printf(“Enter elements of 2nd matrix:\n”); for (i = 0; i < 2; ++i)

for (j = 0; j < 2; ++j) {

printf(“Enter element a%d%d: “, i + 1, j + 1); scanf(“%d”, &b[i][j]);

}

// adding two matrices

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

for (j = 0; j < 2; ++j) { sum[i][j] = a[i][j] + b[i][j];

}

// printing the result

printf(“\nSum of two matrices: \n”); for (i = 0; i < 2; ++i)

for (j = 0; j < 2; ++j) { printf(“%d “, sum[i][j]); if (j == 1) {

printf(“\n\n”);

}

}

Leave a Reply

Your email address will not be published. Required fields are marked *